有没有办法禁用 jquery DataTables 的初始排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4964388/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:17:55  来源:igfitidea点击:

Is there a way to disable initial sorting for jquery DataTables?

jqueryjquery-datatables

提问by smoak

I'm using the jquery DataTablesplugin. From their documentation:

我正在使用jquery DataTables插件。从他们的文档:

If sorting is enabled, then DataTables will perform a first pass sort on initialisation. You can define which column(s) the sort is performed upon, and the sorting direction, with this variable. The aaSorting array should contain an array for each column to be sorted initially containing the column's index and a direction string ('asc' or 'desc').

如果启用排序,则 DataTables 将在初始化时执行第一遍排序。您可以使用此变量定义对哪些列执行排序以及排序方向。aaSorting 数组应包含每个要排序的列的数组,最初包含列的索引和方向字符串('asc' 或 'desc')。

Is it possible to have sorting enabled but disable this first pass sort on initialization? I am currently doing the initial sort server side and need sorting functionality but don't need this initial sort functionality.

是否可以启用排序但在初始化时禁用此第一遍排序?我目前正在做初始排序服务器端,需要排序功能,但不需要这个初始排序功能。

回答by smoak

Well I found the answerset "aaSorting" to an empty array:

好吧,我发现答案集“aaSorting”为一个空数组:

$(document).ready( function() {
    $('#example').dataTable({
        /* Disable initial sort */
        "aaSorting": []
    });
})

For newer versions of Datatables (>= 1.10) use orderoption:

对于较新版本的数据表 (>= 1.10),请使用order选项:

$(document).ready( function() {
    $('#example').dataTable({
        /* No ordering applied by DataTables during initialisation */
        "order": []
    });
})

回答by Ravi Kadaboina

As per latest api docs:

根据最新的 api 文档:

$(document).ready(function() {
    $('#example').dataTable({
        "order": []
    });
});

More Info

更多信息

回答by FennRussel

Try this:

尝试这个:

$(document).ready( function () {
  $('#example').dataTable({
    "order": []
  });
});

this will solve your problem.

这将解决您的问题。

回答by luchopintado

In datatable options put this:

在数据表选项中输入:

$(document).ready( function() {
  $('#example').dataTable({
    "aaSorting": [[ 2, 'asc' ]], 
    //More options ...

   });
})

Here is the solution: "aaSorting": [[ 2, 'asc' ]],

这是解决方案: “aaSorting”:[[ 2, 'asc' ]],

2means table will be sorted by third column,
ascin ascending order.

2表示表将按第三列
asc升序排序。