使用 Jquery 数据表对数据排序属性中的值进行自定义排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20286162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom sorting on values in a data-sort attribute with Jquery Datatables
提问by Pake Beet
I have to do some custom sorts with Jquery Datatables. I do not want to write custom sort functions for every custom sort.
我必须对 Jquery 数据表进行一些自定义排序。我不想为每个自定义排序编写自定义排序函数。
I want to define a value to sort on and have Datatables ignore the original column values, if value is defined.
如果定义了值,我想定义一个值进行排序并让数据表忽略原始列值。
For example:
例如:
<td data-sort="111123">E 1.111,23</td>
<td data-sort="111123">E 1.111,23</td>
I want Jquery Datatables to sort this column numerically on 111123
.
我希望 Jquery 数据表在111123
.
<td data-sort="19801220">20-12-1980</td>
<td data-sort="19801220">20-12-1980</td>
I want Jquery Datatables to sort this column numerically on 19801220
.
我希望 Jquery 数据表在19801220
.
<td>a string</td>
<td>a string</td>
I want Jquery Datatables to sort this column by its original value a string
.
我希望 Jquery Datatables 按其原始值对该列进行排序a string
。
http://www.datatables.net/plug-ins/sortinghas "Hidden title numeric sorting" which is close to what I want, but requires me to specify for every datatable on which column this custom sorting applies. I have too many datatables of differing sizes to do this in a reasonable time. I just want to make Datatables always sort this hidden value / data-* attribute if it is present. No need for custom sort definitions on specific columns.
http://www.datatables.net/plug-ins/sorting具有“隐藏标题数字排序”,这与我想要的很接近,但需要我为每个数据表指定此自定义排序适用的列。我有太多不同大小的数据表,无法在合理的时间内完成此操作。我只是想让 Datatables 总是对这个隐藏的值/data-* 属性进行排序(如果它存在)。无需对特定列进行自定义排序定义。
Related: jQuery DataTables: how to sort by custom parameter value rather than the content of the cell?but unfortunately no answer as to how to sort simply by custom parameter, instead pointers to custom sorting scripts.
相关:jQuery DataTables:如何按自定义参数值而不是单元格内容排序?但不幸的是,没有关于如何简单地通过自定义参数进行排序的答案,而是指向自定义排序脚本的指针。
采纳答案by Vitall
You can use data-order attr, for example
例如,您可以使用数据顺序属性
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Date</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<?php
$count = 0;
foreach($users as $user) {?>
<tr>
<td data-order="<?php echo $count ?>">
<?php echo $user['createdDate']; ?>
</td>
<td>
<?php echo $user['count']; ?>
</td>
</tr>
<?php
$count++;
}?>
<tr>
<td data-order="999999999999999999999999999"> <!--always last-->
Total
</td>
<td>
<?php echo count($users); ?>
</td>
</tr>
more information HTML5 data-* attributes
更多信息HTML5 data-* 属性
回答by Pake Beet
I found an easy solution that works for me and does not require too much code or changes to datatables.js.
我找到了一个对我有用的简单解决方案,不需要太多代码或对 datatables.js 进行更改。
It is very similar to the requirements of the question, but not exactly the same.
它与问题的要求非常相似,但不完全相同。
Instead of data-sort
you can use a HTML comment tag.
data-sort
您可以使用 HTML 注释标签代替。
<td data-sort="111123">E 1.111,23</td>
<td data-sort="111123">E 1.111,23</td>
becomes
变成
<td><!-- 000000111123 -->E 1.111,23</td>
<td><!-- 000000111123 -->E 1.111,23</td>
By zero-padding an int
they will be sorted as a string
. The zero-padding makes the sort behave as you'd expect: sorting the integers from high to low.
通过零填充 aint
它们将被排序为 a string
。零填充使排序的行为符合您的预期:将整数从高到低排序。
Solution works for dates, ints and strings. For dates and ints you can use a scripting language to output them in the way you want (eg: zero-padded, formatted as yyyy-mm-dd).
解决方案适用于日期、整数和字符串。对于日期和整数,您可以使用脚本语言以您想要的方式输出它们(例如:零填充,格式为 yyyy-mm-dd)。