/* A centralized container of the table data. You could hold the
* row-specific data in a data-whatever-info="" attribute in each
* row, you decide what fetchElementData() does!
*/
var demo3Rows = {
'1': { name: 'First row', description: 'Lorem ipsum dolor sit amet' },
'2': { name: 'Second row', description: 'Nemo enim ipsam voluptatem quia voluptas' },
'3': { name: 'Third row', description: 'Ut enim ad minima veniam' }
};
var menu = new BootstrapMenu('.demo3TableRow', {
/* a function to know which row was the context menu opened on,
* given the selected DOM element. When this function is defined,
* every user-defined action callback receives its return value as
* an argument. */
fetchElementData: function($rowElem) {
var rowId = $rowElem.data('rowId');
return demo3Rows[rowId];
},
actions: [{
name: 'Edit name',
onClick: function(row) {
toastr.info("'Edit name' clicked on '" + row.name + "'");
}
}, {
name: 'Edit description',
onClick: function(row) {
toastr.info("'Edit description' clicked on '" + row.name + "'");
}
}]
});
Demo 4
You can give every action an id and group them in the dropdown with the actionsGroups option. You can also use isShown and isEnabled callbacks in an action to control when it should not appear, or appear disabled.
Font Awesome icons can be used adding the iconClass property to an action, with the class name of the icon to use.
var demo4Rows = {
'1': { name: 'First row', isEditable: true, isRemovable: true },
'2': { name: 'Second row', isEditable: false, isRemovable: true },
'3': { name: 'Third row', isEditable: true, isRemovable: false }
};
var menu = new BootstrapMenu('.demo4TableRow', {
fetchElementData: function($rowElem) {
var rowId = $rowElem.data('rowId');
return demo4Rows[rowId];
},
/* group actions by their id to make use of separators between
* them in the context menu. Actions not added to any group with
* this option will appear in a default group of their own. */
actionsGroups: [
['setEditable', 'setUneditable' ],
['deleteRow']
],
/* you can declare 'actions' as an object instead of an array,
* and its keys will be used as action ids. */
actions: {
editName: {
name: 'Edit name',
iconClass: 'fa-pencil',
onClick: function(row) {
toastr.info("'Edit name' clicked on '" + row.name + "'");
},
isEnabled: function(row) {
return row.isEditable;
}
},
editDescription: {
name: 'Edit description',
iconClass: 'fa-pencil',
onClick: function(row) {
toastr.info("'Edit description' clicked on '" + row.name + "'");
},
isEnabled: function(row) {
return row.isEditable;
}
},
setEditable: {
name: 'Set editable',
iconClass: 'fa-unlock',
onClick: function(row) {
toastr.info("'Set editable' clicked on '" + row.name + "'");
},
isShown: function(row) {
return !row.isEditable;
}
},
setUneditable: {
name: 'Set uneditable',
iconClass: 'fa-lock',
onClick: function(row) {
toastr.info("'Set uneditable' clicked on '" + row.name + "'");
},
isShown: function(row) {
return row.isEditable;
}
},
deleteRow: {
name: 'Delete row',
iconClass: 'fa-trash-o',
onClick: function(row) {
toastr.info("'Delete row' clicked on '" + row.name + "'");
},
isEnabled: function(row) {
return row.isEditable && row.isRemovable;
}
}
}
});
Demo 5
Classes can also be added easily to an action by setting a classNames property, which will be parsed through the classnames utility. This property can be a string, an object or a function that receives the element's fetched data as a first argument.
In the same way, an action's name can be defined as a function. In that case, it will be called before
each render of the menu, receiving as the first argument the data returned by fetchElementData.