JQuery Datatables : Cannot read property 'aDataSort' of undefined

前端 未结 8 1971
日久生厌
日久生厌 2020-12-02 21:43

I created this fiddle to and it works well as per my requirements: Fiddle

However, when I use the same in my application I get an error in the browser console saying

相关标签:
8条回答
  • 2020-12-02 22:11

    You need to switch single quotes ['] to double quotes ["] because of parse

    if you are using data-order attribute on the table then use it like this data-order='[[1, "asc"]]'

    0 讨论(0)
  • 2020-12-02 22:11

    In my case I solved the problem by establishing a valid column number when applying the order property inside the script where you configure the data table.

    var table = $('#mytable').DataTable({
         .
         .
         .
         order: [[ 1, "desc" ]],
    
    0 讨论(0)
  • 2020-12-02 22:13

    For me, the bug was in DataTables itself; The code for sorting in DataTables 1.10.9 will not check for bounds; thus if you use something like

    order: [[1, 'asc']]
    

    with an empty table, there is no row idx 1 -> this exception ensures. This happened as the data for the table was being fetched asynchronously. Initially, on page loading the dataTable gets initialized without data. It should be updated later as soon as the result data is fetched.

    My solution:

    // add within function _fnStringToCss( s ) in datatables.js
    // directly after this line
    // srcCol = nestedSort[i][0];
    
    if(srcCol >= aoColumns.length) {
        continue;
    }
    
    // this line follows:
    // aDataSort = aoColumns[ srcCol ].aDataSort;
    
    0 讨论(0)
  • 2020-12-02 22:18

    I had this problem and it was because another script was deleting all of the tables and recreating them, but my table wasn't being recreated. I spent ages on this issue before I noticed that my table wasn't even visible on the page. Can you see your table before you initialize DataTables?

    Essentially, the other script was doing:

    let tables = $("table");
    for (let i = 0; i < tables.length; i++) {
      const table = tables[i];
      if ($.fn.DataTable.isDataTable(table)) {
        $(table).DataTable().destroy(remove);
        $(table).empty();
      }
    }
    

    And it should have been doing:

    let tables = $("table.some-class-only");
    ... the rest ...
    
    0 讨论(0)
  • 2020-12-02 22:19

    It's important that your THEAD not be empty in table.As dataTable requires you to specify the number of columns of the expected data . As per your data it should be

    <table id="datatable">
        <thead>
            <tr>
                <th>Subscriber ID</th>
                <th>Install Location</th>
                <th>Subscriber Name</th>
                <th>some data</th>
            </tr>
        </thead>
    </table>
    
    0 讨论(0)
  • 2020-12-02 22:19

    In my case I had

    $(`#my_table`).empty();
    

    Where it should have been

    $(`#my_table tbody`).empty();
    

    Note: in my case I had to empty the table since i had data that I wanted gone before inserting new data.

    Just thought of sharing where it "might" help someone in the future!

    0 讨论(0)
提交回复
热议问题