Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting pairs of rows with tablesorter

http://jsfiddle.net/9sKwJ/66/

tr.spacer { height: 40px; }
$.tablesorter.addWidget({
    id: 'spacer',
    format: function(table) {
        var c = table.config,
        $t = $(table),
        $r = $t.find('tbody').find('tr'),
        i, l, last, col, rows, spacers = [];
        if (c.sortList && c.sortList[0]) {
            $t.find('tr.spacer').removeClass('spacer');
            col = c.sortList[0][0]; // first sorted column
            rows = table.config.cache.normalized;
            last = rows[0][col]; // text from first row
            l = rows.length;
            for (i=0; i < l; i++) {
                // if text from row doesn't match last row,
                // save it to add a spacer
                if (rows[i][col] !== last) {
                    spacers.push(i-1);
                    last = rows[i][col];
                }
            }
            // add spacer class to the appropriate rows
            for (i=0; i<spacers.length; i++){
                $r.eq(spacers[i]).addClass('spacer');
            }
        }
    }
});


$('table').tablesorter({
    widgets : ['spacer']
});
<table id="test">
  <thead>
    <tr>
      <th>Name</th>
      <th>Number</th>
      <th>Another Example</th>
    </tr>
  </thead>
<tbody>

    <tr>
  <td>Test4</td>
  <td>4</td>
  <td>Hello4</td>
  </tr>          

  <tr>
  <td colspan="3">Test4</td>
  </tr>

    <tr>
  <td>Test3</td>
  <td>3</td>
  <td>Hello3</td>
  </tr>          

  <tr>
  <td colspan="3">Test3</td>
  </tr>

    <tr>
  <td>Test2</td>
  <td>2</td>
  <td>Hello2</td>
  </tr>          

  <tr>
  <td colspan="3">Test2</td>
  </tr>

  <tr>
  <td>Test1</td>
  <td>1</td>
  <td>Hello1</td>
  </tr>          

  <tr>
  <td colspan="3">Test1</td>
  </tr>
</tbody>
</table>

This sorts just the way I want it if you sort it by the first column, but the other two columns don't maintain the same paired 'tr' sort im looking for.

Any help on this?

like image 897
user1659043 Avatar asked Sep 19 '12 04:09

user1659043


1 Answers

Use the expand-child class name on each duplicated row:

<tr>
  <td>Test3</td>
  <td>3</td>
  <td>Hello3</td>
</tr>          

<tr class="expand-child">
  <td colspan="3">Test3</td>
</tr>

It's defined by the cssChildRow option:

$('table').tablesorter({
    cssChildRow: "expand-child"
});​

Here is a demo of it in action.

like image 57
Mottie Avatar answered Nov 11 '22 04:11

Mottie