A DataTables JS extension for adding contextual options to one or many selected rows. Ideal if you want synchronized buttons and context-menu actions defined in a single place.
This will alter your DataTables table in the following ways:
- Your actions will be rendered as a row of buttons above your table
- Right-clicking a row will select it and present the user with a context menu of your actions
Both can be individually enabled/disabled.
- DataTables, the world's most comprehensive jQuery-based table component
- Use DataTables in your project
- Include the javascript and css in your project:
<!-- For example, use a CDN: -->
<link rel="stylesheet" href="https://unpkg.com/datatables-contextual-actions@latest/dist/dataTables.contextualActions.min.css"/>
<script src="https://unpkg.com/datatables-contextual-actions@latest/dist/dataTables.contextualActions.min.js"></script>
- Initialize with a configuration (see the
Configuration
section below)
$(document).ready(function () {
// Set up our table in standard DataTables fashion (with selection enabled)
var myTable = $('#dt').DataTable({
select: {
style: 'os',
selector: 'td:first-child',
},
});
// And initialize our plugin.
myTable.contextualActions({
// Configuration options as described above
});
});
Property | Type | Description | Default |
---|---|---|---|
classes | string[] | CSS classes to apply to both the dropdown-menu and the button container | [] |
iconPrefix | string | CSS class that icons will all start with. For example, providing 'fas' will default an icon to<i class="fas"></i> ...before it's assigned its actual icon class. |
'' |
contextMenu | contextMenu (see below) | required | |
showConfirmationMethod | function(confirmation) | A function taking in a confirmation object (see the documentation on item 's confirmation attribute) that handles how to render and show the confirmation to the user. By default this uses the super basic window.confirm but the incoming confirmation parameter matches the config of BootBox, a plugin for showing confirmations using Bootstrap's modals. If using Bootbox, replace with function(confirmation){bootbox.confirm(confirmation)} |
function(confirmation){ confirmation.callback( window.confirm( confirmation.message)); } |
buttonList | buttonList (see below) | required | |
items | item[] (see below) | An array of item objects that represent the options that will be both rendered as buttons, and as options in a context menu. |
required |
deselectAfterAction | bool | Whether or not to deselect all table rows after an item's action takes place. Note: if your action's code clears and redraws the table anyway, the selection state will be lost regardless of this option. Modify row data in the table itself using the DataTables API if you don't want to accidentally lose the selection state. | true |
Property | Type | Description | Default |
---|---|---|---|
enabled | bool | Whether or not to display a context menu when the user right-clicks a row | true |
isMulti | bool | If true, multiple rows can be selected and then acted upon by right-clicking. If false, right-clicking will de-select all other rows before showing the context menu (default behaviour) | false |
xoffset | int | The horizontal distance away, in pixels, to render the drop-down context menu from the mouse | -10 |
yoffset | int | The vertical distance away, in pixels, to render the drop-down context menu from the mouse | -10 |
showSpeed | string | A CSS duration describing how quickly the context menu should displayed | '0.30s' |
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 |
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 |
Property | Type | Description | Default |
---|---|---|---|
enabled | bool | Whether or not to render the options out into an external container as a series of button groups | true |
containerSelector | string | The CSS selector of the container element that the buttons will be rendered into. For example, '#my-button-bar' |
required |
iconOnly | bool | Whether or not to only display icons in the buttons. If true , buttons will only contain icons, and the option titles are turned into tooltips. |
true |
disabledOpacity | decimal | The opacity of a disabled button or context menu icon | 0.5 |
dividerSpacing | int | The number of pixels between divided groups of buttons | 10 |
groupClass | string | The class to give to the groups of buttons in the button list. Groups are the groupings of buttons separated with divider typed items. For example, with bootstrap, use btn-group |
`` |
Property | Type | Description | Default |
---|---|---|---|
type | string | Items can be of the following types:
|
required |
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:
|
'' |
multiTitle | string | The title (above) to render when more than 1 rows are selected |
'' |
iconClass | string | The class of the <i></i> styled icon to render.For example, if iconPrefix is 'fa fa-fw' and iconClass is 'fa-eye' , then <i class="fa fa-fw fa-eye"></i> 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) |
[] |
buttonClasses | string[] | An array of CSS classes to add onto the rendered item (ONLY the button version) | [] |
id | string | Optionally you may assign an id to the item's rendered element if you wish to target it with any custom code later on | '' |
confirmation | object or function(rows) |
The confirmation configuration object. Include a title and a message that will appear in the default browser confirm dialogueOR you may provide a confirmation object that matches that of Bootbox.js, and then override the default confirmation behavior by passing bootbox.confirm to options.showConfirmationMethod Example (default behavior): { title:'Foo' ,message:'Are you sure you want to Bar?' } Example (if using Bootbox): { title: 'Delete Item(s)', message: 'Do you want to delete the item(s)?', buttons: { cancel: { className: 'btn-link', label: 'Cancel' }, confirm: { className: 'btn-danger', label: 'Delete' } } |
{} |
action | function(rows) | The action to execute against the 1 or more rows selected when the action was executed |
required |
isDisabled | bool or function(row) |
Whether or not to totally disable the option. If a function of row is provided, this becomes a test to run against every selected row.If ANY of the rows pass this test, the option will be disabled For example, to disable the button for "John" rows: (row) => row.FirstName === 'John' |
{} |
isDisabledStrictMode | bool | Modifies the behavior of isDisabled (see above). If specified and is true , then rather than allowing a multi -enabled button to be clicked when ANY selected row is enabled/applicable, ONLY enable the option if ALL selected rows are enabled/applicable. isDisabledStrictMode ensures that ALL selected rows fail the isDisabled check (in other words, are ALL enabled) before allowing an action to be confirmed or executed |
false |
isHidden | bool or function(row) |
Similar to the above isDisabled but renders an option hidden/invisible instead of just being disabled (greyed out) |
{} |
Please see the example page referred to above for a demo of how to use all these options together.
If you change underlying data that some renderers rely on (isDisabled
on a static
-typed action, for example), and want to update the controls without having the user manually select/deselect rows, you can force-update contextualActions like so:
$(document).ready(function () {
var myTable = $('#dt').DataTable({
// ...
});
// And initialize our plugin.
var myContextActions = myTable.contextualActions({
// ...
});
// Manually refresh the control and force all actions to be re-evaluated
myContextActions.update();
});
- Launch the included VSCode workspace file (
datatables-contextual-actions.code-workspace
) - Run
npm i
to install any dependencies - Perform any development and test in the
~/index.html
file - When you're ready to build, execute the default build task (
Ctrl + Shift + B
) - Test the built changes in the
~/docs/index.html
documentation/demo file