Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The HTML:
...
...

The thead and tbody tags must be used.
The thead and tbody tags must be used. Multiple tbody tags may be used; rows within each tbody will be sorted separately.

Add a `data-sort` attribute of "DATATYPE" to the th elements to make them sortable
by that data type. If you don't want that column to be sortable, just omit the
Expand Down Expand Up @@ -117,6 +117,11 @@ or "DESC" first.

<th data-sort="float" data-sort-default="desc">float</th>

Exclude rows from sorting
-------------------------

To omit certain rows from being sorted, add the attribute `data-sort-ignore` to the `<tr>`.

Callbacks
---------

Expand Down
46 changes: 25 additions & 21 deletions stupidtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,36 @@
// More reliable method of forcing a redraw
$table.css("display");

// Run sorting asynchronously on a timout to force browser redraw after
// Run sorting asynchronously on a timeout to force browser redraw after
// `beforetablesort` callback. Also avoids locking up the browser too much.
setTimeout(function() {
// Gather the elements for this column
var column = [];
var sortMethod = sortFns[type];
var trs = $table.children("tbody").children("tr");

// Extract the data for the column that needs to be sorted and pair it up
// with the TR itself into a tuple
trs.each(function(index,tr) {
var $e = $(tr).children().eq(th_index);
var sort_val = $e.data("sort-value");
var order_by = typeof(sort_val) !== "undefined" ? sort_val : $e.text();
column.push([order_by, tr]);
});

// Sort by the data-order-by value
column.sort(function(a, b) { return sortMethod(a[0], b[0]); });
if (sort_dir != dir.ASC)
column.reverse();

// Replace the content of tbody with the sorted rows. Strangely (and
// conveniently!) enough, .append accomplishes this for us.
trs = $.map(column, function(kv) { return kv[1]; });
$table.children("tbody").append(trs);
$table.children("tbody").each(function(index,tbody){
var column = [];
var $tbody = $(tbody);
var trs = $tbody.children("tr").not('[data-sort-ignore]');

// Extract the data for the column that needs to be sorted and pair it up
// with the TR itself into a tuple
trs.each(function(index,tr) {
var $e = $(tr).children().eq(th_index);
var sort_val = $e.data("sort-value");
var order_by = typeof(sort_val) !== "undefined" ? sort_val : $e.text();
column.push([order_by, tr]);
});

// Sort by the data-order-by value
column.sort(function(a, b) { return sortMethod(a[0], b[0]); });
if (sort_dir != dir.ASC)
column.reverse();

// Replace the content of tbody with the sorted rows. Strangely (and
// conveniently!) enough, .append accomplishes this for us.
trs = $.map(column, function(kv) { return kv[1]; });
$tbody.append(trs);
});

// Reset siblings
$table.find("th").data("sort-dir", null).removeClass("sorting-desc sorting-asc");
Expand Down