1

I am trying to modify jQuery DataTable with a custom CheckBox column. My requirement is to add a CheckBox with every row of a table and add click event with the respective row check boxes, so whenever I click on the check boxes, it should show the related row details or get the data to the database. I am able to get data with Ajax call from database and done with the CheckBoxes. I was following this tutorial to make it work and it works fine now:

jQuery DataTable with CheckBoxes

This is what I've done so far in the back-end:

public JsonResult GetData()
{
   var result = db.Doctors.Select(c => new
   {
      FirstName = c.Firstname,
      LicenseNo = c.LicenseNo
   }).ToList();

   return Json(new { data = result }, JsonRequestBehavior.AllowGet);
}

In the front-end:

<link href="https://cdn.datatables.net/s/dt/dt-1.10.10,se-1.1.0/datatables.min.css" rel="stylesheet" />
<link href="https://gyrocode.github.io/jquery-datatables-checkboxes/1.2.6/css/dataTables.checkboxes.css" rel="stylesheet" />

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="https://cdn.datatables.net/s/dt/dt-1.10.10,se-1.1.0/datatables.min.js"></script>
<script src="https://gyrocode.github.io/jquery-datatables-checkboxes/1.2.6/js/dataTables.checkboxes.min.js"></script>
<script>
    $(document).ready(function () {
        var table = $('#example').DataTable({
            "ajax": {
                url: '@Url.Action("GetData")',
                type: "get",
                datatype: "json",
                data: {}
            },

            "columnDefs": [
                   {
                       'targets': 0,
                       'checkboxes': {
                           'selectRow': true
                       }
                   }
            ],

            "columns": [
                { "data": "FirstName" },
                { "data": "FirstName" },
                { "data": "LicenseNo" }
            ],

            'select': {
                'style': 'multi'
            },
            'order': [[1, 'asc']]
        });
    });
</script>

<body>
    <div>
        <hr><br>
        <form>
            <table id="example" class="display" cellspacing="0">
                <thead>
                    <tr>
                        <th></th>
                        <th>Name</th>
                        <th>License No</th>
                    </tr>
                </thead>
            </table>
            <hr>
        </form>
    </div>
</body>

Now the issue is how can I add the check box click events in the jQuery DataTable. So whenever I click with check boxes, I can retrieve the respective row details.

N.B: I've done little R&D on this and checked that the CheckBox is given a class named dt-checkboxes. It's generated dynamically. Is there any way that I can make it to my requirement like assigning unique ids to the check boxes, bit stuck here. This is how it looks like - Simple enough:

jQuery DataTable

9
  • Have you tried with jQuery loops to iterate over the check boxes? Commented May 25, 2018 at 16:20
  • You do not need to give an Id to that. What is stopping you from using ` dt-checkboxes` css class selector to wireup the click event on the checkbox and make the ajax call ? Commented May 25, 2018 at 16:21
  • I guess, the dt-checkboxes class will be applicable to all the check boxes whenever clicked on a single row @Shyju. Commented May 25, 2018 at 16:22
  • 2
    Yes. But your click happens on only one checkbox and $(this) will give you that. Commented May 25, 2018 at 16:24
  • And the code you shared will not render the the checkbox at all!!!! Commented May 25, 2018 at 16:27

1 Answer 1

2

You can use event listeners on the select and deselect events.

table.on( 'deselect', function ( e, dt, type, indexes ) {
   //get the row information for the row deselected.
   console.log(dt.row(indexes).data());
});
table.on( 'select', function ( e, dt, type, indexes ) {
   //get the row information for the row selected.
   console.log(dt.row(indexes).data());
});

The above code snippet will provide you the data for the row that was selected or deselected.

I forked your fiddle to demonstrate it.

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.