// Use this funky notation at the recommendation of the jQuery developer docs.(function( $ ) { $.fn.tableSort = function (paramArgs) { // If this table is already being sorted, abort. if(this.data("sorting") != undefined) return; // Set the sorting status of the table. this.data("sorting", true); // Default parameters for the sort. var params = { sortOn: function (that) { return 0; }, sortWith: function(a, b) { return (a.value - b.value); }, hiddenRows: 0, skipOn: null, desc: false }; // Configure user-specified parameters. if(paramArgs != undefined) $.extend(true, params, paramArgs); // Array used to store jQuery objects for rows and their respective // sorting values. var rowsToSort = new Array(); // Iterate through the appropriate rows and run the logic to // retrieve the value to be sorted. this.children("tbody").children("tr").each(function () { var rowId = null; var nextRow = null; var pushRows = new Array(); var sortValue = null; rowId = $(this).attr("id"); // Only run the logic if the row passes the skip test. This is // necessary because there are often hidden rows which correspond to // each entry in the module tables. if( !rowId.match(params.skipOn) ) { // Retrieve the value to be sorted. sortValue = params.sortOn(this); // Get all rows (including hidden rows) to be stored in the // record. nextRow = $(this); for(var i = 0; i <= params.hiddenRows; i++) { pushRows.push(nextRow); nextRow = nextRow.next(); } rowsToSort.push({ value: sortValue, rows: pushRows }); } }); // Run the provided sorting function. rowsToSort.sort(params.sortWith); // Iterate through the sorted array, detach the elements from the // DOM, and re-attach them in the correct order. Also re-assign the // shading classes for correctly alternating table row colors. var that = this; var i = 0; $.each(rowsToSort, function() { $.each(this.rows, function() { this.detach().removeClass("dark light"); }); if(i % 2) { $.each(this.rows, function() { this.detach().addClass("light"); }); } else { $.each(this.rows, function() { this.detach().addClass("dark"); }); } if(params.desc) { // Reverse the order of rows if sort is descending to insure // they still fill the DOM in the right order. $.each(this.rows.reverse(), function() { this.prependTo(that); }); } else { $.each(this.rows, function() { this.appendTo(that); }); } i++; }); // Set the sorting status as finished. this.removeData("sorting"); };})( jQuery );