How to include a checkbox column for all rows in Datatable?

Asked

Viewed 417 times

1

I would like to place a column at the end, with a checkbox or toggle switch button for all rows coming from an ajax call.

I managed to put by sDefaultContext and mRender, but the checkbox does not work, does not recognize the click.

Does anyone know how to solve?

Currently, I use the following code snippet:

        "aoColumnDefs" : [
            //adiciona a classe para todas as células referente a coluna indicada
            {"sClass" : "hidden-xs sorting","aTargets" : [1]},
            {"mData": null, "sDefaultContent": "<input type='checkbox' name=''></input>","aTargets": [3]}
        ]
  • How are you adding the checkbox? you can put so when adding the checkbox { onclick='Method()'; }

  • @Paulohdsousa I currently use this code snippet : oTable = $('#table1'). datatable({ "aoColumnDefs" : [ //add the class for all cells referring to the specified column {"sClass" "Hidden-Xs Sorting","aTargets" [1]}, {"mData": null, "sDefaultContent": "<input type='checkbox' name=''></input>","aTargets": [3]} ], It appears as I want it just doesn’t work properly

  • 1

    So do -> oTable = $('#table1'). datatable({ "aoColumnDefs" : [ {"sClass" : "Hidden-Xs Sorting","aTargets" [1]}, {"mData": null, "sDefaultContent": "<input onclick="Alert('Call a method here');" type='checkbox' name='></input>","aTargets": [3]} ], Just put the method out and create... does not try to bind elements dynamically added

1 answer

1


Hello,

Do so

function MeuMetodo(){
alert('Faça o que quer aqui');
}

 "aoColumnDefs" : [
            //adiciona a classe para todas as células referente a coluna indicada
            {"sClass" : "hidden-xs sorting","aTargets" : [1]},
            {"mData": null, "sDefaultContent": "<input onclick='MeuMetodo();' type='checkbox' name=''></input>","aTargets": [3]}
        ]

It is not possible to give Jquery Bind to dynamically added elements unless the code that makes the Bind is read after adding the HTML element.

  • Thanks! It worked! And to style this checkbox? Actually what I need is to use this http://www.bootstrap-switch.org/ plug-in, which turns the checkbox into switch toggle button, but it doesn’t seem to recognize the css or js

  • Puts class in input -> <input class="Meuinput" onclick='Meumetodo();' type='checkbox' name=''></input> And run the $(".Meuinput" plugin code). bootstrapSwitch();

  • did not work. I was doing this but through the name

  • Try with class.. and run the code AFTER the checkbox add code.

  • Thank you! It’s working!

  • I have one more problem, I don’t know if you can help me, I managed to generate the buttons and everything, but when I do an action in one of the switches it changes all, due to sDefaultContent replicate for all lines, and if I put an ID in it, It won’t change because the rest will have the same ID. You know what I can do?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.