Sorting based on TH

Sorting based on TH

drjeffpatrickdrjeffpatrick Posts: 1Questions: 1Answers: 0

Hi,

First of all thanks for the wonderful plugin.
I'm rendering data table with server side data in a Jsp page.
I have a backend variable,
if the variable is true, I will have 7 colomns and the sort index is 5
if I the backend variable is false, I will have 12 colomns. and the sort index is 11
So, is it possible to add a CSS to the TH (like : dataTable-defaultSort )so that the datatable sorting will be on that column initially.

Thanks
Jeff

Answers

  • allanallan Posts: 63,356Questions: 1Answers: 10,444 Site admin

    Not directly, but it would only require one additional step to get it working. Add the class you want and then in the DataTable initialisation just find the index of that column and use it as the default sort index:

    $('#myTable').DataTable( {
      order: [[ $('#myTable th.defaultSort').index(), 'asc' ]]
    } );
    

    Allan

This discussion has been closed.