stupidtable.js
119 lines
| 1 | // Stupid jQuery table plugin. |
| 2 | |
| 3 | // Call on a table |
| 4 | // sortFns: Sort functions for your datatypes. |
| 5 | (function($) { |
| 6 | |
| 7 | $.fn.stupidtable = function(sortFns) { |
| 8 | return this.each(function() { |
| 9 | var $table = $(this); |
| 10 | sortFns = sortFns || {}; |
| 11 | |
| 12 | // Merge sort functions with some default sort functions. |
| 13 | sortFns = $.extend({}, $.fn.stupidtable.default_sort_fns, sortFns); |
| 14 | |
| 15 | |
| 16 | // ==================================================== // |
| 17 | // Begin execution! // |
| 18 | // ==================================================== // |
| 19 | |
| 20 | // Do sorting when THs are clicked |
| 21 | $table.on("click.stupidtable", "thead th", function() { |
| 22 | var $this = $(this); |
| 23 | var th_index = 0; |
| 24 | var dir = $.fn.stupidtable.dir; |
| 25 | |
| 26 | // Account for colspans |
| 27 | $this.parents("tr").find("th").slice(0, $this.index() + 1).each(function() { |
| 28 | var cols = $(this).attr("colspan") || 1; |
| 29 | th_index += parseInt(cols,10); |
| 30 | }); |
| 31 | |
| 32 | th_index = th_index - 1; |
| 33 | |
| 34 | // Determine (and/or reverse) sorting direction, default `asc` |
| 35 | var sort_dir = $this.data("sortDefault") || dir.ASC; |
| 36 | if ($this.data("sortDir")) |
| 37 | sort_dir = $this.data("sortDir") === dir.ASC ? dir.DESC : dir.ASC; |
| 38 | |
| 39 | // Choose appropriate sorting function. |
| 40 | var type = $this.data("sort") || null; |
| 41 | |
| 42 | // Prevent sorting if no type defined |
| 43 | if (type === null) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | // Trigger `beforetablesort` event that calling scripts can hook into; |
| 48 | // pass parameters for sorted column index and sorting direction |
| 49 | $table.trigger("beforetablesort", {column: $this.index(), direction: sort_dir}); |
| 50 | // More reliable method of forcing a redraw |
| 51 | $table.css("display"); |
| 52 | |
| 53 | // Run sorting asynchronously on a timeout to force browser redraw after |
| 54 | // `beforetablesort` callback. Also avoids locking up the browser too much. |
| 55 | setTimeout(function() { |
| 56 | // Gather the elements for this column |
| 57 | var sortMethod = sortFns[type]; |
| 58 | |
| 59 | $table.children("tbody").each(function(index,tbody){ |
| 60 | var column = []; |
| 61 | var $tbody = $(tbody); |
| 62 | var trs = $tbody.children("tr").not('[data-sort-ignore]'); |
| 63 | |
| 64 | // Extract the data for the column that needs to be sorted and pair it up |
| 65 | // with the TR itself into a tuple |
| 66 | trs.each(function(index,tr) { |
| 67 | var $e = $(tr).children().eq(th_index); |
| 68 | var sort_val = $e.data("sortValue"); |
| 69 | var order_by = typeof(sort_val) !== "undefined" ? sort_val : $e.text(); |
| 70 | column.push([order_by, tr]); |
| 71 | }); |
| 72 | |
| 73 | // Sort by the data-order-by value |
| 74 | column.sort(function(a, b) { return sortMethod(a[0], b[0]); }); |
| 75 | |
| 76 | if (sort_dir != dir.ASC) |
| 77 | column.reverse(); |
| 78 | |
| 79 | // Replace the content of tbody with the sorted rows. Strangely (and |
| 80 | // conveniently!) enough, .append accomplishes this for us. |
| 81 | trs = $.map(column, function(kv) { return kv[1]; }); |
| 82 | $tbody.append(trs); |
| 83 | }); |
| 84 | |
| 85 | // Reset siblings |
| 86 | $table.find("th").data("sortDir", null).removeClass("sorting-desc sorting-asc"); |
| 87 | $this.data("sortDir", sort_dir).addClass("sorting-"+sort_dir); |
| 88 | |
| 89 | // Trigger `aftertablesort` event. Similar to `beforetablesort` |
| 90 | $table.trigger("aftertablesort", {column: $this.index(), direction: sort_dir}); |
| 91 | // More reliable method of forcing a redraw |
| 92 | $table.css("display"); |
| 93 | }, 10); |
| 94 | }); |
| 95 | }); |
| 96 | }; |
| 97 | |
| 98 | // Enum containing sorting directions |
| 99 | $.fn.stupidtable.dir = {ASC: "asc", DESC: "desc"}; |
| 100 | |
| 101 | $.fn.stupidtable.default_sort_fns = { |
| 102 | "int": function(a, b) { |
| 103 | return parseInt(a, 10) - parseInt(b, 10); |
| 104 | }, |
| 105 | "float": function(a, b) { |
| 106 | return parseFloat(a) - parseFloat(b); |
| 107 | }, |
| 108 | "string": function(a, b) { |
| 109 | return a.localeCompare(b); |
| 110 | }, |
| 111 | "string-ins": function(a, b) { |
| 112 | a = a.toLocaleLowerCase(); |
| 113 | b = b.toLocaleLowerCase(); |
| 114 | return a.localeCompare(b); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | })(jQuery); |
| 119 |