jquery 数据表默认排序不起作用

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

jquery datatables default sort not working

jqueryjquery-plugins

提问by turbo2oh

I have a 4 column table, I want the first 3 columsn to be sortable by the user, but not the 4th, this is working fine. I also want the 3rd column to sort in ASC order by default. This part isn't working, I cant get any of the columns to sort by default and can't figure out what's wrong with my syntax:

我有一个 4 列表,我希望用户可以对前 3 列进行排序,但不能对第 4 列进行排序,这工作正常。我还希望第 3 列默认按 ASC 顺序排序。这部分不起作用,默认情况下我无法对任何列进行排序,也无法弄清楚我的语法有什么问题:

$(document).ready(function() {
$(".table-sortable").dataTable({
    aaSorting: [],
    bPaginate: false,
    bFilter: false,
    bInfo: false,
    bSortable: true,
    bRetrieve: true,
    aoColumnDefs: [
        { "aTargets": [ 0 ], "bSortable": true },
        { "aTargets": [ 1 ], "bSortable": true },
        { "aTargets": [ 2 ], "asSorting": [ "asc" ], "bSortable": true },
        { "aTargets": [ 3 ], "bSortable": false }
    ]
}); 
});

Here's what I've been working from: http://datatables.net/usage/columns

这是我一直在工作的内容:http: //datatables.net/usage/columns

回答by BLSully

This should get you what you need

这应该可以满足您的需求

$(document).ready(function() {
    $(".table-sortable").dataTable({
        aaSorting: [[2, 'asc']],
        bPaginate: false,
        bFilter: false,
        bInfo: false,
        bSortable: true,
        bRetrieve: true,
        aoColumnDefs: [
            { "aTargets": [ 0 ], "bSortable": true },
            { "aTargets": [ 1 ], "bSortable": true },
            { "aTargets": [ 2 ], "bSortable": true },
            { "aTargets": [ 3 ], "bSortable": false }
        ]
    }); 
});

The key is the aaSortingoption. For some reason it's not in his 'main' Usage pages... you can find it here though http://datatables.net/ref

关键是aaSorting选项。出于某种原因,它不在他的“主要”使用页面中......你可以在这里找到它http://datatables.net/ref

回答by Preeti

It worked for me. Thanks.. Initially I was using 'order':[2,'desc']which was not working..Correct option is aaSorting

它对我有用。谢谢..最初我正在使用'order':[2,'desc']它不起作用..正确的选项是aaSorting

eg;

例如;

$(document).ready(function() {
    $('#example1').DataTable({
        aaSorting: [[0, 'desc']]
    });
});