diff --git a/jquery.tablesorter.js b/jquery.tablesorter.js index c5eabf0..0817a1f 100644 --- a/jquery.tablesorter.js +++ b/jquery.tablesorter.js @@ -1,103 +1,107 @@ /* - * + * * 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: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html - * + * */ /** - * + * * @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 * settings An object literal containing key/value pairs to provide * 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 stripWhitespace (optional) Boolean flag indicating if + * tablesorter should strip whitespace in cell or not. + * 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 */ @@ -131,6 +135,7 @@ decimal: '/\.|\,/g', onRenderHeader: null, selectorHeaders: 'thead th', + stripWhitespace: true, debug: false }; @@ -208,7 +213,8 @@ rowIndex++; if (rows[rowIndex]) { node = getNodeFromRowAndCellIndex(rows, rowIndex, cellIndex); - nodeValue = trimAndGetNodeText(table.config, node); + //nodeValue = trimAndGetNodeText(table.config, node); + nodeValue = getElementText(table.config, node); if (table.config.debug) { log('Checking if value was empty on row:' + rowIndex); } @@ -292,12 +298,12 @@ }; function getElementText(config, node) { - + if (!node) return ""; - - var $node = $(node), - data = $node.attr('data-sort-value'); - if (data !== undefined) return data; + + var $node = $(node), + data = $node.attr('data-sort-value'); + if (data !== undefined) return data; var text = ""; @@ -351,7 +357,7 @@ tableBody[0].appendChild(r[pos][j]); } - // + // } } @@ -385,7 +391,7 @@ } var meta = ($.metadata) ? true : false; - + var header_index = computeTableHeaderCellIndexes(table); var $tableHeaders = $(table.config.selectorHeaders, table).each(function (index) { @@ -393,12 +399,12 @@ this.column = header_index[this.parentNode.rowIndex + "-" + this.cellIndex]; // this.column = index; this.order = formatSortingOrder(table.config.sortInitialOrder); - - - this.count = this.order; + + + this.count = this.order; if (checkHeaderMetadata(this) || checkHeaderOptions(table, index)) this.sortDisabled = true; - if (checkHeaderOptionsSortingLocked(table, index)) this.order = this.lockedOrder = checkHeaderOptionsSortingLocked(table, index); + if (checkHeaderOptionsSortingLocked(table, index)) this.order = this.lockedOrder = checkHeaderOptionsSortingLocked(table, index); if (!this.sortDisabled) { var $th = $(this).addClass(table.config.cssHeader); @@ -497,12 +503,12 @@ }; return false; } - - function checkHeaderOptionsSortingLocked(table, i) { + + 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 c = table.config.widgets; var l = c.length; @@ -580,7 +586,7 @@ } /* sorting methods */ - + var sortWrapper; function multisort(table, sortList, cache) { @@ -731,10 +737,10 @@ var i = this.column; // get current column sort order this.order = this.count++ % 2; - // always sort on the locked order. - if(this.lockedOrder) this.order = this.lockedOrder; - - // user only whants to sort on one + // always sort on the locked order. + if(this.lockedOrder) this.order = this.lockedOrder; + + // user only whants to sort on one // column if (!e[config.sortMultiSortKey]) { // flush the sort list @@ -774,9 +780,9 @@ // set css for headers setHeadersCss($this[0], $headers, config.sortList, sortCSS); appendToTable( - $this[0], multisort( - $this[0], config.sortList, cache) - ); + $this[0], multisort( + $this[0], config.sortList, cache) + ); }, 1); // stop normal event by returning false return false; @@ -889,8 +895,13 @@ id: "text", is: function (s) { return true; - }, format: function (s) { - return $.trim(s.toLocaleLowerCase()); + }, format: function (s, table) { + var c = table.config; + if (c.stripWhitespace == false) { + return $.trim(s.toLocaleLowerCase()); + } else { + return s.toLocaleLowerCase(); + } }, type: "text" }); @@ -980,9 +991,9 @@ if (c.dateFormat == "us") { // reformat the string in ISO format s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$1/$2"); - } + } if (c.dateFormat == "pt") { - s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1"); + s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1"); } else if (c.dateFormat == "uk") { // reformat the string in ISO format s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1"); diff --git a/jquery.tablesorter.min.js b/jquery.tablesorter.min.js index d3ff430..879b394 100644 --- a/jquery.tablesorter.min.js +++ b/jquery.tablesorter.min.js @@ -1,4 +1,2 @@ - -(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",parsers:{},widgets:[],widgetZebra:{css:["even","odd"]},headers:{},widthFixed:false,cancelSelection:true,sortList:[],headerList:[],dateFormat:"us",decimal:'/\.|\,/g',onRenderHeader:null,selectorHeaders:'thead th',debug:false};function benchmark(s,d){log(s+","+(new Date().getTime()-d.getTime())+"ms");}this.benchmark=benchmark;function log(s){if(typeof console!="undefined"&&typeof console.debug!="undefined"){console.log(s);}else{alert(s);}}function buildParserCache(table,$headers){if(table.config.debug){var parsersDebug="";}if(table.tBodies.length==0)return;var rows=table.tBodies[0].rows;if(rows[0]){var list=[],cells=rows[0].cells,l=cells.length;for(var i=0;i1){arr=arr.concat(checkCellColSpan(table,headerArr,row++));}else{if(table.tHead.length==1||(cell.rowSpan>1||!r[row+1])){arr.push(cell);}}}return arr;};function checkHeaderMetadata(cell){if(($.metadata)&&($(cell).metadata().sorter===false)){return true;};return false;}function checkHeaderOptions(table,i){if((table.config.headers[i])&&(table.config.headers[i].sorter===false)){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 c=table.config.widgets;var l=c.length;for(var i=0;i');$("tr:first td",table.tBodies[0]).each(function(){colgroup.append($('').css('width',$(this).width()));});$(table).prepend(colgroup);};}function updateHeaderSortCount(table,sortList){var c=table.config,l=sortList.length;for(var i=0;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){if(table.config.sortLocaleCompare)return a.localeCompare(b);return((ab)?1:0));};function sortTextDesc(a,b){if(table.config.sortLocaleCompare)return b.localeCompare(a);return((ba)?1:0));};function sortNumeric(a,b){return a-b;};function sortNumericDesc(a,b){return b-a;};function getCachedSortType(parsers,i){return parsers[i].type;};this.construct=function(settings){return this.each(function(){if(!this.tHead||!this.tBodies)return;var $this,$document,$headers,cache,config,shiftDown=0,sortOrder;this.config={};config=$.extend(this.config,$.tablesorter.defaults,settings);$this=$(this);$.data(this,"tablesorter",config);$headers=buildHeaders(this);this.config.parsers=buildParserCache(this,$headers);cache=buildCache(this);var sortCSS=[config.cssDesc,config.cssAsc];fixColumnWidth(this);$headers.click(function(e){var totalRows=($this[0].tBodies[0]&&$this[0].tBodies[0].rows.length)||0;if(!this.sortDisabled&&totalRows>0){$this.trigger("sortStart");var $cell=$(this);var i=this.column;this.order=this.count++%2;if(this.lockedOrder)this.order=this.lockedOrder;if(!e[config.sortMultiSortKey]){config.sortList=[];if(config.sortForce!=null){var a=config.sortForce;for(var j=0;j0){$this.trigger("sorton",[config.sortList]);}applyWidget(this);});};this.addParser=function(parser){var l=parsers.length,a=true;for(var i=0;i1){arr=arr.concat(checkCellColSpan(table,headerArr,row++))}else{if(table.tHead.length==1||(cell.rowSpan>1||!r[row+1])){arr.push(cell)}}}return arr};function checkHeaderMetadata(cell){if(($.metadata)&&($(cell).metadata().sorter===false)){return true};return false}function checkHeaderOptions(table,i){if((table.config.headers[i])&&(table.config.headers[i].sorter===false)){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 c=table.config.widgets;var l=c.length;for(var i=0;i');$("tr:first td",table.tBodies[0]).each(function(){colgroup.append($('').css('width',$(this).width()))});$(table).prepend(colgroup)}}function updateHeaderSortCount(table,sortList){var c=table.config,l=sortList.length;for(var i=0;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){if(table.config.sortLocaleCompare)return a.localeCompare(b);return((ab)?1:0))};function sortTextDesc(a,b){if(table.config.sortLocaleCompare)return b.localeCompare(a);return((ba)?1:0))};function sortNumeric(a,b){return a-b};function sortNumericDesc(a,b){return b-a};function getCachedSortType(parsers,i){return parsers[i].type};this.construct=function(settings){return this.each(function(){if(!this.tHead||!this.tBodies)return;var $this,$document,$headers,cache,config,shiftDown=0,sortOrder;this.config={};config=$.extend(this.config,$.tablesorter.defaults,settings);$this=$(this);$.data(this,"tablesorter",config);$headers=buildHeaders(this);this.config.parsers=buildParserCache(this,$headers);cache=buildCache(this);var sortCSS=[config.cssDesc,config.cssAsc];fixColumnWidth(this);$headers.click(function(e){var totalRows=($this[0].tBodies[0]&&$this[0].tBodies[0].rows.length)||0;if(!this.sortDisabled&&totalRows>0){$this.trigger("sortStart");var $cell=$(this);var i=this.column;this.order=this.count++%2;if(this.lockedOrder)this.order=this.lockedOrder;if(!e[config.sortMultiSortKey]){config.sortList=[];if(config.sortForce!=null){var a=config.sortForce;for(var j=0;j0){$this.trigger("sorton",[config.sortList])}applyWidget(this)})};this.addParser=function(parser){var l=parsers.length,a=true;for(var i=0;i