Datatables如何正确排序这个表格?

huangapple go评论71阅读模式
英文:

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() {
 $(&#39;#abc&#39;).DataTable( {
   columnDefs: [
       { type: &#39;natural&#39;, targets: 0 }
     ],
  responsive: true,
  
   
  bSort: true,   
   order: []
} );

  } );

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-1.11.3.min.js&quot;&gt;&lt;/script&gt;

    &lt;link href=&quot;https://nightly.datatables.net/css/jquery.dataTables.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
    &lt;script src=&quot;https://nightly.datatables.net/js/jquery.dataTables.js&quot;&gt;&lt;/script&gt;

    &lt;meta charset=utf-8 /&gt;
    &lt;title&gt;DataTables - JS Bin&lt;/title&gt;
  &lt;/head&gt;
&lt;body&gt;
&lt;table id=&quot;abc&quot;  style=&quot;width: 100%;&quot;&gt;&lt;thead&gt;

&lt;tr&gt;
&lt;th style=&quot; width: 25%;&quot;&gt;abcd&lt;/th&gt;

&lt;/tr&gt;

&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt; 1.2&lt;/td&gt;


&lt;tr&gt;
&lt;td&gt; 3.001&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt; 8.1025&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt; 40&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt; 41&lt;/td&gt;

&lt;/tr&gt;


&lt;tr&gt;
&lt;td&gt; 180&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt; 205&lt;/td&gt;




&lt;/tr&gt;

&lt;/tbody&gt;&lt;/table&gt;

<!-- 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() {
    $(&#39;#abc&#39;).DataTable({
        columnDefs: [{
            type: &#39;num&#39;,
            targets: 0
        }],
        responsive: true,


        bSort: true,
        order: []
    });

});

huangapple
  • 本文由 发表于 2023年4月20日 00:56:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定