简体   繁体   中英

React - How to add onclick function to buttons in each row in Datatables

I'm trying to add: edit and delete buttons to each row in my datatable, but clicking the buttons don't execute the functions declared in the "onclick"

$(schedTableID).DataTable({
      data: schedData.data,
      order: [[0, "asc"]],
      columns: [
        { data: "id",render: function(data, type, row) { return data; }, },
        { data: "timein", render: function(data, type, row) {  return data; }, },
        { data: "timeout", render: function(data, type, row) {  return data; }, },
        { data: "slot", render: function(data, type, row) { return data; }, },
        { data: "session", render: function(data, type, row) { return data; }, },
        { data: "schedule", render: function(data, type, row) { return data; }, },
        { data: "statuss", render: function(data, type, row) { return data; }, },
        { data: "id", render: 
          function(data, type, row) { 
            return(
              `
                <button  class="m-0 p-2 px-2 btn btn-success feather icon-check" data-toggle="button" id="${data}" onClick="${deleteHandler}"></button>
                <button  class="m-0 p-2 px-2 btn btn-danger feather icon-x" data-toggle="button" id="${data}" onclick="${deleteHandler}"></button>
              `
            )
          }, 
        },
        
      ],
      pageLength: 5,
      lengthMenu: [
        [5, 10, 25, 50, 100, -1],
        [5, 10, 25, 50, 100, "Show All"],
      ],
    });

Here are the onclick functions

const deleteHandler = (e) => {
  console.log(123123)
}
const checkHandler = (e) => {
  console.log(123123)
}

I used jquery instead

$(document).ready(function () {

    $('#data-table-admin').unbind('click').on('click', '.btn-edit', function () {
      let table = $('#data-table-admin').DataTable();
      //retrieve table id, etc. (table.row($(this).parents('tr')).data().id)

      //function here 
    });


   $('#data-table-admin').on('click', '.btn-delete', function () {
      let table = $('#data-table-admin').DataTable();
      //retrieve table id, etc. (table.row($(this).parents('tr')).data().id)

      //functionality here

   });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM