stupidtable.js 4.0 KB

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