// Stupid jQuery table plugin. // Call on a table // sortFns: Sort functions for your datatypes. (function($) { $.fn.stupidtable = function(sortFns) { return this.each(function() { var $table = $(this); sortFns = sortFns || {}; // ==================================================== // // Utility functions // // ==================================================== // // Merge sort functions with some default sort functions. sortFns = $.extend({}, { "int": function(a, b) { return parseInt(a, 10) - parseInt(b, 10); }, "float": function(a, b) { return parseFloat(a) - parseFloat(b); }, "string": function(a, b) { if (a < b) return -1; if (a > b) return +1; return 0; }, "string-ins": function(a, b) { a = a.toLowerCase(); b = b.toLowerCase(); if (a < b) return -1; if (a > b) return +1; return 0; } }, sortFns); // Return the resulting indexes of a sort so we can apply // this result elsewhere. This returns an array of index numbers. // return[0] = x means "arr's 0th element is now at x" var sort_map = function(arr, sort_function, reverse_column) { var map = []; var index = 0; if (reverse_column) { for (var i = arr.length-1; i >= 0; i--) { map.push(i); } } else { var sorted = arr.slice(0).sort(sort_function); for (var i=0; i'); $('.stupidlink', stupidtables).on('click', function(e) {e.preventDefault();}); // Init stupidtable sorting stupidtables.stupidtable(); // Update class of sort link after sort to match foodsoft style stupidtables.on('aftertablesort', function(e, data) { // Ignore data and use the updated classes in DOM var stupidthead = $('thead', this); $('a.stupidlink', stupidthead).removeClass('sortup sortdown'); $('th.sorting-asc a.stupidlink', stupidthead).addClass('sortup'); $('th.sorting-desc a.stupidlink', stupidthead).addClass('sortdown'); }); // Sort tables with a default sort $('.default-sort', stupidtables).trigger('click'); } });