Enable button only if row is selected and a column has a defined value

Enable button only if row is selected and a column has a defined value

dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

Hello,
i'm using datatables editor with the buttons 'new', 'edit' and 'delete'. The button 'new' is enabled all the time and the buttons 'edit' and 'delete' are only enabled if a row is selected. This all works fine.

My problem:
I have the column 'active' in my table. It contains the value 0 or 1, which represent the status 'not active' and 'active'. Now i want to extend the function of the 'delete'-button, that it will only be enable if a row is selected AND the value in the column 'active' is 0. If the 'active'-value is 1, the 'delete'-button can't be enabled.
How can i do this?

Thanks a lot for your help.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,021Questions: 88Answers: 421
    edited November 2018 Answer ✓

    Here is an example.

    These are my buttons. You need to give the remove button a name to refer to.

    var rateTable = $('#tblRate').DataTable( {
    ...............
    columns: [
            {   data: "rate.ref_rate" },
            {   data: "rate.currency" },
            {   data: "rate.ref_rate_period" },
            {   data: "rate.date" },
            {   data: "rate.rate" },
            {   data: "rate.update_time" }
        ],
    .............
    select: {
            style: 'multi'
        },            
        buttons: [
            {   extend: "create", editor: rateEditor },
            {   extend: "edit",   editor: rateEditor },
            {   extend: "remove", editor: rateEditor, name: "remove" },
                        "colvis"
        ]
    } );
    

    And here are the select and deselect event handlers.

    rateTable
        .on('select', function () {
            rateTable.rows({selected: true}).every( function ( rowIdx, tableLoop, rowLoop ) {
                var data = this.data();
                if ( data.rate.ref_rate_period == '1month') {
                    rateTable.button( 'remove:name' ).disable();
                }
            });
        })
        .on('deselect', function () {
            rateTable.rows({selected: true}).every( function ( rowIdx, tableLoop, rowLoop ) {
                var data = this.data();
                if ( data.rate.ref_rate_period == '1month') {
                    rateTable.button( 'remove:name' ).disable();
                }
            });
        });
    

    Just adjust it to your situation e.g data.yourTable.active == 1 or something.

    Why "deselect":
    If you allow multi select and someone selects multiple records of which some must not be deletable you must also check the condition on deselect otherwise the user will be able also to delete forbidden records on the first deselect.

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    Works!
    Thanks a lot

This discussion has been closed.