我是rails的新手,使用jquery数据库rails gem来排序和搜索我的表数据。它运行正常但是当我使用rails辅助方法(如number_with_delimiter)来显示以逗号分隔的数字时,我没有得到正确的排序结果。
不使用rails helper我得到了正确的结果,我的表数据正确排序。请任何人帮帮我吗?
这是我的代码:
脚本
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#d3').dataTable({
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap",
aaSorting : [[2, 'desc']],
});
});
</script>
在我的部分表数据是
<table class="table table-striped table-bordered " id="d3">
<thead>
<tr>
<th>Folder</th>
<th>#Files</th>
<th>Source Lines</th>
<th>Contents</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<td>String data</td>
<td>numeric data</td>
<td>numeric data</td>
<td>string data</td>
<td>string data</td>
</tbody>
</table>
这是我的结果(第3列数据)
SourceLines
994
98
974
.
.
.
101
1,890
1,674
答案 0 :(得分:0)
我在使用复数帮助器将值添加“Hour(s)”时出现此问题。它发生的原因是因为数据表将值视为字符串而不是数字,并将其排序为一个。
我的解决方案是在我的真实值之前添加一个隐藏的跨度,可以作为字符串正确排序。隐藏的跨度值是具有足够前面的'0'的数字,以使其与该列中的其他值一致。
def datatable_numeric_sort num
content_tag(:span, number_with_precision(num, :precision => 2).to_s.rjust(10, '0'), :style => "display:none")
end
给定数字100.5,它将产生:
<span style="display:none">0000100.50</span>
.to_s.rjust(10,'0')表示“将”0“附加到此字符串,直到长度为10” 它可以是任何长度,我使用10,因为我的字符串永远不会更长。
在我的情况下,number_with_precision是必要的,因为我的数字是小数,你需要在字符串中小数点的位置一致。
使用:
<td><%= datatable_numeric_sort record.value%><%= pluralize record.value, "Hour" %></td>