Making rows not editable with multi-row edit enabled?

Making rows not editable with multi-row edit enabled?

jsmith.kpmgjsmith.kpmg Posts: 3Questions: 0Answers: 0

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

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    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 plural rows() method will resolve to multiple rows though. You can then use rows().every() to loop over them and check the data for each.

    Allan

  • jsmith.kpmgjsmith.kpmg Posts: 3Questions: 0Answers: 0

    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.

  • jsmith.kpmgjsmith.kpmg Posts: 3Questions: 0Answers: 0

    Glad to report it's working! Here's the code I used for future readers:

    editor.on('preOpen', function (e) {
        var modifier = editor.modifier();  // Gets the selected row(s) of the table
        if (modifier) {
            // Variable to flag if the entire selection of rows is editable
            var editable = true;                  
            // Iterate through all selected rows
            table.rows(modifier).every( function ( rowIdx, tableLoop, rowLoop ) {
                // Get the data for each row
                var data = this.data();                        
                if (data.not_editable == 1) {
                    // Set flag to false and exit the function
                    editable = false;
                    return;
                }
            });
                        
            if (editable == false) {
                // Disable the form
                editor.disable();
                // ...
            } else {
                // Enable the form
                editor.enable();
                // ...
            }
        }
    });
    
This discussion has been closed.