2

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();
    } );
} );
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • You probably would want to look at the `columnDefs` `render` option. See the doc [here](https://datatables.net/reference/option/columns.render) – zgood Jul 09 '15 at 20:59
  • Please try to format your question to the best of your abilities and make it as concise (short but well described) as possible. You'll get more attention and better answers in return. – Maarten Bodewes Jul 11 '15 at 22:29

1 Answers1

11

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();
     } );
});
Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185