From 07ca3ecce8b4b4fac7f0f01a8bd019ccdaccb946 Mon Sep 17 00:00:00 2001 From: Carson Morrow Date: Wed, 22 May 2013 14:10:48 -0400 Subject: [PATCH] Add support for rowspans --- examples/rowspan.html | 95 +++++++++++++++++++++++++++++++++++++++++++ stupidtable.js | 28 ++++++++++--- 2 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 examples/rowspan.html diff --git a/examples/rowspan.html b/examples/rowspan.html new file mode 100644 index 0000000..d31668c --- /dev/null +++ b/examples/rowspan.html @@ -0,0 +1,95 @@ + + + + Stupid jQuery table sort (rowspan test) + + + + + + + + + +

Stupid jQuery table sort! (rowspan test)

+ +

Tables using rowspans with colspans are handled just fine.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
intno sortstring
floattime
no sort
15-.1810:00pbanana
95369:00pcoke
2-152.511:00aapple
-5388.511:00azebra
195-8587:00porange
12258.98:00pturnip
+ + + diff --git a/stupidtable.js b/stupidtable.js index 64f434a..ccf9ae0 100644 --- a/stupidtable.js +++ b/stupidtable.js @@ -73,6 +73,27 @@ return clone; }; + // Generates a lookup table for actual TD index from row and index of THs + var column_lookup = {} + var rowspan_lookup = {} + $table.find('thead > tr').each(function (row) { + var col = 0; + $(this).find('th').each(function (item) { + var cols = $(this).attr('colspan') || 1; + var rows = $(this).attr('rowspan') || 1; + // Check for rowspans that impact this row + while (rowspan_lookup[col] > 0) { + --rowspan_lookup[col]; + ++col; + } + // Keep track of rowspans that impact future rows + if (rows > 1) {rowspan_lookup[col] = parseInt(rows,10) - 1;} + column_lookup[row + ',' + item] = col; + col += parseInt(cols,10); + }); + }); + + // ==================================================== // // Begin execution! // // ==================================================== // @@ -81,14 +102,9 @@ $table.on("click", "th", function() { var trs = $table.children("tbody").children("tr"); var $this = $(this); - var th_index = 0; + var th_index = column_lookup[$this.parent().index() + ',' + $this.index()]; var dir = $.fn.stupidtable.dir; - $table.find("th").slice(0, $this.index()).each(function() { - var cols = $(this).attr("colspan") || 1; - th_index += parseInt(cols,10); - }); - // Determine (and/or reverse) sorting direction, default `asc` var sort_dir = $this.data("sort-dir") === dir.ASC ? dir.DESC : dir.ASC;