How do I prevent the Remove button from enabling only for certain rows based on row content?

How do I prevent the Remove button from enabling only for certain rows based on row content?

J BrownJ Brown Posts: 4Questions: 2Answers: 1

I am using the Editor extension with Edit, Remove, and New buttons. When one or more rows are selected, I want to always Edit but not necessarily Remove the selected rows, based on a column value. I think I need to override an event a row is selected, examine the row content to see whether or not to enable the Remove button---something along those lines. But I also want to do the default behavior when a row is selected (i.e. enable the Edit button, highlight the row, and whatever else may be happening).

This question has an accepted answers - jump to answer

Answers

  • J BrownJ Brown Posts: 4Questions: 2Answers: 1
    Answer ✓

    I figured out a solution for myself. Including a code snippet in case it helps someone else:

                            action: function (e, dt) {
                                let selected_rows = dt.rows({selected:true}).data();
                                for (let sr=0; sr < selected_rows.length; sr++){
                                    if (selected_rows[sr].system){
                                        this.disable();
                                        bootbox.alert("Sorry, but you are not allowed to delete any System rule file entries.");
                                        return;
                                    }
    
                                }
                                let rows = selected_rows.map(function(rowData) {
                                    return rowData.rule_file;
                                });
    
                                let message_text = 'Are you sure you want to delete the rule file entries for the '+
                                    'following rows(s)? <ul><li>'+rows.join('</li><li>')+'</li></ul>';
    
                                editor
                                    .title("Delete selected rows")
                                    .message(message_text)
                                    .buttons( {
                                        label: "Delete",
                                        fn: function() {this.submit();}
                                    })
                                    .remove(dt.rows({selected:true}).data());
                            },
    
                        },
    
    
  • allanallan Posts: 54,881Questions: 1Answers: 8,601 Site admin

    Thanks for posting back - that looks just about perfect. The only change I would suggest is to add a listener for the select event which will perform the logic check required and either enable or disable the button as needed.

    This is how Select's built in buttons use the init option to add a suitable listener.

    Allan

This discussion has been closed.