英文:
Datatables how do I sort this table correctly?
问题
我的数据表格排序不正确。我认为问题与小数值有关,因为它们没有一致性。如何才能正确排序?我需要一个特殊的插件吗?任何帮助/建议都将不胜感激。提前感谢。请查看下面的代码。非常感谢您的时间和帮助。
英文:
My datatable is not sorting correctly. I think the problem is related to the decimal values as there is no consistency in them.
What is the best way to sort this correctly?
Do I need a special plugin for this?
Any help/advice appreciated.
Thanks in advance.
Please see code below. Thank you very much for your time and assistance.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('#abc').DataTable( {
columnDefs: [
{ type: 'natural', targets: 0 }
],
responsive: true,
bSort: true,
order: []
} );
} );
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
<meta charset=utf-8 />
<title>DataTables - JS Bin</title>
</head>
<body>
<table id="abc" style="width: 100%;"><thead>
<tr>
<th style=" width: 25%;">abcd</th>
</tr>
</thead><tbody>
<tr>
<td> 1.2</td>
<tr>
<td> 3.001</td>
</tr>
<tr>
<td> 8.1025</td>
</tr>
<tr>
<td> 40</td>
</tr>
<tr>
<td> 41</td>
</tr>
<tr>
<td> 180</td>
</tr>
<tr>
<td> 205</td>
</tr>
</tbody></table>
<!-- end snippet -->
答案1
得分: 2
DataTables默认按文本排序。如果你想按数字排序,你需要在你的columnDefs
中指定。
你可以将你的列类型设置为num
,如此描述的那样:
https://datatables.net/reference/option/columns.type
$(document).ready(function() {
$('#abc').DataTable({
columnDefs: [{
type: 'num',
targets: 0
}],
responsive: true,
bSort: true,
order: []
});
});
英文:
DataTables default is to sort as text. If you want to sort numerically, you need to specify that in your columnDefs
.
You can set your column type as a num
, as described here:
https://datatables.net/reference/option/columns.type
$(document).ready(function() {
$('#abc').DataTable({
columnDefs: [{
type: 'num',
targets: 0
}],
responsive: true,
bSort: true,
order: []
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论