2015-03-05 16:04:59 +00:00
|
|
|
/*! tablesorter Grouping widget - updated 3/5/2015 (v2.21.0) *//*
|
2013-03-26 21:08:44 +00:00
|
|
|
* Requires tablesorter v2.8+ and jQuery 1.7+
|
|
|
|
* by Rob Garrison
|
|
|
|
*/
|
2013-04-13 02:15:57 +00:00
|
|
|
/*jshint browser:true, jquery:true, unused:false */
|
2013-03-26 21:08:44 +00:00
|
|
|
/*global jQuery: false */
|
|
|
|
;(function($){
|
|
|
|
"use strict";
|
2013-10-02 02:30:12 +00:00
|
|
|
var ts = $.tablesorter;
|
2013-03-26 21:08:44 +00:00
|
|
|
|
2013-10-02 02:30:12 +00:00
|
|
|
ts.grouping = {
|
2013-11-14 01:55:43 +00:00
|
|
|
|
2013-12-18 06:34:22 +00:00
|
|
|
types : {
|
|
|
|
number : function(c, $column, txt, num, group){
|
|
|
|
var value, word;
|
|
|
|
if (num > 1 && txt !== '') {
|
|
|
|
if ($column.hasClass(ts.css.sortAsc)) {
|
|
|
|
value = Math.floor(parseFloat(txt)/num) * num;
|
|
|
|
return value > parseFloat(group || 0) ? value : parseFloat(group || 0);
|
|
|
|
} else {
|
|
|
|
value = Math.ceil(parseFloat(txt)/num) * num;
|
|
|
|
return value < parseFloat(group || num) - value ? parseFloat(group || num) - value : value;
|
|
|
|
}
|
2013-10-02 02:30:12 +00:00
|
|
|
} else {
|
2013-12-18 06:34:22 +00:00
|
|
|
word = (txt + '').match(/\d+/g);
|
|
|
|
return word && word.length >= num ? word[num - 1] : txt || '';
|
2013-10-02 02:30:12 +00:00
|
|
|
}
|
2013-12-18 06:34:22 +00:00
|
|
|
},
|
|
|
|
separator : function(c, $column, txt, num){
|
|
|
|
var word = (txt + '').split(c.widgetOptions.group_separator);
|
|
|
|
return $.trim(word && num > 0 && word.length >= num ? word[(num || 1) - 1] : '');
|
|
|
|
},
|
|
|
|
word : function(c, $column, txt, num){
|
|
|
|
var word = (txt + ' ').match(/\w+/g);
|
2013-11-14 01:55:43 +00:00
|
|
|
return word && word.length >= num ? word[num - 1] : txt || '';
|
2013-12-18 06:34:22 +00:00
|
|
|
},
|
|
|
|
letter : function(c, $column, txt, num){
|
|
|
|
return txt ? (txt + ' ').substring(0, num) : '';
|
|
|
|
},
|
|
|
|
date : function(c, $column, txt, part, group){
|
|
|
|
var wo = c.widgetOptions,
|
|
|
|
time = new Date(txt || ''),
|
|
|
|
hours = time.getHours();
|
|
|
|
return part === 'year' ? time.getFullYear() :
|
|
|
|
part === 'month' ? wo.group_months[time.getMonth()] :
|
2014-10-10 02:26:13 +00:00
|
|
|
part === 'monthyear' ? wo.group_months[time.getMonth()] + ' ' + time.getFullYear() :
|
2013-12-18 06:34:22 +00:00
|
|
|
part === 'day' ? wo.group_months[time.getMonth()] + ' ' + time.getDate() :
|
|
|
|
part === 'week' ? wo.group_week[time.getDay()] :
|
|
|
|
part === 'time' ? ('00' + (hours > 12 ? hours - 12 : hours === 0 ? hours + 12 : hours)).slice(-2) + ':' +
|
|
|
|
('00' + time.getMinutes()).slice(-2) + ' ' + ('00' + wo.group_time[hours >= 12 ? 1 : 0]).slice(-2) :
|
|
|
|
wo.group_dateString(time);
|
2013-10-02 02:30:12 +00:00
|
|
|
}
|
|
|
|
},
|
2013-10-21 01:10:33 +00:00
|
|
|
|
2013-12-18 06:34:22 +00:00
|
|
|
update : function(table, c, wo){
|
2013-12-17 23:41:21 +00:00
|
|
|
if ($.isEmptyObject(c.cache)) { return; }
|
2015-02-14 21:00:04 +00:00
|
|
|
var rowIndex, tbodyIndex, currentGroup, $rows, groupClass, grouping, norm_rows, saveName, direction,
|
2013-11-14 01:55:43 +00:00
|
|
|
lang = wo.grouping_language,
|
|
|
|
group = '',
|
2014-03-04 17:26:20 +00:00
|
|
|
savedGroup = false,
|
2013-11-14 01:55:43 +00:00
|
|
|
column = c.sortList[0] ? c.sortList[0][0] : -1;
|
2013-03-26 21:08:44 +00:00
|
|
|
c.$table
|
|
|
|
.find('tr.group-hidden').removeClass('group-hidden').end()
|
|
|
|
.find('tr.group-header').remove();
|
2013-10-02 02:30:12 +00:00
|
|
|
if (wo.group_collapsible) {
|
|
|
|
// clear pager saved spacer height (in case the rows are collapsed)
|
2013-11-14 01:55:43 +00:00
|
|
|
c.$table.data('pagerSavedHeight', 0);
|
2013-10-02 02:30:12 +00:00
|
|
|
}
|
2015-02-27 20:26:16 +00:00
|
|
|
if (column >= 0 && !c.$headerIndexed[column].hasClass('group-false')) {
|
2014-03-04 17:26:20 +00:00
|
|
|
wo.group_currentGroup = ''; // save current groups
|
|
|
|
wo.group_currentGroups = {};
|
|
|
|
|
|
|
|
// group class finds "group-{word/separator/letter/number/date/false}-{optional:#/year/month/day/week/time}"
|
2015-02-27 20:26:16 +00:00
|
|
|
groupClass = (c.$headerIndexed[column].attr('class') || '').match(/(group-\w+(-\w+)?)/g);
|
2014-03-04 17:26:20 +00:00
|
|
|
// grouping = [ 'group', '{word/separator/letter/number/date/false}', '{#/year/month/day/week/time}' ]
|
|
|
|
grouping = groupClass ? groupClass[0].split('-') : ['group','letter',1]; // default to letter 1
|
|
|
|
|
|
|
|
// save current grouping
|
|
|
|
if (wo.group_collapsible && wo.group_saveGroups && ts.storage) {
|
|
|
|
wo.group_currentGroups = ts.storage( table, 'tablesorter-groups' ) || {};
|
|
|
|
// include direction when grouping numbers > 1 (reversed direction shows different range values)
|
|
|
|
direction = (grouping[1] === 'number' && grouping[2] > 1) ? 'dir' + c.sortList[0][1] : '';
|
|
|
|
// combine column, sort direction & grouping as save key
|
|
|
|
saveName = wo.group_currentGroup = '' + column + direction + grouping.join('');
|
|
|
|
if (!wo.group_currentGroups[saveName]) {
|
|
|
|
wo.group_currentGroups[saveName] = [];
|
|
|
|
} else {
|
|
|
|
savedGroup = true;
|
|
|
|
}
|
|
|
|
}
|
2013-11-14 01:55:43 +00:00
|
|
|
for (tbodyIndex = 0; tbodyIndex < c.$tbodies.length; tbodyIndex++) {
|
2015-02-14 21:00:04 +00:00
|
|
|
norm_rows = c.cache[tbodyIndex].normalized;
|
2013-10-02 02:30:12 +00:00
|
|
|
group = ''; // clear grouping across tbodies
|
2013-11-14 01:55:43 +00:00
|
|
|
$rows = c.$tbodies.eq(tbodyIndex).children('tr').not('.' + c.cssChildRow);
|
|
|
|
for (rowIndex = 0; rowIndex < $rows.length; rowIndex++) {
|
|
|
|
if ( $rows.eq(rowIndex).is(':visible') ) {
|
2013-11-25 13:12:45 +00:00
|
|
|
// fixes #438
|
2013-12-18 06:34:22 +00:00
|
|
|
if (ts.grouping.types[grouping[1]]) {
|
2015-02-27 20:26:16 +00:00
|
|
|
currentGroup = norm_rows[rowIndex] ?
|
|
|
|
ts.grouping.types[grouping[1]]( c, c.$headerIndexed[column], norm_rows[rowIndex][column], /date/.test(groupClass) ?
|
2013-11-25 13:12:45 +00:00
|
|
|
grouping[2] : parseInt(grouping[2] || 1, 10) || 1, group, lang ) : currentGroup;
|
|
|
|
if (group !== currentGroup) {
|
|
|
|
group = currentGroup;
|
|
|
|
// show range if number > 1
|
|
|
|
if (grouping[1] === 'number' && grouping[2] > 1 && currentGroup !== '') {
|
|
|
|
currentGroup += ' - ' + (parseInt(currentGroup, 10) +
|
2015-02-27 20:26:16 +00:00
|
|
|
((parseInt(grouping[2],10) - 1) * (c.$headerIndexed[column].hasClass(ts.css.sortAsc) ? 1 : -1)));
|
2013-11-25 13:12:45 +00:00
|
|
|
}
|
|
|
|
if ($.isFunction(wo.group_formatter)) {
|
|
|
|
currentGroup = wo.group_formatter((currentGroup || '').toString(), column, table, c, wo) || currentGroup;
|
|
|
|
}
|
|
|
|
$rows.eq(rowIndex).before('<tr class="group-header ' + c.selectorRemove.slice(1) +
|
2014-03-04 17:26:20 +00:00
|
|
|
'" unselectable="on"><td colspan="' +
|
2013-11-25 13:12:45 +00:00
|
|
|
c.columns + '">' + (wo.group_collapsible ? '<i/>' : '') + '<span class="group-name">' +
|
|
|
|
currentGroup + '</span><span class="group-count"></span></td></tr>');
|
2014-03-04 17:26:20 +00:00
|
|
|
if (wo.group_saveGroups && !savedGroup && wo.group_collapsed && wo.group_collapsible) {
|
|
|
|
// all groups start collapsed
|
|
|
|
wo.group_currentGroups[wo.group_currentGroup].push(currentGroup);
|
|
|
|
}
|
2013-10-02 02:30:12 +00:00
|
|
|
}
|
2013-03-26 21:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 15:34:03 +00:00
|
|
|
c.$table.find('tr.group-header')
|
|
|
|
.bind('selectstart', false)
|
|
|
|
.each(function(){
|
2014-03-12 21:48:52 +00:00
|
|
|
var isHidden, $label, name,
|
2014-02-25 15:34:03 +00:00
|
|
|
$row = $(this),
|
|
|
|
$rows = $row.nextUntil('tr.group-header').filter(':visible');
|
|
|
|
if (wo.group_count || $.isFunction(wo.group_callback)) {
|
|
|
|
$label = $row.find('.group-count');
|
2013-10-18 16:19:30 +00:00
|
|
|
if ($label.length) {
|
|
|
|
if (wo.group_count) {
|
|
|
|
$label.html( wo.group_count.replace(/\{num\}/g, $rows.length) );
|
|
|
|
}
|
|
|
|
if ($.isFunction(wo.group_callback)) {
|
2013-11-14 01:55:43 +00:00
|
|
|
wo.group_callback($row.find('td'), $rows, column, table);
|
2013-10-18 16:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 15:34:03 +00:00
|
|
|
}
|
2015-03-05 13:16:50 +00:00
|
|
|
if (wo.group_saveGroups && wo.group_currentGroups.length && wo.group_currentGroups[wo.group_currentGroup].length) {
|
2014-03-12 21:48:52 +00:00
|
|
|
name = $row.find('.group-name').text().toLowerCase();
|
2014-03-07 18:53:21 +00:00
|
|
|
isHidden = $.inArray( name, wo.group_currentGroups[wo.group_currentGroup] ) > -1;
|
2014-03-04 17:26:20 +00:00
|
|
|
$row.toggleClass('collapsed', isHidden);
|
|
|
|
$rows.toggleClass('group-hidden', isHidden);
|
|
|
|
} else if (wo.group_collapsed && wo.group_collapsible) {
|
|
|
|
$row.addClass('collapsed');
|
2014-02-25 15:34:03 +00:00
|
|
|
$rows.addClass('group-hidden');
|
|
|
|
}
|
|
|
|
});
|
2013-10-18 16:19:30 +00:00
|
|
|
c.$table.trigger(wo.group_complete);
|
2013-03-26 21:08:44 +00:00
|
|
|
}
|
2014-03-04 17:26:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
bindEvents : function(table, c, wo){
|
|
|
|
if (wo.group_collapsible) {
|
|
|
|
wo.group_currentGroups = [];
|
|
|
|
// .on() requires jQuery 1.7+
|
|
|
|
c.$table.on('click toggleGroup', 'tr.group-header', function(event){
|
|
|
|
event.stopPropagation();
|
|
|
|
var isCollapsed, $groups, indx,
|
|
|
|
$this = $(this),
|
2014-03-12 21:48:52 +00:00
|
|
|
name = $this.find('.group-name').text().toLowerCase();
|
2014-03-04 17:26:20 +00:00
|
|
|
// use shift-click to toggle ALL groups
|
|
|
|
if (event.type === 'click' && event.shiftKey) {
|
|
|
|
$this.siblings('.group-header').trigger('toggleGroup');
|
|
|
|
}
|
|
|
|
$this.toggleClass('collapsed');
|
|
|
|
// nextUntil requires jQuery 1.4+
|
|
|
|
$this.nextUntil('tr.group-header').toggleClass('group-hidden', $this.hasClass('collapsed') );
|
|
|
|
// save collapsed groups
|
|
|
|
if (wo.group_saveGroups && ts.storage) {
|
|
|
|
$groups = c.$table.find('.group-header');
|
|
|
|
isCollapsed = $this.hasClass('collapsed');
|
|
|
|
if (!wo.group_currentGroups[wo.group_currentGroup]) {
|
|
|
|
wo.group_currentGroups[wo.group_currentGroup] = [];
|
|
|
|
}
|
|
|
|
if (isCollapsed && wo.group_currentGroup) {
|
|
|
|
wo.group_currentGroups[wo.group_currentGroup].push( name );
|
|
|
|
} else if (wo.group_currentGroup) {
|
|
|
|
indx = $.inArray( name, wo.group_currentGroups[wo.group_currentGroup] );
|
|
|
|
if (indx > -1) {
|
|
|
|
wo.group_currentGroups[wo.group_currentGroup].splice( indx, 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts.storage( table, 'tablesorter-groups', wo.group_currentGroups );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$(wo.group_saveReset).on('click', function(){
|
|
|
|
ts.grouping.clearSavedGroups(table);
|
|
|
|
});
|
|
|
|
c.$table.on('pagerChange.tsgrouping', function(){
|
|
|
|
ts.grouping.update(table, c, wo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clearSavedGroups: function(table){
|
|
|
|
if (table && ts.storage) {
|
|
|
|
ts.storage(table, 'tablesorter-groups', '');
|
|
|
|
ts.grouping.update(table, table.config, table.config.widgetOptions);
|
|
|
|
}
|
2013-12-18 06:34:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ts.addWidget({
|
|
|
|
id: 'group',
|
|
|
|
priority: 100,
|
|
|
|
options: {
|
|
|
|
group_collapsible : true, // make the group header clickable and collapse the rows below it.
|
|
|
|
group_collapsed : false, // start with all groups collapsed
|
2014-03-04 17:26:20 +00:00
|
|
|
group_saveGroups : true, // remember collapsed groups
|
|
|
|
group_saveReset : null, // element to clear saved collapsed groups
|
2013-12-18 06:34:22 +00:00
|
|
|
group_count : ' ({num})', // if not false, the "{num}" string is replaced with the number of rows in the group
|
|
|
|
group_separator : '-', // group name separator; used when group-separator-# class is used.
|
|
|
|
group_formatter : null, // function(txt, column, table, c, wo) { return txt; }
|
|
|
|
group_callback : null, // function($cell, $rows, column, table){}, callback allowing modification of the group header labels
|
|
|
|
group_complete : 'groupingComplete', // event triggered on the table when the grouping widget has finished work
|
|
|
|
|
2015-03-05 13:03:08 +00:00
|
|
|
// checkbox parser text used for checked/unchecked values
|
|
|
|
group_checkbox : [ 'checked', 'unchecked' ],
|
2013-12-18 06:34:22 +00:00
|
|
|
// change these default date names based on your language preferences
|
|
|
|
group_months : [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ],
|
|
|
|
group_week : [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
|
|
|
|
group_time : [ 'AM', 'PM' ],
|
|
|
|
// this function is used when "group-date" is set to create the date string
|
|
|
|
// you can just return date, date.toLocaleString(), date.toLocaleDateString() or d.toLocaleTimeString()
|
|
|
|
// reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Conversion_getter
|
|
|
|
group_dateString : function(date) { return date.toLocaleString(); }
|
|
|
|
},
|
|
|
|
init: function(table, thisWidget, c, wo){
|
2014-03-04 17:26:20 +00:00
|
|
|
ts.grouping.bindEvents(table, c, wo);
|
2013-12-18 06:34:22 +00:00
|
|
|
},
|
|
|
|
format: function(table, c, wo) {
|
|
|
|
ts.grouping.update(table, c, wo);
|
2013-03-26 21:08:44 +00:00
|
|
|
},
|
|
|
|
remove : function(table, c, wo){
|
|
|
|
c.$table
|
|
|
|
.off('click', 'tr.group-header')
|
2014-03-04 17:26:20 +00:00
|
|
|
.off('pagerChange.tsgrouping')
|
2013-03-26 21:08:44 +00:00
|
|
|
.find('.group-hidden').removeClass('group-hidden').end()
|
|
|
|
.find('tr.group-header').remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})(jQuery);
|