jquery.tablesorter-2.0.5b.js revision d137c0bf04fcd2fd174ec2ef1d94bcb7669d6b4e
/*
*
* TableSorter 2.0 - Client-side table sorting with ease!
* Version 2.0.5b
* @requires jQuery v1.2.3
*
* Copyright (c) 2007 Christian Bach
* Examples and docs at: http://tablesorter.com
* Dual licensed under the MIT and GPL licenses:
*
*/
/**
*
* @description Create a sortable table with multi-column sorting capabilitys
*
* @example $('table').tablesorter();
* @desc Create a simple tablesorter interface.
*
* @example $('table').tablesorter({ sortList:[[0,0],[1,0]] });
* @desc Create a tablesorter interface and sort on the first and secound column column headers.
*
* @example $('table').tablesorter({ headers: { 0: { sorter: false}, 1: {sorter: false} } });
*
* @desc Create a tablesorter interface and disableing the first and second column headers.
*
*
* @example $('table').tablesorter({ headers: { 0: {sorter:"integer"}, 1: {sorter:"currency"} } });
*
* @desc Create a tablesorter interface and set a column parser for the first
* and second column.
*
*
* @param Object
* optional settings.
*
*
* @option String cssHeader (optional) A string of the class name to be appended
* to sortable tr elements in the thead of the table. Default value:
* "header"
*
* @option String cssAsc (optional) A string of the class name to be appended to
* sortable tr elements in the thead on a ascending sort. Default value:
* "headerSortUp"
*
* @option String cssDesc (optional) A string of the class name to be appended
* to sortable tr elements in the thead on a descending sort. Default
* value: "headerSortDown"
*
* @option String sortInitialOrder (optional) A string of the inital sorting
* order can be asc or desc. Default value: "asc"
*
* @option String sortMultisortKey (optional) A string of the multi-column sort
* key. Default value: "shiftKey"
*
* @option String textExtraction (optional) A string of the text-extraction
* method to use. For complex html structures inside td cell set this
* option to "complex", on large tables the complex option can be slow.
* Default value: "simple"
*
* @option Object headers (optional) An array containing the forces sorting
* rules. This option let's you specify a default sorting rule. Default
* value: null
*
* @option Array sortList (optional) An array containing the forces sorting
* rules. This option let's you specify a default sorting rule. Default
* value: null
*
* @option Array sortForce (optional) An array containing forced sorting rules.
* This option let's you specify a default sorting rule, which is
* prepended to user-selected rules. Default value: null
*
* @option Boolean sortLocaleCompare (optional) Boolean flag indicating whatever
* to use String.localeCampare method or not. Default set to true.
*
*
* @option Array sortAppend (optional) An array containing forced sorting rules.
* This option let's you specify a default sorting rule, which is
* appended to user-selected rules. Default value: null
*
* @option Boolean widthFixed (optional) Boolean flag indicating if tablesorter
* should apply fixed widths to the table columns. This is usefull when
* using the pager companion plugin. This options requires the dimension
* jquery plugin. Default value: false
*
* @option Boolean cancelSelection (optional) Boolean flag indicating if
* tablesorter should cancel selection of the table headers text.
* Default value: true
*
* @option Boolean debug (optional) Boolean flag indicating if tablesorter
* should display debuging information usefull for development.
*
* @type jQuery
*
* @name tablesorter
*
* @cat Plugins/Tablesorter
*
* @author Christian Bach/christian.bach@polyester.se
*/
(function ($) {
$.extend({
tablesorter: new
function () {
var parsers = [],
widgets = [];
this.defaults = {
cssHeader: "header",
cssAsc: "headerSortUp",
cssDesc: "headerSortDown",
cssChildRow: "expand-child",
sortInitialOrder: "asc",
sortMultiSortKey: "shiftKey",
sortForce: null,
sortAppend: null,
sortLocaleCompare: true,
textExtraction: "simple",
widgetZebra: {
}, headers: {}, widthFixed: false,
cancelSelection: true,
sortList: [],
headerList: [],
dateFormat: "us",
decimal: '/\.|\,/g',
onRenderHeader: null,
selectorHeaders: 'thead th',
debug: false
};
/* debuging utils */
function benchmark(s, d) {
}
function log(s) {
} else {
alert(s);
}
}
/* parsers utils */
var parsersDebug = "";
}
if (rows[0]) {
var list = [],
for (var i = 0; i < l; i++) {
var p = false;
}
if (!p) {
}
}
}
}
}
return list;
};
node = false,
nodeValue = false,
keepLooking = true;
rowIndex++;
}
} else {
keepLooking = false;
}
}
for (var i = 1; i < l; i++) {
return parsers[i];
}
}
// 0 is always the generic parser (text)
return parsers[0];
}
}
}
function getParserById(name) {
for (var i = 0; i < l; i++) {
return parsers[i];
}
}
return false;
}
/* utils */
function buildCache(table) {
var cacheTime = new Date();
}
cache = {
row: [],
normalized: []
};
for (var i = 0; i < totalRows; ++i) {
/** Add the table data to main data array */
cols = [];
// if this is a child row, add it to the last row's children and
// continue to the next row
// go to the next for loop
continue;
}
for (var j = 0; j < totalCells; ++j) {
}
cols = null;
};
}
return cache;
};
var text = "";
if (!node) return "";
if (config.supportsTextContent) {
} else {
} else {
}
}
} else {
} else {
}
}
return text;
}
var appendTime = new Date()
}
var c = cache,
r = c.row,
n = c.normalized,
rows = [];
for (var i = 0; i < totalRows; i++) {
//var o = ;
for (var j = 0; j < l; j++) {
}
//
}
}
}
rows = null;
}
// apply table widgets
// trigger sortend
setTimeout(function () {
}, 0);
};
function buildHeaders(table) {
var time = new Date();
}
// this.column = index;
if (checkHeaderOptionsSortingLocked(table, index)) this.order = this.lockedOrder = checkHeaderOptionsSortingLocked(table, index);
if (!this.sortDisabled) {
}
// add cell to headerList
});
}
return $tableHeaders;
};
// from:
function computeTableHeaderCellIndexes(t) {
var matrix = [];
var lookup = {};
var c = cells[j];
var firstAvailCol;
}
// Find first available column in the first row
firstAvailCol = k;
break;
}
}
if (typeof(matrix[k]) == "undefined") {
matrix[k] = [];
}
matrixrow[l] = "x";
}
}
}
}
return lookup;
}
var arr = [],
for (var i = 0; i < c.length; i++) {
var cell = c[i];
} else {
}
// headerArr[row] = (i+row);
}
}
return arr;
};
function checkHeaderMetadata(cell) {
return true;
};
return false;
}
function checkHeaderOptions(table, i) {
return true;
};
return false;
}
function checkHeaderOptionsSortingLocked(table, i) {
if ((table.config.headers[i]) && (table.config.headers[i].lockedOrder)) return table.config.headers[i].lockedOrder;
return false;
}
function applyWidget(table) {
var l = c.length;
for (var i = 0; i < l; i++) {
}
}
function getWidgetById(name) {
for (var i = 0; i < l; i++) {
return widgets[i];
}
}
};
function formatSortingOrder(v) {
if (typeof(v) != "Number") {
} else {
return (v == 1) ? 1 : 0;
}
}
function isValueInArray(v, a) {
var l = a.length;
for (var i = 0; i < l; i++) {
if (a[i][0] == v) {
return true;
}
}
return false;
}
// remove all header information
var h = [];
if (!this.sortDisabled) {
h[this.column] = $(this);
}
});
for (var i = 0; i < l; i++) {
}
}
if (c.widthFixed) {
var colgroup = $('<colgroup>');
});
};
}
for (var i = 0; i < l; i++) {
var s = sortList[i],
o = c.headerList[s[0]];
o.count = s[1];
o.count++;
}
}
/* sorting methods */
var sortTime = new Date();
}
var dynamicExp = "var sortWrapper = function(a,b) {",
// TODO: inline functions.
for (var i = 0; i < l; i++) {
var c = sortList[i][0];
// var s = (getCachedSortType(table.config.parsers,c) == "text") ?
// ((order == 0) ? "sortText" : "sortTextDesc") : ((order == 0) ?
// "sortNumeric" : "sortNumericDesc");
// var s = (table.config.parsers[c].type == "text") ? ((order == 0)
// ? makeSortText(c) : makeSortTextDesc(c)) : ((order == 0) ?
// makeSortNumeric(c) : makeSortNumericDesc(c));
var s = (table.config.parsers[c].type == "text") ? ((order == 0) ? makeSortFunction("text", "asc", c) : makeSortFunction("text", "desc", c)) : ((order == 0) ? makeSortFunction("numeric", "asc", c) : makeSortFunction("numeric", "desc", c));
var e = "e" + i;
// + "]); ";
dynamicExp += "else { ";
}
// if value is the same keep orignal order
for (var i = 0; i < l; i++) {
dynamicExp += "}; ";
}
dynamicExp += "return 0; ";
dynamicExp += "}; ";
}
}
return cache;
};
return "(" + a + " == " + b + " ? 0 : (" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : (" + a + " < " + b + ") ? -1 : 1 )));";
return "(" + a + " == " + b + " ? 0 : (" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : (" + b + " < " + a + ") ? -1 : 1 )));";
return "(" + a + " === null && " + b + " === null) ? 0 :(" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : " + a + " - " + b + "));";
return "(" + a + " === null && " + b + " === null) ? 0 :(" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : " + b + " - " + a + "));";
}
};
function makeSortText(i) {
return "((a[" + i + "] < b[" + i + "]) ? -1 : ((a[" + i + "] > b[" + i + "]) ? 1 : 0));";
};
function makeSortTextDesc(i) {
return "((b[" + i + "] < a[" + i + "]) ? -1 : ((b[" + i + "] > a[" + i + "]) ? 1 : 0));";
};
function makeSortNumeric(i) {
return "a[" + i + "]-b[" + i + "];";
};
function makeSortNumericDesc(i) {
return "b[" + i + "]-a[" + i + "];";
};
function sortText(a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
};
function sortTextDesc(a, b) {
return ((b < a) ? -1 : ((b > a) ? 1 : 0));
};
function sortNumeric(a, b) {
return a - b;
};
function sortNumericDesc(a, b) {
return b - a;
};
function getCachedSortType(parsers, i) {
}; /* public methods */
return this.each(function () {
// if no thead or tbody quit.
// declare
// new blank config object
this.config = {};
// merge and extend.
// store common expression for speed
$this = $(this);
// save the settings where they read
// build headers
$headers = buildHeaders(this);
// try to auto detect column type, and store in tables config
// build the cache for the tbody cells
cache = buildCache(this);
// get the css class names, could be done else where.
// fixate columns if the users supplies the fixedWidth option
fixColumnWidth(this);
// apply event handling to headers
// this is to big, perhaps break it out?
function (e) {
// Only call sortStart if sorting is
// enabled.
// store exp, for speed
var $cell = $(this);
// get current column index
var i = this.column;
// get current column sort order
// always sort on the locked order.
// user only whants to sort on one
// column
if (!e[config.sortMultiSortKey]) {
// flush the sort list
for (var j = 0; j < a.length; j++) {
if (a[j][0] != i) {
}
}
}
// add column to sort list
// multi column sorting
} else {
// the user has clicked on an all
// ready sortet column.
// revers the sorting direction
// for all tables.
if (s[0] == i) {
o.count = s[1];
o.count++;
}
}
} else {
// add column to sort list array
}
};
setTimeout(function () {
// set css for headers
);
}, 1);
// stop normal event by returning false
return false;
}
// cancel selection
}).mousedown(function () {
if (config.cancelSelection) {
this.onselectstart = function () {
return false
};
return false;
}
});
// apply easy methods that trigger binded events
var me = this;
setTimeout(function () {
// rebuild parsers.
// rebuild the cache map
}, 1);
// get position from the dom.
// update cache
$(this).trigger("sortStart");
// update and store the sortlist
// update header count index
updateHeaderSortCount(this, sortList);
// set css for headers
// sort the table and append it to the dom
}).bind("appendCache", function () {
appendToTable(this, cache);
}).bind("applyWidgets", function () {
// apply widgets
applyWidget(this);
});
}
// if user has supplied a sort list to constructor.
}
// apply widgets
applyWidget(this);
});
};
a = true;
for (var i = 0; i < l; i++) {
a = false;
}
}
if (a) {
};
};
};
this.formatFloat = function (s) {
var i = parseFloat(s);
return (isNaN(i)) ? 0 : i;
};
this.formatInt = function (s) {
var i = parseInt(s);
return (isNaN(i)) ? 0 : i;
};
// replace all an wanted chars and match.
};
this.clearTableBody = function (table) {
if ($.browser.msie) {
function empty() {
while (this.firstChild)
this.removeChild(this.firstChild);
}
empty.apply(table.tBodies[0]);
} else {
table.tBodies[0].innerHTML = "";
}
};
}
});
// extend plugin scope
$.fn.extend({
tablesorter: $.tablesorter.construct
});
// make shortcut
var ts = $.tablesorter;
// add default parsers
ts.addParser({
id: "text",
is: function (s) {
return true;
}, format: function (s) {
return $.trim(s.toLocaleLowerCase());
}, type: "text"
});
ts.addParser({
id: "digit",
is: function (s, table) {
var c = table.config;
return $.tablesorter.isDigit(s, c);
}, format: function (s) {
return $.tablesorter.formatFloat(s);
}, type: "numeric"
});
ts.addParser({
id: "currency",
is: function (s) {
return /^[£$€?.]/.test(s);
}, format: function (s) {
return $.tablesorter.formatFloat(s.replace(new RegExp(/[£$€]/g), ""));
}, type: "numeric"
});
ts.addParser({
id: "ipAddress",
is: function (s) {
return /^\d{2,3}[\.]\d{2,3}[\.]\d{2,3}[\.]\d{2,3}$/.test(s);
}, format: function (s) {
var a = s.split("."),
r = "",
l = a.length;
for (var i = 0; i < l; i++) {
var item = a[i];
if (item.length == 2) {
r += "0" + item;
} else {
r += item;
}
}
return $.tablesorter.formatFloat(r);
}, type: "numeric"
});
ts.addParser({
id: "url",
is: function (s) {
return /^(https?|ftp|file):\/\/$/.test(s);
}, format: function (s) {
return jQuery.trim(s.replace(new RegExp(/(https?|ftp|file):\/\//), ''));
}, type: "text"
});
ts.addParser({
id: "isoDate",
is: function (s) {
return /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(s);
}, format: function (s) {
return $.tablesorter.formatFloat((s != "") ? new Date(s.replace(
new RegExp(/-/g), "/")).getTime() : "0");
}, type: "numeric"
});
ts.addParser({
id: "percent",
is: function (s) {
return /\%$/.test($.trim(s));
}, format: function (s) {
return $.tablesorter.formatFloat(s.replace(new RegExp(/%/g), ""));
}, type: "numeric"
});
ts.addParser({
id: "usLongDate",
is: function (s) {
return s.match(new RegExp(/^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/));
}, format: function (s) {
}, type: "numeric"
});
id: "shortDate",
is: function (s) {
s = s.replace(/\-/g, "/");
if (c.dateFormat == "us") {
// reformat the string in ISO format
} else if (c.dateFormat == "uk") {
// reformat the string in ISO format
}
}, type: "numeric"
});
id: "time",
is: function (s) {
}, format: function (s) {
}, type: "numeric"
});
id: "metadata",
is: function (s) {
return false;
}, type: "numeric"
});
// add default widgets
id: "zebra",
var time = new Date();
}
odd;
// loop through the visible rows
$tr = $(this);
// style children rows the same way the parent
// row was styled
});
}
}
});
})(jQuery);