How to sort a column base on a substring of the values in each cell with JQuery DataTables

依然范特西╮ 提交于 2020-01-05 03:53:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!