In this article, we’ll show you how to work with buttons for List or Library control, adding new buttons, removing or modifying existing ones.
Note
For a practical example of how you can customize List or Library buttons, check out Duplicate item button for List or Library article.
The following code allows you to add a button to the left side of the toolbar, select an icon and run custom code on click:
fd.spRendered(function() {
//new button
var button = {
text: 'Quick action',
class: 'btn-secondary',
visible: true,
icon: 'LightningBolt',
iconType: 0,
click: function() {
alert('Button clicked!');
}
}
fd.control('SPDataTable1').ready(function(dt) {
//dt parameter is the same as fd.control('SPDataTable1')
dt.buttons.push(button);
});
});
The following code allows you to add a button to the right side of the toolbar:
fd.spRendered(function() {
//new button
var button = {
text: '',
class: 'btn-secondary',
visible: true,
icon: 'Blocked',
iconType: 0,
location: 1,
click: function() {
alert('Button clicked!');
}
}
fd.control('SPDataTable1').ready(function(dt) {
//dt parameter is the same as fd.control('SPDataTable1')
dt.buttons.push(button);
});
});
The following samples will allow you to hide, change text or icon for the existing buttons.
You can hide any button, for example, New button, with the following code:
fd.spRendered(function() {
fd.control('SPDataTable1').ready(function(dt) {
//dt parameter is the same as fd.control('SPDataTable1')
dt.buttons[0].visible = false;
});
});
Change the text on the Delete button:
fd.spRendered(function() {
fd.control('SPDataTable1').ready(function(dt) {
//dt parameter is the same as fd.control('SPDataTable1')
dt.buttons[1].text = 'Remove';
});
});
Swap the icon for the Delete button (the icons are taken from Office UI Fabric Icons):
fd.spRendered(function() {
fd.control('SPDataTable1').ready(function(dt) {
//dt parameter is the same as fd.control('SPDataTable1')
dt.buttons[1].icon = 'Broom';
});
});