-2

enter image description hereI am unable to add checkbox inside the table ,I tried lot of things but everything went to vain

Jquery Code:

$(document).ready(function() {
  var total = [];
  $.ajax({
    url: 'user.json',
    dataType: 'json',
    success: function(json) {
      // get the `airport` array 
      var device = json.siteList;
      var j = 1;

      // loop through the array to populate your list
      $.each(device, function(i, sites) {
        // console.log(sites.siteName)
        // $('#data_table').append("<tr>" + "<td>" + sites.siteName + "</td>" + "</tr>");
        $.each(sites.deviceList, function(i, values) {

          item = {}
          item['siteName'] = sites.siteName;
          item["deviceName"] = values.deviceName;
          item["count"] = values.count;
          total.push(item);

        });
      });

      $('#rpa_table').DataTable(
        columnDefs: [{
          orderable: false,
          className: 'select-checkbox',
          targets: 0
        }],
        select: {
          style: 'os',
          selector: 'td:first-child'
        },
        order: [
          [1, 'asc']
        ], {
          "aaData": total,
          "aoColumns": [{
              "sTitle": "Site",
              "mData": "siteName"
            },
            {
              "sTitle": "Cabinet",
              "mData": "deviceName"
            },
            {
              "sTitle": "Count",
              "mData": "count"
            },
          ],

        });

    }
  });
  // Handle click on "Select all" control
  // Handle click on "Select all" control
  $('#checkall').on('click', function() {
    // Check/uncheck all checkboxes in the table
    var rows = table.rows({
      'search': 'applied'
    }).nodes();
    $('input[type="checkbox"]', rows).prop('checked', this.checked);
  });

});

Every Time I add columnDef it shows me an errorenter image description here

The image is attached above, checked a lot of sites no clue nothing same error occures everytime any help would be highly appreciated Thanks in advance....................

Swarnadeep
  • 325
  • 1
  • 3
  • 17
  • 1
    Presumably, you should be passing an object to the DataTable constructor. Objects are enclosed in `{}`. eg `$('#rpa_table').DataTable({ columnDefs: [] })` – Turnip Sep 01 '20 at 14:02
  • ypeError: Cannot read property 'aDataSort' of undefined this error is coming – Swarnadeep Sep 01 '20 at 14:12
  • [Cannot read property 'aDataSort' of undefined](https://stackoverflow.com/questions/28454203/jquery-datatables-cannot-read-property-adatasort-of-undefined) – Turnip Sep 01 '20 at 14:18

1 Answers1

1

You have some syntax errors around your options being passed into .DataTables(). See my comments below:

$('#rpa_table').DataTable({     // <-- Need a curly brace here
    columnDefs: [ 
        {
            orderable: false,
            className: 'select-checkbox',
            targets:   0
        }
    ],
    select: {
        style: 'os',
        selector: 'td:first-child'
    },
    order: [
        [ 1, 'asc' ]
    ],
    {
        "aaData": total,
        "aoColumns": [
            {
                "sTitle": "Site",
                "mData": "siteName"
            },
            {
                "sTitle": "Cabinet",
                "mData": "deviceName"
            },
            {
                "sTitle": "Count",
                "mData": "count"
            }     // <-- Remove the trailing comma here
        ]         // <-- Remove the trailing comma here
    }             // <-- Close the inner object here
});               // <-- Close the options object here

When passing options to .DataTables() you have to pass the contents as an options object. This includes the opening and closing curly braces at the opening parathensis and the closing parathensis.

War10ck
  • 12,387
  • 7
  • 41
  • 54