DataTables Sorting html table on the basis of TD A

2019-05-07 15:38发布

问题:

I am using the DataTables jquery library to display simple HTML table markup, i.e. not using Ajax or JSON. It is all working nicely except that I have some columns where I would like to sort on the raw data, not on the formatted data that is displayed. I would like to add an attribute to my TD tags, such as 'data-sort="42"' and initialize DataTables, instructing it to sort the column based on this value, where present.

I have read the documentation for mData and mRender, but these seem to be handling a much more complex situation than my own, i.e. where the data is prone to being changed or fetched from the server on demand.

Please could someone spell out for me how the initialization code goes, or point me to an example that does so? Many thanks!

Bob

回答1:

Take a look at the section on Hidden title string sorting (or Hidden title numeric sorting) on this page: http://www.datatables.net/plug-ins/sorting. This method requires that you put your raw data into the title attribute of a span:

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

Then add this to your initialization script for the table (assuming you're using hidden title string sorting):

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