2

I have a datatable that has a delete button, when click the delete button i want to pass the values in the row to my sever to delete and also delete in on the row when the server responds with a success. How can i achieve getting the value of the row to the onclick function and also the tr element so i can delete it.

enter image description here

this is my present code:

model.initDataTable = function () {

    $('#datatables').DataTable({

        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "http://localhost:8080/admin/query/fibre",
            "data": function ( data ) {
                //process data before sent to server.
            }},
        "columns": [
            { "data": "id", "name" : "ID", "title" : "ID"  },
            { "data": "name", "name" : "Name" , "title" : "Name"},
            { "class":"",
                "orderable":      false,
                "data":           null,
                "defaultContent": "<button id='dltBtn' type=\"button\" class=\"btn purple-gradient btn-sm\">Delete</button>"},
            { "class":"",
                "orderable":      false,
                "data":           null,
                "defaultContent": "<button id='viewBtn' type=\"button\" class=\"btn purple-gradient btn-sm\">Delete</button>"},

        ]




    } );


    $('#datatables tbody').on( 'click', '#dltBtn', function () {



    } );




};

1 Answer 1

2

You need to use class instead of ID for the delete button, for example: btn-delete.

Then use the handler below.

$('#datatables tbody').on('click', '.btn-delete', function (){
   var $row = $(this).closest('tr');
   var data =  $('#datatables').DataTable().row($row).data();

   console.log('data', data);
   console.log('Record ID is', data['id']);

   // Add your code here   

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.