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.
+
+
+
+
+ int |
+ no sort |
+ string |
+
+
+ float |
+ time |
+
+
+ no sort |
+
+
+
+
+ 15 |
+ -.18 |
+ 10:00p |
+ banana |
+
+
+ 95 |
+ 36 |
+ 9:00p |
+ coke |
+
+
+ 2 |
+ -152.5 |
+ 11:00a |
+ apple |
+
+
+ -53 |
+ 88.5 |
+ 11:00a |
+ zebra |
+
+
+ 195 |
+ -858 |
+ 7:00p |
+ orange |
+
+
+ 122 |
+ 58.9 |
+ 8:00p |
+ turnip |
+
+
+
+
+
+
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;