mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
Optimizations: replace arrays using $.each with for loops. See #827
This commit is contained in:
parent
87652f2cb7
commit
873f6d2ee2
@ -135,7 +135,8 @@
|
||||
},
|
||||
|
||||
calcFilters = function(table, p) {
|
||||
var c = table.config,
|
||||
var normalized, indx, len,
|
||||
c = table.config,
|
||||
hasFilters = c.$table.hasClass('hasFilters');
|
||||
if (hasFilters && !p.ajaxUrl) {
|
||||
if ($.isEmptyObject(c.cache)) {
|
||||
@ -143,9 +144,11 @@
|
||||
p.filteredRows = p.totalRows = c.$tbodies.eq(0).children('tr').not( p.countChildRows ? '' : '.' + c.cssChildRow ).length;
|
||||
} else {
|
||||
p.filteredRows = 0;
|
||||
$.each(c.cache[0].normalized, function(i, el) {
|
||||
p.filteredRows += p.regexRows.test(el[c.columns].$row[0].className) ? 0 : 1;
|
||||
});
|
||||
normalized = c.cache[0].normalized;
|
||||
len = normalized.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
p.filteredRows += p.regexRows.test(normalized[indx][c.columns].$row[0].className) ? 0 : 1;
|
||||
}
|
||||
}
|
||||
} else if (!hasFilters) {
|
||||
p.filteredRows = p.totalRows;
|
||||
@ -154,7 +157,7 @@
|
||||
|
||||
updatePageDisplay = function(table, p, completed) {
|
||||
if ( p.initializing ) { return; }
|
||||
var s, t, $out,
|
||||
var s, t, $out, indx, len, options,
|
||||
c = table.config,
|
||||
sz = p.size || p.settings.size || 10; // don't allow dividing by zero
|
||||
if (p.countChildRows) { t.push(c.cssChildRow); }
|
||||
@ -192,9 +195,11 @@
|
||||
});
|
||||
if ( p.$goto.length ) {
|
||||
t = '';
|
||||
$.each(buildPageSelect(p), function(i, opt){
|
||||
t += '<option value="' + opt + '">' + opt + '</option>';
|
||||
});
|
||||
options = buildPageSelect(p);
|
||||
len = options.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
t += '<option value="' + options[indx] + '">' + options[indx] + '</option>';
|
||||
}
|
||||
// innerHTML doesn't work in IE9 - http://support2.microsoft.com/kb/276228
|
||||
p.$goto.html(t).val( p.page + 1 );
|
||||
}
|
||||
@ -532,35 +537,38 @@
|
||||
},
|
||||
|
||||
getAjaxUrl = function(table, p) {
|
||||
var c = table.config,
|
||||
var indx, len,
|
||||
c = table.config,
|
||||
url = (p.ajaxUrl) ? p.ajaxUrl
|
||||
// allow using "{page+1}" in the url string to switch to a non-zero based index
|
||||
.replace(/\{page([\-+]\d+)?\}/, function(s,n){ return p.page + (n ? parseInt(n, 10) : 0); })
|
||||
.replace(/\{size\}/g, p.size) : '',
|
||||
sl = c.sortList,
|
||||
fl = p.currentFilters || $(table).data('lastSearch') || [],
|
||||
sortList = c.sortList,
|
||||
filterList = p.currentFilters || $(table).data('lastSearch') || [],
|
||||
sortCol = url.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),
|
||||
filterCol = url.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),
|
||||
arry = [];
|
||||
if (sortCol) {
|
||||
sortCol = sortCol[1];
|
||||
$.each(sl, function(i,v){
|
||||
arry.push(sortCol + '[' + v[0] + ']=' + v[1]);
|
||||
});
|
||||
len = sortList.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
arry.push(sortCol + '[' + sortList[indx][0] + ']=' + sortList[indx][1]);
|
||||
}
|
||||
// if the arry is empty, just add the col parameter... "&{sortList:col}" becomes "&col"
|
||||
url = url.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : sortCol );
|
||||
arry = [];
|
||||
}
|
||||
if (filterCol) {
|
||||
filterCol = filterCol[1];
|
||||
$.each(fl, function(i,v){
|
||||
if (v) {
|
||||
arry.push(filterCol + '[' + i + ']=' + encodeURIComponent(v));
|
||||
len = filterList.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (filterList[indx]) {
|
||||
arry.push(filterCol + '[' + indx + ']=' + encodeURIComponent(filterList[indx]));
|
||||
}
|
||||
});
|
||||
}
|
||||
// if the arry is empty, just add the fcol parameter... "&{filterList:fcol}" becomes "&fcol"
|
||||
url = url.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : filterCol );
|
||||
p.currentFilters = fl;
|
||||
p.currentFilters = filterList;
|
||||
}
|
||||
if ( typeof(p.customAjaxUrl) === "function" ) {
|
||||
url = p.customAjaxUrl(table, url);
|
||||
|
File diff suppressed because one or more lines are too long
127
dist/js/jquery.tablesorter.js
vendored
127
dist/js/jquery.tablesorter.js
vendored
@ -592,49 +592,50 @@
|
||||
});
|
||||
}
|
||||
|
||||
function updateHeaderSortCount(table, list) {
|
||||
var s, t, o, col, primary,
|
||||
function updateHeaderSortCount( table, list ) {
|
||||
var col, dir, group, header, indx, primary, temp, val,
|
||||
c = table.config,
|
||||
sl = list || c.sortList;
|
||||
sortList = list || c.sortList,
|
||||
len = sortList.length;
|
||||
c.sortList = [];
|
||||
$.each(sl, function(i,v){
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
val = sortList[indx];
|
||||
// ensure all sortList values are numeric - fixes #127
|
||||
col = parseInt(v[0], 10);
|
||||
col = parseInt(val[0], 10);
|
||||
// make sure header exists
|
||||
o = c.$headers.filter('[data-column="' + col + '"]:last')[0];
|
||||
if (o) { // prevents error if sorton array is wrong
|
||||
// o.count = o.count + 1;
|
||||
t = ('' + v[1]).match(/^(1|d|s|o|n)/);
|
||||
t = t ? t[0] : '';
|
||||
header = c.$headers.filter('[data-column="' + col + '"]:last')[0];
|
||||
if (header) { // prevents error if sorton array is wrong
|
||||
dir = ('' + val[1]).match(/^(1|d|s|o|n)/);
|
||||
dir = dir ? dir[0] : '';
|
||||
// 0/(a)sc (default), 1/(d)esc, (s)ame, (o)pposite, (n)ext
|
||||
switch(t) {
|
||||
switch(dir) {
|
||||
case '1': case 'd': // descending
|
||||
t = 1;
|
||||
dir = 1;
|
||||
break;
|
||||
case 's': // same direction (as primary column)
|
||||
// if primary sort is set to 's', make it ascending
|
||||
t = primary || 0;
|
||||
dir = primary || 0;
|
||||
break;
|
||||
case 'o':
|
||||
s = o.order[(primary || 0) % (c.sortReset ? 3 : 2)];
|
||||
temp = header.order[(primary || 0) % (c.sortReset ? 3 : 2)];
|
||||
// opposite of primary column; but resets if primary resets
|
||||
t = s === 0 ? 1 : s === 1 ? 0 : 2;
|
||||
dir = temp === 0 ? 1 : temp === 1 ? 0 : 2;
|
||||
break;
|
||||
case 'n':
|
||||
o.count = o.count + 1;
|
||||
t = o.order[(o.count) % (c.sortReset ? 3 : 2)];
|
||||
header.count = header.count + 1;
|
||||
dir = header.order[(header.count) % (c.sortReset ? 3 : 2)];
|
||||
break;
|
||||
default: // ascending
|
||||
t = 0;
|
||||
dir = 0;
|
||||
break;
|
||||
}
|
||||
primary = i === 0 ? t : primary;
|
||||
s = [ col, parseInt(t, 10) || 0 ];
|
||||
c.sortList.push(s);
|
||||
t = $.inArray(s[1], o.order); // fixes issue #167
|
||||
o.count = t >= 0 ? t : s[1] % (c.sortReset ? 3 : 2);
|
||||
primary = indx === 0 ? dir : primary;
|
||||
group = [ col, parseInt(dir, 10) || 0 ];
|
||||
c.sortList.push(group);
|
||||
dir = $.inArray(group[1], header.order); // fixes issue #167
|
||||
header.count = dir >= 0 ? dir : group[1] % (c.sortReset ? 3 : 2);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getCachedSortType(parsers, i) {
|
||||
@ -1633,7 +1634,8 @@
|
||||
|
||||
ts.applyWidget = function(table, init, callback) {
|
||||
table = $(table)[0]; // in case this is called externally
|
||||
var c = table.config,
|
||||
var indx, len, name,
|
||||
c = table.config,
|
||||
wo = c.widgetOptions,
|
||||
tableClass = ' ' + c.table.className + ' ',
|
||||
widgets = [],
|
||||
@ -1648,9 +1650,10 @@
|
||||
// extract out the widget id from the table class (widget id's can include dashes)
|
||||
w = tableClass.match( wd );
|
||||
if ( w ) {
|
||||
$.each( w, function( i,n ){
|
||||
c.widgets.push( n.replace( wd, '$1' ) );
|
||||
});
|
||||
len = w.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
c.widgets.push( w[indx].replace( wd, '$1' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
if (c.widgets.length) {
|
||||
@ -1659,41 +1662,44 @@
|
||||
c.widgets = $.grep(c.widgets, function(v, k){
|
||||
return $.inArray(v, c.widgets) === k;
|
||||
});
|
||||
name = c.widgets || [];
|
||||
len = name.length;
|
||||
// build widget array & add priority as needed
|
||||
$.each(c.widgets || [], function(i,n){
|
||||
wd = ts.getWidgetById(n);
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
wd = ts.getWidgetById(name[indx]);
|
||||
if (wd && wd.id) {
|
||||
// set priority to 10 if not defined
|
||||
if (!wd.priority) { wd.priority = 10; }
|
||||
widgets[i] = wd;
|
||||
widgets[indx] = wd;
|
||||
}
|
||||
});
|
||||
}
|
||||
// sort widgets by priority
|
||||
widgets.sort(function(a, b){
|
||||
return a.priority < b.priority ? -1 : a.priority === b.priority ? 0 : 1;
|
||||
});
|
||||
// add/update selected widgets
|
||||
$.each(widgets, function(i,w){
|
||||
if (w) {
|
||||
if (init || !(c.widgetInit[w.id])) {
|
||||
len = widgets.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (widgets[indx]) {
|
||||
if ( init || !( c.widgetInit[ widgets[indx].id ] ) ) {
|
||||
// set init flag first to prevent calling init more than once (e.g. pager)
|
||||
c.widgetInit[w.id] = true;
|
||||
if (w.hasOwnProperty('options')) {
|
||||
wo = table.config.widgetOptions = $.extend( true, {}, w.options, wo );
|
||||
c.widgetInit[ widgets[indx].id ] = true;
|
||||
if ( 'options' in widgets[indx] ) {
|
||||
wo = table.config.widgetOptions = $.extend( true, {}, widgets[indx].options, wo );
|
||||
}
|
||||
if (w.hasOwnProperty('init')) {
|
||||
if ( 'init' in widgets[indx] ) {
|
||||
if (c.debug) { time2 = new Date(); }
|
||||
w.init(table, w, c, wo);
|
||||
if (c.debug) { ts.benchmark('Initializing ' + w.id + ' widget', time2); }
|
||||
widgets[indx].init(table, widgets[indx], c, wo);
|
||||
if (c.debug) { ts.benchmark('Initializing ' + widgets[indx].id + ' widget', time2); }
|
||||
}
|
||||
}
|
||||
if (!init && w.hasOwnProperty('format')) {
|
||||
if ( !init && 'format' in widgets[indx] ) {
|
||||
if (c.debug) { time2 = new Date(); }
|
||||
w.format(table, c, wo, false);
|
||||
if (c.debug) { ts.benchmark( ( init ? 'Initializing ' : 'Applying ' ) + w.id + ' widget', time2); }
|
||||
widgets[indx].format(table, c, wo, false);
|
||||
if (c.debug) { ts.benchmark( ( init ? 'Initializing ' : 'Applying ' ) + widgets[indx].id + ' widget', time2); }
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
// callback executed on init only
|
||||
if (!init && typeof callback === 'function') {
|
||||
callback(table);
|
||||
@ -1711,29 +1717,31 @@
|
||||
|
||||
ts.removeWidget = function(table, name, refreshing){
|
||||
table = $(table)[0];
|
||||
var i, widget, indx, len,
|
||||
c = table.config;
|
||||
// if name === true, add all widgets from $.tablesorter.widgets
|
||||
if (name === true) {
|
||||
name = [];
|
||||
$.each( ts.widgets, function(i, w){
|
||||
if (w && w.id) {
|
||||
name.push( w.id );
|
||||
len = ts.widgets.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
widget = ts.widgets[indx];
|
||||
if (widget && widget.id) {
|
||||
name.push( widget.id );
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// name can be either an array of widgets names,
|
||||
// or a space/comma separated list of widget names
|
||||
name = ( $.isArray(name) ? name.join(',') : name || '' ).toLowerCase().split( /[\s,]+/ );
|
||||
}
|
||||
var i, widget, indx,
|
||||
c = table.config,
|
||||
len = name.length;
|
||||
len = name.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
widget = ts.getWidgetById(name[i]);
|
||||
indx = $.inArray( name[i], c.widgets );
|
||||
if ( widget && 'remove' in widget ) {
|
||||
if (c.debug && indx >= 0) { log( 'Removing "' + name[i] + '" widget' ); }
|
||||
widget.remove(table, c, c.widgetOptions, refreshing);
|
||||
c.widgetInit[name[i]] = false;
|
||||
c.widgetInit[ name[i] ] = false;
|
||||
}
|
||||
// don't remove the widget from config.widget if refreshing
|
||||
if (indx >= 0 && refreshing !== true) {
|
||||
@ -1744,18 +1752,21 @@
|
||||
|
||||
ts.refreshWidgets = function(table, doAll, dontapply) {
|
||||
table = $(table)[0]; // see issue #243
|
||||
var c = table.config,
|
||||
var indx,
|
||||
c = table.config,
|
||||
cw = c.widgets,
|
||||
widgets = ts.widgets,
|
||||
len = widgets.length,
|
||||
list = [],
|
||||
callback = function(table){
|
||||
$(table).trigger('refreshComplete');
|
||||
};
|
||||
// remove widgets not defined in config.widgets, unless doAll is true
|
||||
$.each( ts.widgets, function(i, w){
|
||||
if (w && w.id && (doAll || $.inArray( w.id, cw ) < 0)) {
|
||||
list.push( w.id );
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (widgets[indx] && widgets[indx].id && (doAll || $.inArray( widgets[indx].id, cw ) < 0)) {
|
||||
list.push( widgets[indx].id );
|
||||
}
|
||||
});
|
||||
}
|
||||
ts.removeWidget( table, list.join(','), true );
|
||||
if (dontapply !== true) {
|
||||
// call widget init if
|
||||
|
4
dist/js/jquery.tablesorter.min.js
vendored
4
dist/js/jquery.tablesorter.min.js
vendored
File diff suppressed because one or more lines are too long
53
dist/js/jquery.tablesorter.widgets.js
vendored
53
dist/js/jquery.tablesorter.widgets.js
vendored
@ -769,7 +769,8 @@ ts.filter = {
|
||||
}
|
||||
},
|
||||
filterInitComplete: function(c){
|
||||
var wo = c.widgetOptions,
|
||||
var indx, len,
|
||||
wo = c.widgetOptions,
|
||||
count = 0,
|
||||
completed = function(){
|
||||
wo.filter_initialized = true;
|
||||
@ -779,11 +780,12 @@ ts.filter = {
|
||||
if ( $.isEmptyObject( wo.filter_formatter ) ) {
|
||||
completed();
|
||||
} else {
|
||||
$.each( wo.filter_formatterInit, function(i, val) {
|
||||
if (val === 1) {
|
||||
len = wo.filter_formatterInit.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (wo.filter_formatterInit[indx] === 1) {
|
||||
count++;
|
||||
}
|
||||
});
|
||||
}
|
||||
clearTimeout(wo.filter_initTimer);
|
||||
if (!wo.filter_initialized && count === wo.filter_formatterCount) {
|
||||
// filter widget initialized
|
||||
@ -1065,7 +1067,7 @@ ts.filter = {
|
||||
},
|
||||
multipleColumns: function( c, $input ) {
|
||||
// look for multiple columns "1-3,4-6,8" in data-column
|
||||
var ranges, singles, indx,
|
||||
var temp, ranges, range, start, end, singles, i, indx, len,
|
||||
wo = c.widgetOptions,
|
||||
// only target "all" column inputs on initialization
|
||||
// & don't target "all" column inputs if they don't exist
|
||||
@ -1075,31 +1077,32 @@ ts.filter = {
|
||||
// process column range
|
||||
if ( targets && /-/.test( val ) ) {
|
||||
ranges = val.match( /(\d+)\s*-\s*(\d+)/g );
|
||||
$.each(ranges, function(i,v){
|
||||
var t,
|
||||
range = v.split( /\s*-\s*/ ),
|
||||
start = parseInt( range[0], 10 ) || 0,
|
||||
end = parseInt( range[1], 10 ) || ( c.columns - 1 );
|
||||
if ( start > end ) { t = start; start = end; end = t; } // swap
|
||||
len = ranges.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
range = ranges[indx].split( /\s*-\s*/ );
|
||||
start = parseInt( range[0], 10 ) || 0;
|
||||
end = parseInt( range[1], 10 ) || ( c.columns - 1 );
|
||||
if ( start > end ) { temp = start; start = end; end = temp; } // swap
|
||||
if ( end >= c.columns ) { end = c.columns - 1; }
|
||||
for ( ; start <= end; start++ ) {
|
||||
columns.push(start);
|
||||
}
|
||||
// remove processed range from val
|
||||
val = val.replace( v, '' );
|
||||
});
|
||||
val = val.replace( ranges[indx], '' );
|
||||
}
|
||||
}
|
||||
// process single columns
|
||||
if ( targets && /,/.test( val ) ) {
|
||||
singles = val.split( /\s*,\s*/ );
|
||||
$.each( singles, function(i,v) {
|
||||
if (v !== '') {
|
||||
indx = parseInt( v, 10 );
|
||||
len = singles.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (singles[i] !== '') {
|
||||
indx = parseInt( singles[i], 10 );
|
||||
if ( indx < c.columns ) {
|
||||
columns.push( indx );
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
// return all columns
|
||||
if (!columns.length) {
|
||||
@ -1398,7 +1401,7 @@ ts.filter = {
|
||||
},
|
||||
getOptionSource: function(table, column, onlyAvail) {
|
||||
table = $(table)[0];
|
||||
var cts,
|
||||
var cts, indx, len,
|
||||
c = table.config,
|
||||
wo = c.widgetOptions,
|
||||
parsed = [],
|
||||
@ -1440,12 +1443,13 @@ ts.filter = {
|
||||
// unsorted select options
|
||||
return arry;
|
||||
} else {
|
||||
len = arry.length;
|
||||
// parse select option values
|
||||
$.each(arry, function(i, v){
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
// parse array data using set column parser; this DOES NOT pass the original
|
||||
// table cell to the parser format function
|
||||
parsed.push({ t : v, p : c.parsers && c.parsers[column].format( v, table, [], column ) });
|
||||
});
|
||||
parsed.push({ t : arry[indx], p : c.parsers && c.parsers[column].format( arry[indx], table, [], column ) });
|
||||
}
|
||||
|
||||
// sort parsed select options
|
||||
cts = c.textSorter || '';
|
||||
@ -1467,9 +1471,10 @@ ts.filter = {
|
||||
});
|
||||
// rebuild arry from sorted parsed data
|
||||
arry = [];
|
||||
$.each(parsed, function(i, v){
|
||||
arry.push(v.t);
|
||||
});
|
||||
len = parsed.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
arry.push( parsed[indx].t );
|
||||
}
|
||||
return arry;
|
||||
}
|
||||
},
|
||||
|
2
dist/js/jquery.tablesorter.widgets.min.js
vendored
2
dist/js/jquery.tablesorter.widgets.min.js
vendored
File diff suppressed because one or more lines are too long
2
dist/js/widgets/widget-filter.min.js
vendored
2
dist/js/widgets/widget-filter.min.js
vendored
File diff suppressed because one or more lines are too long
2
dist/js/widgets/widget-pager.min.js
vendored
2
dist/js/widgets/widget-pager.min.js
vendored
File diff suppressed because one or more lines are too long
@ -592,49 +592,50 @@
|
||||
});
|
||||
}
|
||||
|
||||
function updateHeaderSortCount(table, list) {
|
||||
var s, t, o, col, primary,
|
||||
function updateHeaderSortCount( table, list ) {
|
||||
var col, dir, group, header, indx, primary, temp, val,
|
||||
c = table.config,
|
||||
sl = list || c.sortList;
|
||||
sortList = list || c.sortList,
|
||||
len = sortList.length;
|
||||
c.sortList = [];
|
||||
$.each(sl, function(i,v){
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
val = sortList[indx];
|
||||
// ensure all sortList values are numeric - fixes #127
|
||||
col = parseInt(v[0], 10);
|
||||
col = parseInt(val[0], 10);
|
||||
// make sure header exists
|
||||
o = c.$headers.filter('[data-column="' + col + '"]:last')[0];
|
||||
if (o) { // prevents error if sorton array is wrong
|
||||
// o.count = o.count + 1;
|
||||
t = ('' + v[1]).match(/^(1|d|s|o|n)/);
|
||||
t = t ? t[0] : '';
|
||||
header = c.$headers.filter('[data-column="' + col + '"]:last')[0];
|
||||
if (header) { // prevents error if sorton array is wrong
|
||||
dir = ('' + val[1]).match(/^(1|d|s|o|n)/);
|
||||
dir = dir ? dir[0] : '';
|
||||
// 0/(a)sc (default), 1/(d)esc, (s)ame, (o)pposite, (n)ext
|
||||
switch(t) {
|
||||
switch(dir) {
|
||||
case '1': case 'd': // descending
|
||||
t = 1;
|
||||
dir = 1;
|
||||
break;
|
||||
case 's': // same direction (as primary column)
|
||||
// if primary sort is set to 's', make it ascending
|
||||
t = primary || 0;
|
||||
dir = primary || 0;
|
||||
break;
|
||||
case 'o':
|
||||
s = o.order[(primary || 0) % (c.sortReset ? 3 : 2)];
|
||||
temp = header.order[(primary || 0) % (c.sortReset ? 3 : 2)];
|
||||
// opposite of primary column; but resets if primary resets
|
||||
t = s === 0 ? 1 : s === 1 ? 0 : 2;
|
||||
dir = temp === 0 ? 1 : temp === 1 ? 0 : 2;
|
||||
break;
|
||||
case 'n':
|
||||
o.count = o.count + 1;
|
||||
t = o.order[(o.count) % (c.sortReset ? 3 : 2)];
|
||||
header.count = header.count + 1;
|
||||
dir = header.order[(header.count) % (c.sortReset ? 3 : 2)];
|
||||
break;
|
||||
default: // ascending
|
||||
t = 0;
|
||||
dir = 0;
|
||||
break;
|
||||
}
|
||||
primary = i === 0 ? t : primary;
|
||||
s = [ col, parseInt(t, 10) || 0 ];
|
||||
c.sortList.push(s);
|
||||
t = $.inArray(s[1], o.order); // fixes issue #167
|
||||
o.count = t >= 0 ? t : s[1] % (c.sortReset ? 3 : 2);
|
||||
primary = indx === 0 ? dir : primary;
|
||||
group = [ col, parseInt(dir, 10) || 0 ];
|
||||
c.sortList.push(group);
|
||||
dir = $.inArray(group[1], header.order); // fixes issue #167
|
||||
header.count = dir >= 0 ? dir : group[1] % (c.sortReset ? 3 : 2);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getCachedSortType(parsers, i) {
|
||||
@ -1633,7 +1634,8 @@
|
||||
|
||||
ts.applyWidget = function(table, init, callback) {
|
||||
table = $(table)[0]; // in case this is called externally
|
||||
var c = table.config,
|
||||
var indx, len, name,
|
||||
c = table.config,
|
||||
wo = c.widgetOptions,
|
||||
tableClass = ' ' + c.table.className + ' ',
|
||||
widgets = [],
|
||||
@ -1648,9 +1650,10 @@
|
||||
// extract out the widget id from the table class (widget id's can include dashes)
|
||||
w = tableClass.match( wd );
|
||||
if ( w ) {
|
||||
$.each( w, function( i,n ){
|
||||
c.widgets.push( n.replace( wd, '$1' ) );
|
||||
});
|
||||
len = w.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
c.widgets.push( w[indx].replace( wd, '$1' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
if (c.widgets.length) {
|
||||
@ -1659,41 +1662,44 @@
|
||||
c.widgets = $.grep(c.widgets, function(v, k){
|
||||
return $.inArray(v, c.widgets) === k;
|
||||
});
|
||||
name = c.widgets || [];
|
||||
len = name.length;
|
||||
// build widget array & add priority as needed
|
||||
$.each(c.widgets || [], function(i,n){
|
||||
wd = ts.getWidgetById(n);
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
wd = ts.getWidgetById(name[indx]);
|
||||
if (wd && wd.id) {
|
||||
// set priority to 10 if not defined
|
||||
if (!wd.priority) { wd.priority = 10; }
|
||||
widgets[i] = wd;
|
||||
widgets[indx] = wd;
|
||||
}
|
||||
});
|
||||
}
|
||||
// sort widgets by priority
|
||||
widgets.sort(function(a, b){
|
||||
return a.priority < b.priority ? -1 : a.priority === b.priority ? 0 : 1;
|
||||
});
|
||||
// add/update selected widgets
|
||||
$.each(widgets, function(i,w){
|
||||
if (w) {
|
||||
if (init || !(c.widgetInit[w.id])) {
|
||||
len = widgets.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (widgets[indx]) {
|
||||
if ( init || !( c.widgetInit[ widgets[indx].id ] ) ) {
|
||||
// set init flag first to prevent calling init more than once (e.g. pager)
|
||||
c.widgetInit[w.id] = true;
|
||||
if (w.hasOwnProperty('options')) {
|
||||
wo = table.config.widgetOptions = $.extend( true, {}, w.options, wo );
|
||||
c.widgetInit[ widgets[indx].id ] = true;
|
||||
if ( 'options' in widgets[indx] ) {
|
||||
wo = table.config.widgetOptions = $.extend( true, {}, widgets[indx].options, wo );
|
||||
}
|
||||
if (w.hasOwnProperty('init')) {
|
||||
if ( 'init' in widgets[indx] ) {
|
||||
if (c.debug) { time2 = new Date(); }
|
||||
w.init(table, w, c, wo);
|
||||
if (c.debug) { ts.benchmark('Initializing ' + w.id + ' widget', time2); }
|
||||
widgets[indx].init(table, widgets[indx], c, wo);
|
||||
if (c.debug) { ts.benchmark('Initializing ' + widgets[indx].id + ' widget', time2); }
|
||||
}
|
||||
}
|
||||
if (!init && w.hasOwnProperty('format')) {
|
||||
if ( !init && 'format' in widgets[indx] ) {
|
||||
if (c.debug) { time2 = new Date(); }
|
||||
w.format(table, c, wo, false);
|
||||
if (c.debug) { ts.benchmark( ( init ? 'Initializing ' : 'Applying ' ) + w.id + ' widget', time2); }
|
||||
widgets[indx].format(table, c, wo, false);
|
||||
if (c.debug) { ts.benchmark( ( init ? 'Initializing ' : 'Applying ' ) + widgets[indx].id + ' widget', time2); }
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
// callback executed on init only
|
||||
if (!init && typeof callback === 'function') {
|
||||
callback(table);
|
||||
@ -1711,29 +1717,31 @@
|
||||
|
||||
ts.removeWidget = function(table, name, refreshing){
|
||||
table = $(table)[0];
|
||||
var i, widget, indx, len,
|
||||
c = table.config;
|
||||
// if name === true, add all widgets from $.tablesorter.widgets
|
||||
if (name === true) {
|
||||
name = [];
|
||||
$.each( ts.widgets, function(i, w){
|
||||
if (w && w.id) {
|
||||
name.push( w.id );
|
||||
len = ts.widgets.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
widget = ts.widgets[indx];
|
||||
if (widget && widget.id) {
|
||||
name.push( widget.id );
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// name can be either an array of widgets names,
|
||||
// or a space/comma separated list of widget names
|
||||
name = ( $.isArray(name) ? name.join(',') : name || '' ).toLowerCase().split( /[\s,]+/ );
|
||||
}
|
||||
var i, widget, indx,
|
||||
c = table.config,
|
||||
len = name.length;
|
||||
len = name.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
widget = ts.getWidgetById(name[i]);
|
||||
indx = $.inArray( name[i], c.widgets );
|
||||
if ( widget && 'remove' in widget ) {
|
||||
if (c.debug && indx >= 0) { log( 'Removing "' + name[i] + '" widget' ); }
|
||||
widget.remove(table, c, c.widgetOptions, refreshing);
|
||||
c.widgetInit[name[i]] = false;
|
||||
c.widgetInit[ name[i] ] = false;
|
||||
}
|
||||
// don't remove the widget from config.widget if refreshing
|
||||
if (indx >= 0 && refreshing !== true) {
|
||||
@ -1744,18 +1752,21 @@
|
||||
|
||||
ts.refreshWidgets = function(table, doAll, dontapply) {
|
||||
table = $(table)[0]; // see issue #243
|
||||
var c = table.config,
|
||||
var indx,
|
||||
c = table.config,
|
||||
cw = c.widgets,
|
||||
widgets = ts.widgets,
|
||||
len = widgets.length,
|
||||
list = [],
|
||||
callback = function(table){
|
||||
$(table).trigger('refreshComplete');
|
||||
};
|
||||
// remove widgets not defined in config.widgets, unless doAll is true
|
||||
$.each( ts.widgets, function(i, w){
|
||||
if (w && w.id && (doAll || $.inArray( w.id, cw ) < 0)) {
|
||||
list.push( w.id );
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (widgets[indx] && widgets[indx].id && (doAll || $.inArray( widgets[indx].id, cw ) < 0)) {
|
||||
list.push( widgets[indx].id );
|
||||
}
|
||||
});
|
||||
}
|
||||
ts.removeWidget( table, list.join(','), true );
|
||||
if (dontapply !== true) {
|
||||
// call widget init if
|
||||
|
@ -775,7 +775,8 @@ ts.filter = {
|
||||
}
|
||||
},
|
||||
filterInitComplete: function(c){
|
||||
var wo = c.widgetOptions,
|
||||
var indx, len,
|
||||
wo = c.widgetOptions,
|
||||
count = 0,
|
||||
completed = function(){
|
||||
wo.filter_initialized = true;
|
||||
@ -785,11 +786,12 @@ ts.filter = {
|
||||
if ( $.isEmptyObject( wo.filter_formatter ) ) {
|
||||
completed();
|
||||
} else {
|
||||
$.each( wo.filter_formatterInit, function(i, val) {
|
||||
if (val === 1) {
|
||||
len = wo.filter_formatterInit.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (wo.filter_formatterInit[indx] === 1) {
|
||||
count++;
|
||||
}
|
||||
});
|
||||
}
|
||||
clearTimeout(wo.filter_initTimer);
|
||||
if (!wo.filter_initialized && count === wo.filter_formatterCount) {
|
||||
// filter widget initialized
|
||||
@ -1071,7 +1073,7 @@ ts.filter = {
|
||||
},
|
||||
multipleColumns: function( c, $input ) {
|
||||
// look for multiple columns "1-3,4-6,8" in data-column
|
||||
var ranges, singles, indx,
|
||||
var temp, ranges, range, start, end, singles, i, indx, len,
|
||||
wo = c.widgetOptions,
|
||||
// only target "all" column inputs on initialization
|
||||
// & don't target "all" column inputs if they don't exist
|
||||
@ -1081,31 +1083,32 @@ ts.filter = {
|
||||
// process column range
|
||||
if ( targets && /-/.test( val ) ) {
|
||||
ranges = val.match( /(\d+)\s*-\s*(\d+)/g );
|
||||
$.each(ranges, function(i,v){
|
||||
var t,
|
||||
range = v.split( /\s*-\s*/ ),
|
||||
start = parseInt( range[0], 10 ) || 0,
|
||||
end = parseInt( range[1], 10 ) || ( c.columns - 1 );
|
||||
if ( start > end ) { t = start; start = end; end = t; } // swap
|
||||
len = ranges.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
range = ranges[indx].split( /\s*-\s*/ );
|
||||
start = parseInt( range[0], 10 ) || 0;
|
||||
end = parseInt( range[1], 10 ) || ( c.columns - 1 );
|
||||
if ( start > end ) { temp = start; start = end; end = temp; } // swap
|
||||
if ( end >= c.columns ) { end = c.columns - 1; }
|
||||
for ( ; start <= end; start++ ) {
|
||||
columns.push(start);
|
||||
}
|
||||
// remove processed range from val
|
||||
val = val.replace( v, '' );
|
||||
});
|
||||
val = val.replace( ranges[indx], '' );
|
||||
}
|
||||
}
|
||||
// process single columns
|
||||
if ( targets && /,/.test( val ) ) {
|
||||
singles = val.split( /\s*,\s*/ );
|
||||
$.each( singles, function(i,v) {
|
||||
if (v !== '') {
|
||||
indx = parseInt( v, 10 );
|
||||
len = singles.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (singles[i] !== '') {
|
||||
indx = parseInt( singles[i], 10 );
|
||||
if ( indx < c.columns ) {
|
||||
columns.push( indx );
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
// return all columns
|
||||
if (!columns.length) {
|
||||
@ -1404,7 +1407,7 @@ ts.filter = {
|
||||
},
|
||||
getOptionSource: function(table, column, onlyAvail) {
|
||||
table = $(table)[0];
|
||||
var cts,
|
||||
var cts, indx, len,
|
||||
c = table.config,
|
||||
wo = c.widgetOptions,
|
||||
parsed = [],
|
||||
@ -1446,12 +1449,13 @@ ts.filter = {
|
||||
// unsorted select options
|
||||
return arry;
|
||||
} else {
|
||||
len = arry.length;
|
||||
// parse select option values
|
||||
$.each(arry, function(i, v){
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
// parse array data using set column parser; this DOES NOT pass the original
|
||||
// table cell to the parser format function
|
||||
parsed.push({ t : v, p : c.parsers && c.parsers[column].format( v, table, [], column ) });
|
||||
});
|
||||
parsed.push({ t : arry[indx], p : c.parsers && c.parsers[column].format( arry[indx], table, [], column ) });
|
||||
}
|
||||
|
||||
// sort parsed select options
|
||||
cts = c.textSorter || '';
|
||||
@ -1473,9 +1477,10 @@ ts.filter = {
|
||||
});
|
||||
// rebuild arry from sorted parsed data
|
||||
arry = [];
|
||||
$.each(parsed, function(i, v){
|
||||
arry.push(v.t);
|
||||
});
|
||||
len = parsed.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
arry.push( parsed[indx].t );
|
||||
}
|
||||
return arry;
|
||||
}
|
||||
},
|
||||
|
@ -426,7 +426,8 @@ ts.filter = {
|
||||
}
|
||||
},
|
||||
filterInitComplete: function(c){
|
||||
var wo = c.widgetOptions,
|
||||
var indx, len,
|
||||
wo = c.widgetOptions,
|
||||
count = 0,
|
||||
completed = function(){
|
||||
wo.filter_initialized = true;
|
||||
@ -436,11 +437,12 @@ ts.filter = {
|
||||
if ( $.isEmptyObject( wo.filter_formatter ) ) {
|
||||
completed();
|
||||
} else {
|
||||
$.each( wo.filter_formatterInit, function(i, val) {
|
||||
if (val === 1) {
|
||||
len = wo.filter_formatterInit.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (wo.filter_formatterInit[indx] === 1) {
|
||||
count++;
|
||||
}
|
||||
});
|
||||
}
|
||||
clearTimeout(wo.filter_initTimer);
|
||||
if (!wo.filter_initialized && count === wo.filter_formatterCount) {
|
||||
// filter widget initialized
|
||||
@ -722,7 +724,7 @@ ts.filter = {
|
||||
},
|
||||
multipleColumns: function( c, $input ) {
|
||||
// look for multiple columns "1-3,4-6,8" in data-column
|
||||
var ranges, singles, indx,
|
||||
var temp, ranges, range, start, end, singles, i, indx, len,
|
||||
wo = c.widgetOptions,
|
||||
// only target "all" column inputs on initialization
|
||||
// & don't target "all" column inputs if they don't exist
|
||||
@ -732,31 +734,32 @@ ts.filter = {
|
||||
// process column range
|
||||
if ( targets && /-/.test( val ) ) {
|
||||
ranges = val.match( /(\d+)\s*-\s*(\d+)/g );
|
||||
$.each(ranges, function(i,v){
|
||||
var t,
|
||||
range = v.split( /\s*-\s*/ ),
|
||||
start = parseInt( range[0], 10 ) || 0,
|
||||
end = parseInt( range[1], 10 ) || ( c.columns - 1 );
|
||||
if ( start > end ) { t = start; start = end; end = t; } // swap
|
||||
len = ranges.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
range = ranges[indx].split( /\s*-\s*/ );
|
||||
start = parseInt( range[0], 10 ) || 0;
|
||||
end = parseInt( range[1], 10 ) || ( c.columns - 1 );
|
||||
if ( start > end ) { temp = start; start = end; end = temp; } // swap
|
||||
if ( end >= c.columns ) { end = c.columns - 1; }
|
||||
for ( ; start <= end; start++ ) {
|
||||
columns.push(start);
|
||||
}
|
||||
// remove processed range from val
|
||||
val = val.replace( v, '' );
|
||||
});
|
||||
val = val.replace( ranges[indx], '' );
|
||||
}
|
||||
}
|
||||
// process single columns
|
||||
if ( targets && /,/.test( val ) ) {
|
||||
singles = val.split( /\s*,\s*/ );
|
||||
$.each( singles, function(i,v) {
|
||||
if (v !== '') {
|
||||
indx = parseInt( v, 10 );
|
||||
len = singles.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (singles[i] !== '') {
|
||||
indx = parseInt( singles[i], 10 );
|
||||
if ( indx < c.columns ) {
|
||||
columns.push( indx );
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
// return all columns
|
||||
if (!columns.length) {
|
||||
@ -1055,7 +1058,7 @@ ts.filter = {
|
||||
},
|
||||
getOptionSource: function(table, column, onlyAvail) {
|
||||
table = $(table)[0];
|
||||
var cts,
|
||||
var cts, indx, len,
|
||||
c = table.config,
|
||||
wo = c.widgetOptions,
|
||||
parsed = [],
|
||||
@ -1097,12 +1100,13 @@ ts.filter = {
|
||||
// unsorted select options
|
||||
return arry;
|
||||
} else {
|
||||
len = arry.length;
|
||||
// parse select option values
|
||||
$.each(arry, function(i, v){
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
// parse array data using set column parser; this DOES NOT pass the original
|
||||
// table cell to the parser format function
|
||||
parsed.push({ t : v, p : c.parsers && c.parsers[column].format( v, table, [], column ) });
|
||||
});
|
||||
parsed.push({ t : arry[indx], p : c.parsers && c.parsers[column].format( arry[indx], table, [], column ) });
|
||||
}
|
||||
|
||||
// sort parsed select options
|
||||
cts = c.textSorter || '';
|
||||
@ -1124,9 +1128,10 @@ ts.filter = {
|
||||
});
|
||||
// rebuild arry from sorted parsed data
|
||||
arry = [];
|
||||
$.each(parsed, function(i, v){
|
||||
arry.push(v.t);
|
||||
});
|
||||
len = parsed.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
arry.push( parsed[indx].t );
|
||||
}
|
||||
return arry;
|
||||
}
|
||||
},
|
||||
|
@ -371,7 +371,8 @@ tsp = ts.pager = {
|
||||
},
|
||||
|
||||
calcFilters: function(table, c) {
|
||||
var wo = c.widgetOptions,
|
||||
var normalized, indx, len,
|
||||
wo = c.widgetOptions,
|
||||
p = c.pager,
|
||||
hasFilters = c.$table.hasClass('hasFilters');
|
||||
if (hasFilters && !wo.pager_ajaxUrl) {
|
||||
@ -380,9 +381,11 @@ tsp = ts.pager = {
|
||||
p.filteredRows = p.totalRows = c.$tbodies.eq(0).children('tr').not( wo.pager_countChildRows ? '' : '.' + c.cssChildRow ).length;
|
||||
} else {
|
||||
p.filteredRows = 0;
|
||||
$.each(c.cache[0].normalized, function(i, el) {
|
||||
p.filteredRows += p.regexRows.test(el[c.columns].$row[0].className) ? 0 : 1;
|
||||
});
|
||||
normalized = c.cache[0].normalized;
|
||||
len = normalized.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
p.filteredRows += p.regexRows.test(normalized[indx][c.columns].$row[0].className) ? 0 : 1;
|
||||
}
|
||||
}
|
||||
} else if (!hasFilters) {
|
||||
p.filteredRows = p.totalRows;
|
||||
@ -391,7 +394,7 @@ tsp = ts.pager = {
|
||||
|
||||
updatePageDisplay: function(table, c, completed) {
|
||||
if ( c.pager.initializing ) { return; }
|
||||
var s, t, $out,
|
||||
var s, t, $out, options, indx, len,
|
||||
wo = c.widgetOptions,
|
||||
p = c.pager,
|
||||
sz = p.size || p.setSize || 10; // don't allow dividing by zero
|
||||
@ -431,9 +434,11 @@ tsp = ts.pager = {
|
||||
});
|
||||
if ( p.$goto.length ) {
|
||||
t = '';
|
||||
$.each(tsp.buildPageSelect(p, c), function(i, opt){
|
||||
t += '<option value="' + opt + '">' + opt + '</option>';
|
||||
});
|
||||
options = tsp.buildPageSelect(p, c);
|
||||
len = options.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
t += '<option value="' + options[indx] + '">' + options[indx] + '</option>';
|
||||
}
|
||||
// innerHTML doesn't work in IE9 - http://support2.microsoft.com/kb/276228
|
||||
p.$goto.html(t).val( p.page + 1 );
|
||||
}
|
||||
@ -762,36 +767,39 @@ tsp = ts.pager = {
|
||||
},
|
||||
|
||||
getAjaxUrl: function(table, c) {
|
||||
var p = c.pager,
|
||||
var indx, len,
|
||||
p = c.pager,
|
||||
wo = c.widgetOptions,
|
||||
url = (wo.pager_ajaxUrl) ? wo.pager_ajaxUrl
|
||||
// allow using "{page+1}" in the url string to switch to a non-zero based index
|
||||
.replace(/\{page([\-+]\d+)?\}/, function(s,n){ return p.page + (n ? parseInt(n, 10) : 0); })
|
||||
.replace(/\{size\}/g, p.size) : '',
|
||||
sl = c.sortList,
|
||||
fl = p.currentFilters || $(table).data('lastSearch') || [],
|
||||
sortList = c.sortList,
|
||||
filterList = p.currentFilters || $(table).data('lastSearch') || [],
|
||||
sortCol = url.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),
|
||||
filterCol = url.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),
|
||||
arry = [];
|
||||
if (sortCol) {
|
||||
sortCol = sortCol[1];
|
||||
$.each(sl, function(i,v){
|
||||
arry.push(sortCol + '[' + v[0] + ']=' + v[1]);
|
||||
});
|
||||
len = sortList.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
arry.push(sortCol + '[' + sortList[indx][0] + ']=' + sortList[indx][1]);
|
||||
}
|
||||
// if the arry is empty, just add the col parameter... "&{sortList:col}" becomes "&col"
|
||||
url = url.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : sortCol );
|
||||
arry = [];
|
||||
}
|
||||
if (filterCol) {
|
||||
filterCol = filterCol[1];
|
||||
$.each(fl, function(i,v){
|
||||
if (v) {
|
||||
arry.push(filterCol + '[' + i + ']=' + encodeURIComponent(v));
|
||||
len = filterList.length;
|
||||
for (indx = 0; indx < len; indx++) {
|
||||
if (filterList[indx]) {
|
||||
arry.push(filterCol + '[' + indx + ']=' + encodeURIComponent(filterList[indx]));
|
||||
}
|
||||
});
|
||||
}
|
||||
// if the arry is empty, just add the fcol parameter... "&{filterList:fcol}" becomes "&fcol"
|
||||
url = url.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : filterCol );
|
||||
p.currentFilters = fl;
|
||||
p.currentFilters = filterList;
|
||||
}
|
||||
if ( $.isFunction(wo.pager_customAjaxUrl) ) {
|
||||
url = wo.pager_customAjaxUrl(table, url);
|
||||
|
Loading…
Reference in New Issue
Block a user