DataTables Sort html table based on TD attribute value

I am using jQuery DataTables library to display simple markup of an HTML table, i.e. without using Ajax or JSON. Everything works well, except that I have several columns where I would like to sort the raw data, rather than the formatted data that is displayed. I would like to add an attribute to my TD tags, for example "data-sort =" 42 "and initialize the DataTables, indicating that it will sort the column based on this value, if present.

I read the documentation for mData and mRender, but they seem to handle a much more complicated situation than mine, i.e. where data is subject to change or selection from the server on demand.

Please, can someone tell me how the initialization code goes, or give me an example that does this? Thank you very much!

Bean

+4
source share
1 answer

Take a look at the section on sorting a hidden header line (or sorting with a hidden name) on this page: http://www.datatables.net/plug-ins/sorting . This method requires you to place your raw data in the title span attribute:

<td><span title="[raw data]">[Formatted data]</span></td> 

Then add this to your initialization script for the table (assuming you use hidden header row sorting):

 "aoColumns": [ { "sType": "title-string" }, ] 
+5
source

All Articles