How add more then one button in each row in JQuery datatables and how to apply event on them

感情迁移 提交于 2020-01-02 08:56:17

问题


I have one button but I need two buttons and perform some MySql when they are clicked to get data from the underlying database.

How can I perform an event using these two buttons?

$(document).ready(function () {
    var table= $('#example').dataTable( {
        "ajax": "..//wp-content/plugins/jobify/Admin/data.txt",
        "columnDefs": [ {
            "targets": -1,
            "data": null,
            "defaultContent": "<button>View Posted Jobs</button>"
        } ]
    } );

    $('#example tbody').on( 'click', 'button', function () {
        //var data = table.row( $(this).parents('tr') ).data();
    } );
} );

回答1:


Just assign a class to each button and attach an event handler to each class.

$(document).ready(function () {
   var table = $('#example').dataTable( {
      "ajax": "../wp-content/plugins/jobify/Admin/data.txt",
       "columnDefs": [ {
          "targets": -1,
          "data": null,
          "defaultContent": 
             '<button class="btn-view" type="button">View Posted Jobs</button>'
             + '<button class="btn-delete"  type="button">Delete</button>'
        } ]
     } );

     // Handle click on "View" button
     $('#example tbody').on('click', '.btn-view', function (e) {
        //var data = table.row( $(this).parents('tr') ).data();
     } );


     // Handle click on "Delete" button
     $('#example tbody').on('click', '.btn-delete', function (e) {
        //var data = table.row( $(this).parents('tr') ).data();
     } );
});


来源:https://stackoverflow.com/questions/31327933/how-add-more-then-one-button-in-each-row-in-jquery-datatables-and-how-to-apply-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!