Pel*_*lle 22 tablesorter jquery-plugins
我正在使用jQuery tablesorter插件,我有一个包含月份和年份名称的列
April, 1975
January, 2001
Run Code Online (Sandbox Code Playgroud)
我想将此列排序为日期列.据我了解,可以使用其他一些"隐藏"值对列进行排序,但我似乎无法找到该功能的文档.有帮助吗?
更新
这个forkorter的叉子http://mottie.github.com/tablesorter/docs/index.html就是我所需要的; 能够存储值以在属性中排序,工作真的很棒.
Cha*_*les 36
只需使用textExtraction函数.在TD上设置数据排序值.如果不存在,则默认为普通文本.
$(".sort-table").tablesorter({
textExtraction: function(node) {
var attr = $(node).attr('data-sort-value');
if (typeof attr !== 'undefined' && attr !== false) {
return attr;
}
return $(node).text();
}
});
Run Code Online (Sandbox Code Playgroud)
Mot*_*tie 20
我有一个tablesorter的分支,允许你编写一个解析器,可以从表格单元格中提取数据属性,并为每列分配特定的textExtraction.
$(function(){
$.tablesorter.addParser({
// set a unique id
id: 'myParser',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s, table, cell, cellIndex) {
// get data attributes from $(cell).attr('data-something');
// check specific column using cellIndex
return $(cell).attr('data-something');
},
// set type, either numeric or text
type: 'text'
});
$('table').tablesorter({
headers : {
0 : { sorter: 'myParser' }
}
});
});
Run Code Online (Sandbox Code Playgroud)
jaz*_*cat 11
为回答一个旧问题而道歉,但这现在是一个标准特征的tablesorter,虽然由于某种原因它没有记录.如果您打开文件https://github.com/christianbach/tablesorter/blob/master/jquery.tablesorter.js并查看第307行,您会看到它支持"data-sort-value"属性.
用法:
<td data-sort-value="42">Answer to the question</td>
Run Code Online (Sandbox Code Playgroud)
这有点像黑客(好吧,这是一个彻底的黑客),但是如果您将列的解析器设置为“文本”,并使用您真正想要在隐藏范围内排序的字符串预先修复您的漂亮输出将正确排序。
您可以使用headers选项在列上设置解析器,例如将第一列和第二列上的解析器设置为“文本”,您可以设置以下内容:
headers: {0: {sorter: 'text'}, : {sorter: 'text'}
Run Code Online (Sandbox Code Playgroud)
要对日期进行此技巧,您可以使用按词法排序的 ISO8601 日期格式。JS 的Date对象可以通过该toISOString()函数生成 ISO8601 日期字符串。
鉴于 CSS:
span.hidden{
display:none;
}
Run Code Online (Sandbox Code Playgroud)
表中的示例单元格如下所示:
<td><span class="hidden">2015-04-18T23:48:33</span>19 April 2015</td>
Run Code Online (Sandbox Code Playgroud)
不是世界上最漂亮的代码,但它确实有效。
我在用
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.29.0/js/jquery.tablesorter.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
工作与数据文本
<td data-text="42">Answer to the question</td>
Run Code Online (Sandbox Code Playgroud)
不使用数据排序值
<td data-sort-value="42">Answer to the question</td>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14320 次 |
| 最近记录: |