Making rows not editable with multi-row edit enabled?
Making rows not editable with multi-row edit enabled?
I have the need to prevent some rows from being edited in the DataTables editor. Currently I have a solution implemented that was previously discussed here in another thread. The solutions uses preOpen()
to check the contents of the selected row for a condition. This works perfectly when you only select one row to edit.
The issue I'm having is that when multi-row editing is enabled, only the first row selected is checked for the condition. If a row that is not meant to be editable is selected after a row that can be edited is selected, then both rows will be edited.
I don't have much experience with the APIs used in the solution such as modifier()
so I am not familiar with how they work. How can the code below be modified to check every row that is selected for editing before opening or locking the form?
editor.on('preOpen', function (e) {
var modifier = editor.modifier(); // Gets the selected row(s) of the table
if (modifier) {
// Get data for this row
var data = table.row(modifier).data();
if (data.not_editable == 1) {
// Disable the form
editor.disable();
// ...
} else {
// Enable the form
editor.enable();
// ...
}
}
});
Replies
The problem here is that you are using the singular
row()
method with the modifier. So even if the modifier matches multiple rows, it will always get truncated to a single row. The pluralrows()
method will resolve to multiple rows though. You can then userows().every()
to loop over them and check the data for each.Allan
Ah of course! Thanks for the reply Allan, that looks like it will do the trick.
Will comment here if there is any further issues. Cheers.
Glad to report it's working! Here's the code I used for future readers: