button().add()
Create a new button and add it to the document.
Please note - this property requires the Buttons extension for DataTables.
Description
This method provides the ability to dynamically add new buttons to a button instance. The inverse of this method is button().remove()
which can be used to remove existing buttons dynamically.
Important: This method will effect the indexes of other buttons in the instances. If you are using index based selectors for the buttons, please ensure that you take this into account for API interactions after calling this method.
When using the buttons()
selector method, the second argument (the buttons selector) is effectively ignored when processing this method. Only the button instances are used.
Type
function button().add( index, config, draw )
- Description:
Create a new button, adding it to the selected button instance and inserting immediately into the document.
- Parameters:
Name Type Optional 1 index
No Insert index for the button. This option can be one of:
- An integer value if the value is to be inserted into the main buttons collection,
- A string with a dash (
-
) separator if the button is to be inserted into a collection (please refer to thebutton-selector
documentation for full details of button indexes, including how they apply to collections of sub-buttons), null
to insert a button at the end of the main buttons collection.
2 config
No Button configuration. Please refer to
buttons.buttons
for full details of the configuration options available here.3 draw
Yes - default:true Since 2.1.0: Indicate if Buttons should immediately draw the new button (
true
) or not (false
). This can be useful when added a large number of buttons, as disabling the draw until the last item can significantly improve performance.- Returns:
New DataTables API instance with the result set containing the newly created button. This means it is possible to immediately using the chaining API to manipulate the button.
Example
Add a new button, into index position 0, that provides the ability to reload an Ajax sourced DataTable:
var table = new DataTable('#myTable');
table.button().add(0, {
action: function (e, dt, button, config) {
dt.ajax.reload();
},
text: 'Reload table'
});