From 5eac6e251601efc2bdaba87b589904c6702a481d Mon Sep 17 00:00:00 2001 From: danildenha Date: Mon, 2 Jun 2025 13:23:21 -0500 Subject: [PATCH 1/2] optionally diable counter on contextual menu --- dist/dataTables.contextualActions.min.js | 2 +- src/dataTables.contextualActions.js | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/dist/dataTables.contextualActions.min.js b/dist/dataTables.contextualActions.min.js index d2bff44..edfe772 100644 --- a/dist/dataTables.contextualActions.min.js +++ b/dist/dataTables.contextualActions.min.js @@ -1 +1 @@ -jQuery.fn.dataTable.Api.register("contextualActions()",function(u){var e=this.table();const h={DIVIDER:"divider",OPTION:"option",STATIC:"static"},b={isDisabled:function(t,e){return t.type!==h.STATIC&&0===e.length||void 0!==t.multi&&!1===t.multi&&1!t.isDisabled(e)).length||"boolean"==typeof t.isDisabledStrictMode&&!0===t.isDisabledStrictMode&&e.some(e=>t.isDisabled(e)))||t.type===h.STATIC&&"function"==typeof t.isDisabled&&t.isDisabled()}};if((u=function e(t,...i){if(!i.length)return t;var n=i.shift();if(s(t)&&s(n))for(const o in n)s(n[o])?(t[o]||Object.assign(t,{[o]:{}}),e(t[o],n[o])):Object.assign(t,{[o]:n[o]});return e(t,...i)}({contextMenu:{enabled:!0,isMulti:!1,xoffset:-10,yoffset:-10,showSpeed:"0.30s",headerRenderer:"",headerIsFollowedByDivider:!1,showStaticOptions:!1,startsNewSelection:!1},buttonList:{enabled:!0,groupClass:"btn-group",iconOnly:!1,disabledOpacity:.5,dividerSpacing:10},classes:[],iconPrefix:"",items:[],deselectAfterAction:!0,showConfirmationMethod:function(e){e.callback(window.confirm(e.message))}},u=null==u?{}:u)).buttonList.enabled&&void 0===u.buttonList.containerSelector)throw"The buttonList.containerSelector option must be specified if the buttonList is enabled, to specify where the buttons will be created";var y={dt:null,table:null,contextMenuId:"",rightClickedRowData:"",init:function(o){this.dt=e,this.table=$(this.dt.container()).find("table"),$(window).click(function(e){$("#"+y.contextMenuId).is(":visible")&&!$(e.target).closest(".dropdown-menu").length&&i()}),this.contextMenuId=(this.table instanceof jQuery?this.table.attr("id"):this.table.id)+"-context-menu",this.rightClickedRowData=[];var s=this;$(this.table).on("contextmenu","tr",function(e){var t=()=>y.table.DataTable().rows().deselect();o.contextMenu.isMulti?s.dt.rows({selected:!0}).indexes().toArray().includes(y.dt.row(this).index())||o.contextMenu.startsNewSelection&&t():t(),s.dt.row(this).select();t=s.table.DataTable().rows({selected:!0}).toArray()[0],t=s.table.DataTable().rows(t).data().toArray();if(o.contextMenu.enabled&&void 0!==t)return i(),s.rightClickedRowData=t,function(t,e){!function(a,e,d,c,l){var r=$(''):(t.type===h.OPTION||u.contextMenu.showStaticOptions&&t.type===h.STATIC)&&(i="",void 0!==t.iconClass&&""!==t.icon&&(i=''),n=void 0!==t.contextMenuClasses?t.contextMenuClasses.join(" "):"",o=void 0!==t.classes?t.classes.join(" "):"",e=void 0!==t.multiTitle&&1'+i+e+""),b.isDisabled(t,l)&&$(s).addClass("disabled").css("opacity","0.5"),1!t.isDisabled(e)):l).length,$(s).append(" ("+e+")")),void 0!==t.confirmation?$(s).click(function(){v(t,l),p(a)}):$(s).click(function(){var e=l.filter(e=>void 0===t.isDisabled||!t.isDisabled(e));p(a),t.action(e,$(s)),u.deselectAfterAction?y.dt.rows().deselect():y.update()}),r.append(s)))}),r.children(":last-child.dropdown-divider").remove(),r.children().each(function(){$(this).hasClass("dropdown-divider")&&$(this).prev().hasClass("dropdown-divider")&&$(this).remove()}),$("body").append(r)}(y.contextMenuId,u.classes,u.iconPrefix,u.items,y.rightClickedRowData);var o=e+u.contextMenu.yoffset,s=t+u.contextMenu.xoffset;if(!1!==u.contextMenu.headerRenderer){t="";try{"string"==typeof u.contextMenu.headerRenderer?t=u.contextMenu.headerRenderer:"function"==typeof u.contextMenu.headerRenderer&&(t=u.contextMenu.headerRenderer(y.rightClickedRowData))}catch(e){t=""}$("#"+y.contextMenuId).find(".dropdown-header").html(t)}setTimeout(function(){$("#"+y.contextMenuId).css({display:"block",visibility:"visible",opacity:0,top:o,left:s,transition:u.contextMenu.showSpeed+" ease all, left 0s, top 0s","z-index":99999}),$("#"+y.contextMenuId).css({opacity:1});var e=$("#"+y.contextMenuId).get(0).getBoundingClientRect(),t=window.innerHeight,i=document.body.clientWidth,n=!1;e.bottom>t&&(o-=e.bottom-t,n=!0),e.right>i&&(s-=e.right-i,n=!0),n&&$("#"+y.contextMenuId).css({top:o-20,left:s-20})},1)}(e.pageX,e.pageY),!1}),this.dt.on("select",function(e,t,i,n){"row"===i&&(i=t.rows({selected:!0}).toArray()[0],i=s.table.DataTable().rows(i).data().toArray(),a(0,o,i))}),this.dt.on("deselect",function(e,t,i,n){"row"===i&&(i=t.rows({selected:!0}).toArray()[0],i=s.table.DataTable().rows(i).data().toArray(),a(0,o,i))}),a(this.dt,o,[])},update:function(){var e=this.dt.rows({selected:!0}).data().toArray();a(this.dt,u,e)}};function i(){$("#"+y.contextMenuId).removeClass("show").hide(),y.rightClickedRowData=[],p(y.contextMenuId)}function a(e,t,i){var n,o,s;t.buttonList.enabled&&(n=t.buttonList,o=t.classes,s=t.items,t=t.iconPrefix,i=i,function(e,t){$.each(t,function(e,t){$(u.containerSelector).removeClass(t)}),$(e).empty()}(n.containerSelector,o),function(d,e,c,l,r){var u='
',p=[],f=null;$.each(c,function(e,t){if(null===f)f=$(u);else{if(0===e&&t.type===h.DIVIDER)return;if(0t.isHidden(e)))||t.type!==h.OPTION&&t.type!==h.STATIC||(s="",o=d.iconOnly?"":"margin-right:10px;",void 0!==t.iconClass&&""!==t.icon&&(s=''),i=void 0!==t.buttonClasses?t.buttonClasses.join(" "):"",n=void 0!==t.classes?t.classes.join(" "):"",e="",o=void 0!==t.multiTitle&&1!t.isDisabled(e)):r).length,d.iconOnly?t.type!==h.STATIC&&1'+e+""),void 0!==t.id&&$(a).attr("id",t.id),(e=b.isDisabled(t,r))?($(a).addClass("disabled"),$(a).attr("disabled","disabled"),$(a).css("cursor","not-allowed"),$(a).css("opacity",d.disabledOpacity),$(a).removeAttr("title","")):!d.iconOnly&&t.type!==h.STATIC&&1void 0===t.isDisabled||!t.isDisabled(e));t.action(e,$(a)),d.deselectAfterAction?y.dt.rows().deselect():y.update()})),f.append(a))}),p.push(f),$.each(e,function(e,t){$(d.containerSelector).addClass(t)}),$.each(p,function(e,t){0void 0===t.isDisabled||!t.isDisabled(e)),e=t.confirmation(n);e.callback=function(e){e&&(t.action(n,i),u.deselectAfterAction?y.dt.rows().deselect():y.update())},u.showConfirmationMethod(e)}function s(e){return e&&"object"==typeof e&&!Array.isArray(e)}return y.init(u),y}); \ No newline at end of file +jQuery.fn.dataTable.Api.register("contextualActions()",function(u){var e=this.table();const h={DIVIDER:"divider",OPTION:"option",STATIC:"static"},b={isDisabled:function(t,e){return t.type!==h.STATIC&&0===e.length||void 0!==t.multi&&!1===t.multi&&1!t.isDisabled(e)).length||"boolean"==typeof t.isDisabledStrictMode&&!0===t.isDisabledStrictMode&&e.some(e=>t.isDisabled(e)))||t.type===h.STATIC&&"function"==typeof t.isDisabled&&t.isDisabled()}};if((u=function e(t,...i){if(!i.length)return t;var n=i.shift();if(s(t)&&s(n))for(const o in n)s(n[o])?(t[o]||Object.assign(t,{[o]:{}}),e(t[o],n[o])):Object.assign(t,{[o]:n[o]});return e(t,...i)}({contextMenu:{enabled:!0,isMulti:!1,xoffset:-10,yoffset:-10,showSpeed:"0.30s",headerRenderer:"",headerIsFollowedByDivider:!1,showStaticOptions:!1,startsNewSelection:!1},buttonList:{enabled:!0,groupClass:"btn-group",iconOnly:!1,disabledOpacity:.5,dividerSpacing:10},classes:[],iconPrefix:"",items:[],deselectAfterAction:!0,showConfirmationMethod:function(e){e.callback(window.confirm(e.message))}},u=null==u?{}:u)).buttonList.enabled&&void 0===u.buttonList.containerSelector)throw"The buttonList.containerSelector option must be specified if the buttonList is enabled, to specify where the buttons will be created";var y={dt:null,table:null,contextMenuId:"",rightClickedRowData:"",init:function(o){this.dt=e,this.table=$(this.dt.container()).find("table"),$(window).click(function(e){$("#"+y.contextMenuId).is(":visible")&&!$(e.target).closest(".dropdown-menu").length&&i()}),this.contextMenuId=(this.table instanceof jQuery?this.table.attr("id"):this.table.id)+"-context-menu",this.rightClickedRowData=[];var s=this;$(this.table).on("contextmenu","tr",function(e){var t=()=>y.table.DataTable().rows().deselect();o.contextMenu.isMulti?s.dt.rows({selected:!0}).indexes().toArray().includes(y.dt.row(this).index())||o.contextMenu.startsNewSelection&&t():t(),s.dt.row(this).select();t=s.table.DataTable().rows({selected:!0}).toArray()[0],t=s.table.DataTable().rows(t).data().toArray();if(o.contextMenu.enabled&&void 0!==t)return i(),s.rightClickedRowData=t,function(t,e){!function(d,e,a,c,l){var r=$(''):(t.type===h.OPTION||u.contextMenu.showStaticOptions&&t.type===h.STATIC)&&(i="",void 0!==t.iconClass&&""!==t.icon&&(i=''),n=void 0!==t.contextMenuClasses?t.contextMenuClasses.join(" "):"",o=void 0!==t.classes?t.classes.join(" "):"",e=void 0!==t.multiTitle&&1'+i+e+""),b.isDisabled(t,l)&&$(s).addClass("disabled").css("opacity","0.5"),1!t.isDisabled(e)):l).length,!1!==t.showAffectedItemCount&&$(s).append(" ("+e+")")),void 0!==t.confirmation?$(s).click(function(){v(t,l),f(d)}):$(s).click(function(){var e=l.filter(e=>void 0===t.isDisabled||!t.isDisabled(e));f(d),t.action(e,$(s)),u.deselectAfterAction?y.dt.rows().deselect():y.update()}),r.append(s)))}),r.children(":last-child.dropdown-divider").remove(),r.children().each(function(){$(this).hasClass("dropdown-divider")&&$(this).prev().hasClass("dropdown-divider")&&$(this).remove()}),$("body").append(r)}(y.contextMenuId,u.classes,u.iconPrefix,u.items,y.rightClickedRowData);var o=e+u.contextMenu.yoffset,s=t+u.contextMenu.xoffset;if(!1!==u.contextMenu.headerRenderer){t="";try{"string"==typeof u.contextMenu.headerRenderer?t=u.contextMenu.headerRenderer:"function"==typeof u.contextMenu.headerRenderer&&(t=u.contextMenu.headerRenderer(y.rightClickedRowData))}catch(e){t=""}$("#"+y.contextMenuId).find(".dropdown-header").html(t)}setTimeout(function(){$("#"+y.contextMenuId).css({display:"block",visibility:"visible",opacity:0,top:o,left:s,transition:u.contextMenu.showSpeed+" ease all, left 0s, top 0s","z-index":99999}),$("#"+y.contextMenuId).css({opacity:1});var e=$("#"+y.contextMenuId).get(0).getBoundingClientRect(),t=window.innerHeight,i=document.body.clientWidth,n=!1;e.bottom>t&&(o-=e.bottom-t,n=!0),e.right>i&&(s-=e.right-i,n=!0),n&&$("#"+y.contextMenuId).css({top:o-20,left:s-20})},1)}(e.pageX,e.pageY),!1}),this.dt.on("select",function(e,t,i,n){"row"===i&&(i=t.rows({selected:!0}).toArray()[0],i=s.table.DataTable().rows(i).data().toArray(),d(0,o,i))}),this.dt.on("deselect",function(e,t,i,n){"row"===i&&(i=t.rows({selected:!0}).toArray()[0],i=s.table.DataTable().rows(i).data().toArray(),d(0,o,i))}),d(this.dt,o,[])},update:function(){var e=this.dt.rows({selected:!0}).data().toArray();d(this.dt,u,e)}};function i(){$("#"+y.contextMenuId).removeClass("show").hide(),y.rightClickedRowData=[],f(y.contextMenuId)}function d(e,t,i){var n,o,s;t.buttonList.enabled&&(n=t.buttonList,o=t.classes,s=t.items,t=t.iconPrefix,i=i,function(e,t){$.each(t,function(e,t){$(u.containerSelector).removeClass(t)}),$(e).empty()}(n.containerSelector,o),function(a,e,c,l,r){var u='
',f=[],p=null;$.each(c,function(e,t){if(null===p)p=$(u);else{if(0===e&&t.type===h.DIVIDER)return;if(0t.isHidden(e)))||t.type!==h.OPTION&&t.type!==h.STATIC||(s="",o=a.iconOnly?"":"margin-right:10px;",void 0!==t.iconClass&&""!==t.icon&&(s=''),i=void 0!==t.buttonClasses?t.buttonClasses.join(" "):"",n=void 0!==t.classes?t.classes.join(" "):"",e="",o=void 0!==t.multiTitle&&1!t.isDisabled(e)):r).length,a.iconOnly?t.type!==h.STATIC&&1'+e+""),void 0!==t.id&&$(d).attr("id",t.id),(e=b.isDisabled(t,r))?($(d).addClass("disabled"),$(d).attr("disabled","disabled"),$(d).css("cursor","not-allowed"),$(d).css("opacity",a.disabledOpacity),$(d).removeAttr("title","")):!a.iconOnly&&t.type!==h.STATIC&&1void 0===t.isDisabled||!t.isDisabled(e));t.action(e,$(d)),a.deselectAfterAction?y.dt.rows().deselect():y.update()})),p.append(d))}),f.push(p),$.each(e,function(e,t){$(a.containerSelector).addClass(t)}),$.each(f,function(e,t){0void 0===t.isDisabled||!t.isDisabled(e)),e=t.confirmation(n);e.callback=function(e){e&&(t.action(n,i),u.deselectAfterAction?y.dt.rows().deselect():y.update())},u.showConfirmationMethod(e)}function s(e){return e&&"object"==typeof e&&!Array.isArray(e)}return y.init(u),y}); \ No newline at end of file diff --git a/src/dataTables.contextualActions.js b/src/dataTables.contextualActions.js index 83c8122..e259110 100644 --- a/src/dataTables.contextualActions.js +++ b/src/dataTables.contextualActions.js @@ -468,7 +468,9 @@ jQuery.fn.dataTable.Api.register('contextualActions()', function (options) { typeof item.isDisabled === 'function' ? rows.filter((row) => !item.isDisabled(row)).length : rows.length; - $(itemElement).append(' (' + affectedItemCount + ')'); + if (item.showAffectedItemCount !== false) { + $(itemElement).append(' (' + affectedItemCount + ')'); + } } // On click, perform the action @@ -632,7 +634,7 @@ jQuery.fn.dataTable.Api.register('contextualActions()', function (options) { // Of it's just an icon, it needs a tooltip title title = title; - if (item.type !== ITEMTYPE.STATIC && rows.length > 1) { + if (item.type !== ITEMTYPE.STATIC && rows.length > 1 && item.showAffectedItemCount !== false) { // Append the count if there's multiple rows selected and we're not a static button title += ' (' + affectedItemCount + ')'; } @@ -671,8 +673,10 @@ jQuery.fn.dataTable.Api.register('contextualActions()', function (options) { item.type !== ITEMTYPE.STATIC && rows.length > 1 ) { - // Add the count of selected rows to the title, if the user doesn't want to just see the icon - $(itemElement).append(' (' + affectedItemCount + ')'); + if (item.showAffectedItemCount !== false) { + // Add the count of selected rows to the title, if the user doesn't want to just see the icon + $(itemElement).append(' (' + affectedItemCount + ')'); + } } // If we're icon- only force immediate and obvious tooltips From 359fc7836dd6833e2ca2efd1a042b9983859f1b3 Mon Sep 17 00:00:00 2001 From: danildenha Date: Mon, 2 Jun 2025 13:41:30 -0500 Subject: [PATCH 2/2] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9b628e5..d570f5a 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ $(document).ready(function () { | **headerRenderer** | string
*or*
function(rows)
*or*
false (bool) | What to display as the context menu's header.

Can be a static string or a function of the rows selected.

Set to `false` to skip rendering a header. | `''` | | **headerIsFollowedByDivider** | bool | Controls if a divider is automatically added next to the header. (Ignored, if header is set to `false`.) | `false` | | **showStaticOptions** | bool | Whether or not to display `static` items in the context menu | `false` | +| **showAffectedItemCount** | bool | Whether or not to display the count of affected items next to an action's title when multiple rows are selected. This can be overridden on a per-item basis. | `true` | | **startsNewSelection** | bool | Whether or not to start a new selection (deselect other rows) when `isMulti` is `true` and a non-selected row is right-clicked | `false` | ## options.buttonList @@ -98,6 +99,7 @@ $(document).ready(function () { | **multi** | bool | Whether or not to enable this button when more than 1 rows are selected | `false` | | **title** | string |
What the option is named.
The title is rendered as:

  • In buttons: the button text

  • In buttons when `iconOnly` is `true`: the button's tooltip


  • In context menus: the dropdown option's text


    • | '' | | **multiTitle** | string | The `title` (above) to render when more than 1 rows are selected | '' | +| **showAffectedItemCount** | bool | Overrides the global `contextMenu.showAffectedItemCount` setting for this specific item. Set to `true` to show the count, or `false` to hide it. If undefined, the global setting is used. | (inherits from `contextMenu.showAffectedItemCount`) | | **iconClass** | string | The class of the `` styled icon to render.
      For example, if `iconPrefix` is `'fa fa-fw'` and `iconClass` is `'fa-eye'`, then `` is rendered.

      Leave blank to render no icon. | `''` | | **classes** | string[] | An array of CSS classes to add onto the rendered item (either the button or the
      dropdown option) | `[]` | | **contextMenuClasses** | string[] | An array of CSS classes to add onto the rendered item (ONLY the
      dropdown version) | `[]` |