问题
Suppose I have a column that contains object identifiers in the format P<2 digit year>-<4 digit identifier>, e.g.
P12-3767
I am using version 1.9.1 of the JQuery DataTables plugin for sorting and pagination. Is there a way that I can ignore the first 4 characters (the "P12-" part) of the cell values so that I'm effectively sorting on just the numeric part of the identifier?
回答1:
You could simply create a column in your query that is the substring version of the original string you are looking for. Then, when you sort, order by the substring column instead of the original. That's a database-side solution that would work great if you are using server-side paging.
You can also capture what the sort order and direction is via:
$(oSettings.aoColumns[oSettings.aaSorting[0][0]].nTh).attr('innerText');
So, you could include the substring column in your original query, hide it, then when the sort order clicked is the column you want to substring... change the sort to the hidden column instead. Not sure where you'd do this, possibly in fnPreDrawCallback.
回答2:
Import jstl functions in the jsp page
<%@ taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "fn" %>
Include data-sort attribute specifying the substring to sort
<td data-sort="${fn:substringAfter('P12-3767', 'P12-')}">P12-3767</td>
来源:https://stackoverflow.com/questions/10452305/how-to-sort-a-column-base-on-a-substring-of-the-values-in-each-cell-with-jquery