diff --git a/dist/js/widgets/widget-grouping.min.js b/dist/js/widgets/widget-grouping.min.js index 4c5005a8..cb8b0920 100644 --- a/dist/js/widgets/widget-grouping.min.js +++ b/dist/js/widgets/widget-grouping.min.js @@ -1,2 +1,2 @@ /*! Widget: grouping - updated 3/5/2015 (v2.21.0) */ -!function(a){"use strict";var b=a.tablesorter;b.grouping={types:{number:function(a,c,d,e,f){var g,h;return e>1&&""!==d?c.hasClass(b.css.sortAsc)?(g=Math.floor(parseFloat(d)/e)*e,g>parseFloat(f||0)?g:parseFloat(f||0)):(g=Math.ceil(parseFloat(d)/e)*e,g=e?h[e-1]:d||"")},separator:function(b,c,d,e){var f=(d+"").split(b.widgetOptions.group_separator);return a.trim(f&&e>0&&f.length>=e?f[(e||1)-1]:"")},word:function(a,b,c,d){var e=(c+" ").match(/\w+/g);return e&&e.length>=d?e[d-1]:c||""},letter:function(a,b,c,d){return c?(c+" ").substring(0,d):""},date:function(a,b,c,d,e){var f=a.widgetOptions,g=new Date(c||""),h=g.getHours();return"year"===d?g.getFullYear():"month"===d?f.group_months[g.getMonth()]:"monthyear"===d?f.group_months[g.getMonth()]+" "+g.getFullYear():"day"===d?f.group_months[g.getMonth()]+" "+g.getDate():"week"===d?f.group_week[g.getDay()]:"time"===d?("00"+(h>12?h-12:0===h?h+12:h)).slice(-2)+":"+("00"+g.getMinutes()).slice(-2)+" "+("00"+f.group_time[h>=12?1:0]).slice(-2):f.group_dateString(g)}},update:function(c,d,e){if(!a.isEmptyObject(d.cache)){var f,g,h,i,j,k,l,m,n,o=e.grouping_language,p="",q=!1,r=d.sortList[0]?d.sortList[0][0]:-1;if(d.$table.find("tr.group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove(),e.group_collapsible&&d.$table.data("pagerSavedHeight",0),r>=0&&!d.$headerIndexed[r].hasClass("group-false")){for(e.group_currentGroup="",e.group_currentGroups={},j=(d.$headerIndexed[r].attr("class")||"").match(/(group-\w+(-\w+)?)/g),k=j?j[0].split("-"):["group","letter",1],e.group_collapsible&&e.group_saveGroups&&b.storage&&(e.group_currentGroups=b.storage(c,"tablesorter-groups")||{},n="number"===k[1]&&k[2]>1?"dir"+d.sortList[0][1]:"",m=e.group_currentGroup=""+r+n+k.join(""),e.group_currentGroups[m]?q=!0:e.group_currentGroups[m]=[]),g=0;g1&&""!==h&&(h+=" - "+(parseInt(h,10)+(parseInt(k[2],10)-1)*(d.$headerIndexed[r].hasClass(b.css.sortAsc)?1:-1))),a.isFunction(e.group_formatter)&&(h=e.group_formatter((h||"").toString(),r,c,d,e)||h),i.eq(f).before(''+(e.group_collapsible?"":"")+''+h+''),e.group_saveGroups&&!q&&e.group_collapsed&&e.group_collapsible&&e.group_currentGroups[e.group_currentGroup].push(h)));d.$table.find("tr.group-header").bind("selectstart",!1).each(function(){var b,d,f,g=a(this),h=g.nextUntil("tr.group-header").filter(":visible");(e.group_count||a.isFunction(e.group_callback))&&(d=g.find(".group-count"),d.length&&(e.group_count&&d.html(e.group_count.replace(/\{num\}/g,h.length)),a.isFunction(e.group_callback)&&e.group_callback(g.find("td"),h,r,c))),e.group_saveGroups&&e.group_currentGroups.length&&e.group_currentGroups[e.group_currentGroup].length?(f=g.find(".group-name").text().toLowerCase(),b=a.inArray(f,e.group_currentGroups[e.group_currentGroup])>-1,g.toggleClass("collapsed",b),h.toggleClass("group-hidden",b)):e.group_collapsed&&e.group_collapsible&&(g.addClass("collapsed"),h.addClass("group-hidden"))}),d.$table.trigger(e.group_complete)}}},bindEvents:function(c,d,e){e.group_collapsible&&(e.group_currentGroups=[],d.$table.on("click toggleGroup keyup","tr.group-header",function(f){if(f.stopPropagation(),"keyup"!==f.type||13===f.which){var g,h,i,j=a(this),k=j.find(".group-name").text().toLowerCase();!f.shiftKey||"click"!==f.type&&"keyup"!==f.type||j.siblings(".group-header").trigger("toggleGroup"),j.toggleClass("collapsed"),j.nextUntil("tr.group-header").toggleClass("group-hidden",j.hasClass("collapsed")),e.group_saveGroups&&b.storage&&(h=d.$table.find(".group-header"),g=j.hasClass("collapsed"),e.group_currentGroups[e.group_currentGroup]||(e.group_currentGroups[e.group_currentGroup]=[]),g&&e.group_currentGroup?e.group_currentGroups[e.group_currentGroup].push(k):e.group_currentGroup&&(i=a.inArray(k,e.group_currentGroups[e.group_currentGroup]),i>-1&&e.group_currentGroups[e.group_currentGroup].splice(i,1)),b.storage(c,"tablesorter-groups",e.group_currentGroups))}})),a(e.group_saveReset).on("click",function(){b.grouping.clearSavedGroups(c)}),d.$table.on("pagerChange.tsgrouping",function(){b.grouping.update(c,d,e)})},clearSavedGroups:function(a){a&&b.storage&&(b.storage(a,"tablesorter-groups",""),b.grouping.update(a,a.config,a.config.widgetOptions))}},b.addWidget({id:"group",priority:100,options:{group_collapsible:!0,group_collapsed:!1,group_saveGroups:!0,group_saveReset:null,group_count:" ({num})",group_separator:"-",group_formatter:null,group_callback:null,group_complete:"groupingComplete",group_checkbox:["checked","unchecked"],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"],group_dateString:function(a){return a.toLocaleString()}},init:function(a,c,d,e){b.grouping.bindEvents(a,d,e)},format:function(a,c,d){b.grouping.update(a,c,d)},remove:function(a,b,c){b.$table.off("click","tr.group-header").off("pagerChange.tsgrouping").find(".group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove()}})}(jQuery); \ No newline at end of file +!function(a){"use strict";var b=a.tablesorter;b.grouping={types:{number:function(a,c,d,e,f){var g,h;return e>1&&""!==d?c.hasClass(b.css.sortAsc)?(g=Math.floor(parseFloat(d)/e)*e,g>parseFloat(f||0)?g:parseFloat(f||0)):(g=Math.ceil(parseFloat(d)/e)*e,g=e?h[e-1]:d||"")},separator:function(b,c,d,e){var f=(d+"").split(b.widgetOptions.group_separator);return a.trim(f&&e>0&&f.length>=e?f[(e||1)-1]:"")},word:function(a,b,c,d){var e=(c+" ").match(/\w+/g);return e&&e.length>=d?e[d-1]:c||""},letter:function(a,b,c,d){return c?(c+" ").substring(0,d):""},date:function(a,b,c,d,e){var f=a.widgetOptions,g=new Date(c||""),h=g.getHours();return"year"===d?g.getFullYear():"month"===d?f.group_months[g.getMonth()]:"monthyear"===d?f.group_months[g.getMonth()]+" "+g.getFullYear():"day"===d?f.group_months[g.getMonth()]+" "+g.getDate():"week"===d?f.group_week[g.getDay()]:"time"===d?("00"+(h>12?h-12:0===h?h+12:h)).slice(-2)+":"+("00"+g.getMinutes()).slice(-2)+" "+("00"+f.group_time[h>=12?1:0]).slice(-2):f.group_dateString(g)}},update:function(c,d,e){if(!a.isEmptyObject(d.cache)){var f,g,h,i,j,k,l,m,n,o="undefined"!=typeof d.sortList[0],p="",q=0,r=!1,s="undefined"!=typeof e.group_forceColumn[0]?e.group_enforceSort&&!o?-1:e.group_forceColumn[0]:o?d.sortList[0][0]:-1;if(d.$table.find("tr.group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove(),e.group_collapsible&&d.$table.data("pagerSavedHeight",0),s>=0&&s1&&""!==h&&(h+=" - "+(parseInt(h,10)+(parseInt(k[2],10)-1)*(d.$headerIndexed[s].hasClass(b.css.sortAsc)?1:-1))),a.isFunction(e.group_formatter)&&(h=e.group_formatter((h||"").toString(),s,c,d,e)||h),i.eq(f).before(''+(e.group_collapsible?"":"")+''+h+''),e.group_saveGroups&&!r&&e.group_collapsed&&e.group_collapsible&&e.group_currentGroups[e.group_currentGroup].push(h)));d.$table.find("tr.group-header").bind("selectstart",!1).each(function(){var b,d,f,g=a(this),h=g.nextUntil("tr.group-header").filter(":visible");(e.group_count||a.isFunction(e.group_callback))&&(d=g.find(".group-count"),d.length&&(e.group_count&&d.html(e.group_count.replace(/\{num\}/g,h.length)),a.isFunction(e.group_callback)&&e.group_callback(g.find("td"),h,s,c))),e.group_saveGroups&&!a.isEmptyObject(e.group_currentGroups)&&e.group_currentGroups[e.group_currentGroup].length?(f=g.find(".group-name").text().toLowerCase()+g.attr("data-group-index"),b=a.inArray(f,e.group_currentGroups[e.group_currentGroup])>-1,g.toggleClass("collapsed",b),h.toggleClass("group-hidden",b)):e.group_collapsed&&e.group_collapsible&&(g.addClass("collapsed"),h.addClass("group-hidden"))}),d.$table.trigger(e.group_complete)}}},bindEvents:function(c,d,e){e.group_collapsible&&(e.group_currentGroups=[],d.$table.on("click toggleGroup keyup","tr.group-header",function(f){if(f.stopPropagation(),"keyup"!==f.type||13===f.which){var g,h,i,j=a(this),k=j.find(".group-name").text().toLowerCase()+j.attr("data-group-index");!f.shiftKey||"click"!==f.type&&"keyup"!==f.type||j.siblings(".group-header").trigger("toggleGroup"),j.toggleClass("collapsed"),j.nextUntil("tr.group-header").toggleClass("group-hidden",j.hasClass("collapsed")),e.group_saveGroups&&b.storage&&(h=d.$table.find(".group-header"),g=j.hasClass("collapsed"),e.group_currentGroups[e.group_currentGroup]||(e.group_currentGroups[e.group_currentGroup]=[]),g&&e.group_currentGroup?e.group_currentGroups[e.group_currentGroup].push(k):e.group_currentGroup&&(i=a.inArray(k,e.group_currentGroups[e.group_currentGroup]),i>-1&&e.group_currentGroups[e.group_currentGroup].splice(i,1)),b.storage(c,"tablesorter-groups",e.group_currentGroups))}})),a(e.group_saveReset).on("click",function(){b.grouping.clearSavedGroups(c)}),d.$table.on("pagerChange.tsgrouping",function(){b.grouping.update(c,d,e)})},clearSavedGroups:function(a){a&&b.storage&&(b.storage(a,"tablesorter-groups",""),b.grouping.update(a,a.config,a.config.widgetOptions))}},b.addWidget({id:"group",priority:100,options:{group_collapsible:!0,group_collapsed:!1,group_saveGroups:!0,group_saveReset:null,group_count:" ({num})",group_separator:"-",group_formatter:null,group_callback:null,group_complete:"groupingComplete",group_forceColumn:[],group_enforceSort:!0,group_checkbox:["checked","unchecked"],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"],group_dateString:function(a){return a.toLocaleString()}},init:function(a,c,d,e){b.grouping.bindEvents(a,d,e)},format:function(a,c,d){b.grouping.update(a,c,d)},remove:function(a,b,c){b.$table.off("click","tr.group-header").off("pagerChange.tsgrouping").find(".group-hidden").removeClass("group-hidden").end().find("tr.group-header").remove()}})}(jQuery); \ No newline at end of file diff --git a/docs/example-widget-grouping.html b/docs/example-widget-grouping.html index 1a755108..2fb1cff0 100644 --- a/docs/example-widget-grouping.html +++ b/docs/example-widget-grouping.html @@ -86,6 +86,10 @@ tr.group-header.collapsed td i { group_saveReset : '.group_reset', // element to clear saved collapsed groups group_count : " ({num})", // if not false, the "{num}" string is replaced with the number of rows in the group + // apply the grouping widget only to selected column + group_forceColumn : [], // only the first value is used; set as an array for future expansion + group_enforceSort : true, // only apply group_forceColumn when a sort is applied to the table + // checkbox parser text used for checked/unchecked values group_checkbox : [ 'checked', 'unchecked' ], @@ -151,6 +155,12 @@ tr.group-header.collapsed td i {
  • This widget will only work in tablesorter version 2.8+ and jQuery version 1.7+.
  • Please do not use this widget in very large tables (it might be really slow) or when your table includes multiple tbodies.

  • +
  • In v2.23.3 +
      +
    • Added group_forceColumn & group_enforceSort options for force column grouping.
    • +
    • Updated method used to save collapsed groups, so any previously collapsed groups will be ignored after this update.
    • +
    +
  • In v2.22.0, group headers are now keyboard accessible using Tab; and pressing Enter while the header has focus will toggle the group header, or use Shift + Enter to toggle all groups.
  • In v2.21.0
      @@ -301,10 +311,30 @@ group_separator : /[/.]/ + + + [] + + Force the group widget to only apply to one column (v2.23.3). +
      +

      When this option is set, only the first value in the array is used; it is set up as an array for future expansion.

      +

      When set, the set column will be the only column grouped by this widget.

      +

      If the group_enforceSort option is set to false, the column will be grouped without a sort applied to the table.

      +
      + + + + + + true + + Only allow group_forceColumn to work when a sort is applied to the table (v2.23.3). + + - see description) + (see description) Set checkbox parser text (v2.21.0)
      @@ -457,7 +487,7 @@ group_dateString : function(date) { Animals (first letter) Natural Sort (first word) Inputs (second word) - + Date (Full) diff --git a/docs/index.html b/docs/index.html index afbe56ab..bd5346a4 100644 --- a/docs/index.html +++ b/docs/index.html @@ -496,7 +496,7 @@
    • Beta Formatter widget (v2.19.1).
    • Grouping rows widget:
    • diff --git a/js/widgets/widget-grouping.js b/js/widgets/widget-grouping.js index 5d124741..53855012 100644 --- a/js/widgets/widget-grouping.js +++ b/js/widgets/widget-grouping.js @@ -55,9 +55,13 @@ update : function(table, c, wo){ if ($.isEmptyObject(c.cache)) { return; } var rowIndex, tbodyIndex, currentGroup, $rows, groupClass, grouping, norm_rows, saveName, direction, + hasSort = typeof c.sortList[0] !== 'undefined', group = '', + groupIndex = 0, savedGroup = false, - column = c.sortList[0] ? c.sortList[0][0] : -1; + column = typeof wo.group_forceColumn[0] !== 'undefined' ? + ( wo.group_enforceSort && !hasSort ? -1 : wo.group_forceColumn[0] ) : + ( hasSort ? c.sortList[0][0] : -1 ); c.$table .find('tr.group-hidden').removeClass('group-hidden').end() .find('tr.group-header').remove(); @@ -65,7 +69,7 @@ // clear pager saved spacer height (in case the rows are collapsed) c.$table.data('pagerSavedHeight', 0); } - if (column >= 0 && !c.$headerIndexed[column].hasClass('group-false')) { + if (column >= 0 && column < c.columns && !c.$headerIndexed[column].hasClass('group-false')) { wo.group_currentGroup = ''; // save current groups wo.group_currentGroups = {}; @@ -77,10 +81,10 @@ // 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] : ''; + // include direction when saving groups (reversed numbers shows different range values) + direction = 'dir' + c.sortList[0][1]; // combine column, sort direction & grouping as save key - saveName = wo.group_currentGroup = '' + column + direction + grouping.join(''); + saveName = wo.group_currentGroup = '' + c.sortList[0][0] + direction + grouping.join(''); if (!wo.group_currentGroups[saveName]) { wo.group_currentGroups[saveName] = []; } else { @@ -109,9 +113,11 @@ currentGroup = wo.group_formatter((currentGroup || '').toString(), column, table, c, wo) || currentGroup; } $rows.eq(rowIndex).before('' + (wo.group_collapsible ? '' : '') + '' + - currentGroup + ''); + '" unselectable="on" ' + ( c.tabIndex ? 'tabindex="0" ' : '' ) + 'data-group-index="' + + ( groupIndex++ ) + '">' + + ( wo.group_collapsible ? '' : '' ) + + '' + currentGroup + '' + + ''); if (wo.group_saveGroups && !savedGroup && wo.group_collapsed && wo.group_collapsible) { // all groups start collapsed wo.group_currentGroups[wo.group_currentGroup].push(currentGroup); @@ -138,8 +144,8 @@ } } } - if (wo.group_saveGroups && wo.group_currentGroups.length && wo.group_currentGroups[wo.group_currentGroup].length) { - name = $row.find('.group-name').text().toLowerCase(); + if (wo.group_saveGroups && !$.isEmptyObject(wo.group_currentGroups) && wo.group_currentGroups[wo.group_currentGroup].length) { + name = $row.find('.group-name').text().toLowerCase() + $row.attr('data-group-index'); isHidden = $.inArray( name, wo.group_currentGroups[wo.group_currentGroup] ) > -1; $row.toggleClass('collapsed', isHidden); $rows.toggleClass('group-hidden', isHidden); @@ -162,7 +168,7 @@ if (event.type === 'keyup' && event.which !== 13) { return; } var isCollapsed, $groups, indx, $this = $(this), - name = $this.find('.group-name').text().toLowerCase(); + name = $this.find('.group-name').text().toLowerCase() + $this.attr('data-group-index'); // use shift-click to toggle ALL groups if (event.shiftKey && (event.type === 'click' || event.type === 'keyup')) { $this.siblings('.group-header').trigger('toggleGroup'); @@ -220,6 +226,10 @@ 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 + // apply the grouping widget only to selected column + group_forceColumn : [], // only the first value is used; set as an array for future expansion + group_enforceSort : true, // only apply group_forceColumn when a sort is applied to the table + // checkbox parser text used for checked/unchecked values group_checkbox : [ 'checked', 'unchecked' ], // change these default date names based on your language preferences