Table Object Redraw With Empty Sort Array Throws Error

Table Object Redraw With Empty Sort Array Throws Error

joshuasiegaljoshuasiegal Posts: 19Questions: 2Answers: 1
edited September 2016 in Bug reports

According DT documentation, the proper way to not sort a Table is to pass an empty array to the "order" option.

Also, to redraw a table, with the new documentation, it's recommended to not destroy and reinstantiate the table, but rather call clear() and then rows.add(data), and then draw() on the existing table object.

However, if the first draw has a sort, and the redraw of the table requires no sort, DT will throw an error: "TypeError: e[j] is undefined".

Sample:

var dashboardOrdersDefaultSort = [[6,'desc']];

if ( /* some condition */ ) {
    dashboardOrdersDefaultSort = [];
}

if (!dashboardOrdersDTO) {
        dashboardOrdersDTO = $dashboardOrdersTable.DataTable({
          destroy:true,
          data:theData,
          columns:dashboardOrdersMap,
          autoWidth:false,
          order:dashboardOrdersDefaultSort,
          paging:false,
          dom:'ft',
          searching: false
        });
  } else {
        dashboardOrdersDTO.clear();
        dashboardOrdersDTO.rows.add(theData);
        dashboardOrdersDTO.order(dashboardOrdersDefaultSort).draw();
  }

Any help is much appreciated, thanks!

Answers

  • allanallan Posts: 63,678Questions: 1Answers: 10,497 Site admin

    dashboardOrdersDTO.order(dashboardOrdersDefaultSort).draw();

    This won't remove any sort applied to the table - it means don't resort the data, but the data will already be sorted! This plug-in can be used to have DataTables show data in the index order.

    If that doesn't work for you, please show a test case.

    Allan

  • joshuasiegaljoshuasiegal Posts: 19Questions: 2Answers: 1
    edited September 2016

    That works, but the remaining question is how to apply a sort to the table redraw, in the case that the instantiation has order:[ ] and the redraw then wants a sort applied.

    Thanks for the quick response sir!

  • joshuasiegaljoshuasiegal Posts: 19Questions: 2Answers: 1

    Wups, I got it.

    Here is what works, using the plugin solution mentioned above.

    var dashboardOrdersDefaultSort = [[6,'desc']];
     
    if ( /* some condition */ ) {
        dashboardOrdersDefaultSort = [];
    }
    
        if (!dashboardOrdersDTO) {
            dashboardOrdersDTO = $dashboardOrdersTable.DataTable({
              //etc.
              order:dashboardOrdersDefaultSort,
              //etc.
            });
          } else {
            dashboardOrdersDTO.clear();
            dashboardOrdersDTO.rows.add(theData);
            if (!dashboardOrdersDefaultSort.length) {
              dashboardOrdersDTO.order.neutral();
            } else {
              dashboardOrdersDTO.order(dashboardOrdersDefaultSort);
            }
            dashboardOrdersDTO.draw();
          }
    
    

    Thanks again for the reply.

    I still think it might be buggy that:

    sortArray = [[0, "asc"]];  //ok for instantiation, ok for DTO.order(sortArray)
    
    sortArray = [];  //ok for instantiation, throws error on DTO.order(sortArray)
    
    

    Really appreciate the help and speedy reply!

  • joshuasiegaljoshuasiegal Posts: 19Questions: 2Answers: 1

    For anyone reading this, something insinuated but not explicitly mentioned above is that, in this case, "dashboardOrdersDefaultSort" can change value between initializing and subsequent redraws of the table, as well.

  • allanallan Posts: 63,678Questions: 1Answers: 10,497 Site admin

    You might want to .slice() the array you are passing in. I can't remember off the top of my head, but I think DataTables will simply use the array you pass in, so any changed made to it would still be in effect.

    Allan

This discussion has been closed.