How can I disable 'edit' button by using table event

How can I disable 'edit' button by using table event

5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0

Hi, I have to disable 'edit' button based on selected record, here is what I did

I used default button option for editer

    table = $("#POTable").DataTable
     buttons: [
    { extend: 'create', editor: editor, text: 'Create' },
    { extend: 'edit', editor: editor, text: 'Edit' },
...

    table.on('select', function (e,dt, type, indexes) {
        if (type = 'row') {
            var exdata = table.rows('.selected', { select: true }).data()[0];
            if (exdata.ReceptionStatus === 'C') {
                table.button(0).disable();
                table.button(1).enable();
            } else if (exdata.ReceptionStatus === 'D') {
                table.button(0).enable();
                table.button(1).disable();
            } else {
                table.buttons().disable();
            }

        }
    })

The issue is I cannot disable 'edit' button, the event works however the button was enabled by somewhere inside, please help.

Thanks,
Wenbin

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,055 Site admin
    Answer ✓

    The edit button type has its own select event listener, so you might run into a conflict there.

    What I would suggest instead is that you have your own completely custom button and use the Editor API to activate the Editor. That way you can control the enabled state of the button yourself.

    Regards,
    Allan

  • 5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0

    Hi Allan,

    Thanks for your comment, it works, Here is my code.

            buttons: [
        {
            text: 'create', action: function (e, dt, node, config) {
                editor.create();
            }
        },
        {
            text: 'edit', action: function (e, dt, node, config) {
                editor.edit(table.rows('.selected', { select: true }));
            }
        },
    
This discussion has been closed.