0

I have a server-side datatable setup with the checkboxes plugin. Each checkbox has the data of a product_id.

var table = $('#display_users').DataTable( {
        "processing": true,
        "serverSide": true,
        'ajax': '{{ route ('/getUsers') }}',
        'columns' : [
            {"data" : "product_id"},
            {"data" : "product_id"},
            {"data" : "first_name"},
            {"data" : "last_name"},
            {"data" : "email"},
            {"data" : "company"},
            {"data" : "department"},
            {"data" : "created_at"}
        ],
        'columnDefs': [
            {
                'targets': 0,
                'checkboxes': {
                    'selectRow': true
                },

I would like to be able to, when a checkbox is selected, select all the checkboxes with the same product_id. This is only necessary for the records on the currently selected page. It seems this should be possible with the checkboxes select api, however I haven't been successful so far

0

1 Answer 1

2

I don't see much value in 'checkbox' plug-in as its features can be effortlessly implemented with few lines of code while giving you greater flexibility.

However, you don't really need to dig deep into 'checkbox' plug-in internals as your required feature can be easily coded with native DataTables API:

//listen for the clicking first column checkboxes
$('#display_users').click('tbody td:eq(0) [type="checkbox"]', function(){
  //grab current checkbox state and criteria to match (product_id) from the row data
  const state = $(event.target).prop('checked');
  const productId = dataTable.row($(event.target).closest('tr')).data().product_id;
  //iterate through current page rows and adjust checkboxes upon matching product_id
  dataTable.rows({page:'current'}).every(function(){
    if(this.data().product_id == productId) $(this.node()).find('td:eq(0) [type="checkbox"]').prop('checked', state);
  });
});

Complete demo of this concept you may find below:

//data sample
const srcData = [
  {product_id: 7, first_name: 'Nick', last_name: 'Furry', company: 'Avengers Inc'},
  {product_id: 7, first_name: 'Steve', last_name: 'Rogers', company: 'Avengers Inc'},
  {product_id: 4, first_name: 'Arthur', last_name: 'Curry', company: 'Justice Ltd'},
  {product_id: 4, first_name: 'Clark', last_name: 'Kent', company: 'Justice Ltd'},
  {product_id: 4, first_name: 'Barry', last_name: 'Allen', company: 'Justice Ltd'}
];

//datatable initialization
const dataTable = $('#display_users').DataTable({
  dom: 't',
  order: [1, 'asc'],
  data: srcData,
  columns: [null, ...Object.keys(srcData[0])].map(header => ({title: header || '', data: header})),
  columnDefs: [
    {targets: 0, orderable: false, render: () => '<input type="checkbox"></input>'}
  ]
});

//essential part - row checkbox click hander
$('#display_users').click('tbody td:eq(0) [type="checkbox"]', function(){
  //grab current checkbox state and criteria to match (product_id)
  const state = $(event.target).prop('checked');
  const productId = dataTable.row($(event.target).closest('tr')).data().product_id;
  //iterate through current page rows and adjust checkboxes upon matching product_id
  dataTable.rows({page:'current'}).every(function(){
    if(this.data().product_id == productId) $(this.node()).find('td:eq(0) [type="checkbox"]').prop('checked', state);
  });
});
<!doctype html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
</head>
<body>
<table id="display_users"></table>
</body>
</html>

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.