teikuei
teikuei

Reputation: 311

Do an action when click a button in datatable

sorry about my English, is not very good. I'll try to explain my problem. I've a table with some button at the end of each row, and I need a diferent action when click them. Table is made with dataTables and this is all I have.

    $(document).ready(function() {
    var table = $('#dataart').DataTable( {

        "columnDefs": [ {
            "targets": -1,
            "data": null,
            "defaultContent": '<button id="ver" class="btn btn-sm btn-primary"><span class="glyphicon glyphicon-eye-open"></span></button>&nbsp;' +
                              '<button id="editar" class="btn btn-sm btn-warning"><span class="glyphicon glyphicon-edit"></span></button>&nbsp;' +
                              '<button id="eliminar" class="btn btn-sm btn-danger"><span class="glyphicon glyphicon-trash"></span></button>'
        } ]
    } );

    $('#dataart tbody').on( 'click', 'button', function () {
        var data = table.row( $(this).parents('tr') ).data();
        **if (button id=ver click) do something like this document.getElementById("x").value= data[1] or call a function.
        if (button id=editar click) do a second function.
        if (button id=eliminar click) do other function.**
    } );

} )

And that's all, maybe is very simple to do, but it's first time I use this plugin.

Thank you very much.

Upvotes: 0

Views: 2690

Answers (2)

Ravinder Reddy
Ravinder Reddy

Reputation: 3879

I hope the below code will help. Not tested

$('#dataart tbody').on( 'click', 'button', function () {
    var data = table.row( $(this).parents('tr') ).data();
    var button_id = this.id; // get the id of the button clicked
    if (button_id=='ver'){
        //do some thing
    }
    if (button_id=='editar'){ //do a second function.}
    if (button_id=='eliminar'){ //do other function.}
} );

Upvotes: 2

Bahirji Naik
Bahirji Naik

Reputation: 185

Give hyperlink to button. like :

<a href='xyz.html'><button.....></button>

If you want to open file, give hyperlink. If you want to run script, use onClick action of button.

Upvotes: 0

Related Questions