Jquery DataTables 在排序时将顺序更改为 desc

发布于 2024-09-24 03:48:22 字数 119 浏览 6 评论 0原文

我正在使用 DataTables 来显示一些数据,它效果很好,但我想稍微自定义它,但不确定如何。

我想要做的是,当用户单击列标题对该列进行排序时,我希望它最初按降序而不是升序排序。有什么办法可以做到这一点吗?

I am using DataTables to display some data and it works great but I want to customize it slightly and not sure how.

What I want to do is when a user clicks on a column heading to sort that column I want it to initially order descendingly rather than ascendingly. Is there any way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

司马昭之心 2024-10-01 03:48:22

看看这个: DataTables 排序方向控制示例

您可以执行以下操作:

$(document).ready(function() {
    $('#example').dataTable( {
        "aoColumns": [
            { "asSorting": [ "desc", "asc" ] }, //first sort desc, then asc
        ]
    } );
} );

Have a look at this: DataTables sorting direction control example

You can do something like:

$(document).ready(function() {
    $('#example').dataTable( {
        "aoColumns": [
            { "asSorting": [ "desc", "asc" ] }, //first sort desc, then asc
        ]
    } );
} );
回眸一遍 2024-10-01 03:48:22

当前版本的 DataTables (1.10) 提供了以下方法来切换此默认排序顺序,其中使用 columnDefs 下的属性 orderSequence (或 columns 但更少)灵活的)。

以下是有关 orderSequence 的文档。

"columnDefs": [
    { "orderSequence": [ "desc", "asc"], "targets": [ 1 ] },
]

正如它还提到的,您可以强制列在单击时按 DESC 或 ASC 进行排序,您的界面可能会从中受益。

就我而言,我需要在初始单击时对不确定数量的列进行降序排序,因此我决定切换示例以定位列标题的 class 名称,而不是将每列显式定义为 < code>"targets":[1],"targets":[2],...[n] 或以编程方式构建我关心的列索引数组。

您可以通过多种方式来定位列根据此处

最终结果是一个像这样的表定义:

<table><thead><tr>
    <th class='descendFirst'>DESCend when first clicked</th>
    <th>a normally sorted ASC->DESC->... column</th>
    ...
</tr></thead></table>

数据表的授权如下:

$("#table").dataTable({
    "columnDefs": [
        {"orderSequence": ["desc","asc"], "targets":"descendFirst" },
    ]
});

瞧!首先,对带有“descendFirst”类(任意选择的描述性类名)标记的 的所有列单击降序排序。

The current version of DataTables (1.10) provides the following way of switching this default sorting order with the property orderSequence under columnDefs (or columns but less flexible).

Here's the documentation on orderSequence.

"columnDefs": [
    { "orderSequence": [ "desc", "asc"], "targets": [ 1 ] },
]

As it also mentions, you may force a column to only sort when clicked as DESC or ASC which your interface may very well benefit from.

In my case, I needed to have columns descending their sort on initial click for an indeterminate number of columns so I decided to switch the example to target a column header's class name rather than explicitly defining each column as "targets":[1],"targets":[2],...[n] or programatically building an array of the indices of columns I cared about.

You can target columns multiple ways according to here.

The end result is a table definition like so:

<table><thead><tr>
    <th class='descendFirst'>DESCend when first clicked</th>
    <th>a normally sorted ASC->DESC->... column</th>
    ...
</tr></thead></table>

And Data Table empowered as such:

$("#table").dataTable({
    "columnDefs": [
        {"orderSequence": ["desc","asc"], "targets":"descendFirst" },
    ]
});

Voila! First click descending sort on all columns with a <th> marked with a class of 'descendFirst' (an arbitrarily chosen, descriptive class name).

小霸王臭丫头 2024-10-01 03:48:22

为了回应最后对空白进行排序,这就是我想到的--
(我只是讨厌首先对空白进行排序!!)

包含这些自定义排序函数

// custom sort functions
jQuery.fn.dataTableExt.oSort['text-blankslast-asc'] = function (x, y) {  
   x = (x == "") ? String.fromCharCode(255) : x;  
   y = (y == "") ? String.fromCharCode(255) : y;  
   return ((x < y) ? -1 : ((x > y) ? 1 : 0));  
};  

jQuery.fn.dataTableExt.oSort['text-blankslast-desc'] = function (x, y) {  
   x = (x == "") ? String.fromCharCode(0) : x;  
   y = (y == "") ? String.fromCharCode(0) : y;  
   return ((x < y) ? 1 : ((x > y) ? -1 : 0));  
};  

将排序标签应用到适当的列

// init example  
$('#table2').dataTable({  
   "bJQueryUI": true,  
   "aoColumns": [  
      null,  
      { "sType": "text-blankslast" },  
      { "sType": "text-blankslast" },  
      { "sType": "text-blankslast" },  
      null  
   ]  
});  

In response to sorting blanks last, here's what I came up with--
(I just HATE blanks sorting first!!)

Include these custom sort functions

// custom sort functions
jQuery.fn.dataTableExt.oSort['text-blankslast-asc'] = function (x, y) {  
   x = (x == "") ? String.fromCharCode(255) : x;  
   y = (y == "") ? String.fromCharCode(255) : y;  
   return ((x < y) ? -1 : ((x > y) ? 1 : 0));  
};  

jQuery.fn.dataTableExt.oSort['text-blankslast-desc'] = function (x, y) {  
   x = (x == "") ? String.fromCharCode(0) : x;  
   y = (y == "") ? String.fromCharCode(0) : y;  
   return ((x < y) ? 1 : ((x > y) ? -1 : 0));  
};  

Apply the sort tags to the appropriate columns

// init example  
$('#table2').dataTable({  
   "bJQueryUI": true,  
   "aoColumns": [  
      null,  
      { "sType": "text-blankslast" },  
      { "sType": "text-blankslast" },  
      { "sType": "text-blankslast" },  
      null  
   ]  
});  
掩耳倾听 2024-10-01 03:48:22

如果像戴夫和我这样的其他人正在寻找一种在所有列上设置排序顺序的方法,那么以下内容可能适合您。为了更改所有列的排序顺序,我设置了一个循环来在表实例化后更改设置:

$(document).ready(function() {
    var example_table = $('#example').dataTable();
    $.each(example_table.dataTableSettings[0].aoColumns, function(key, column) {
        column.asSorting = [ "desc", "asc" ];
    } );
} );

希望有帮助。在 jQuery 1.11.0 和 DataTables 1.10.0 上测试

If anyone else like Dave and myself is looking for a way to set the sort order on all columns, the following may work for you. In order to change the sorting order on all of the columns I set up a loop to alter the settings after the table had instantiated:

$(document).ready(function() {
    var example_table = $('#example').dataTable();
    $.each(example_table.dataTableSettings[0].aoColumns, function(key, column) {
        column.asSorting = [ "desc", "asc" ];
    } );
} );

Hope that helps. Tested on jQuery 1.11.0 and DataTables 1.10.0

硪扪都還晓 2024-10-01 03:48:22

下面的代码是先设置默认排序顺序 desc,然后对所有列设置升序。

aoColumnDefs: [{
    orderSequence: ["desc", "asc"],
    aTargets: ['_all']
}],

Below code is to set default sorting order desc first then asceding for all columns.

aoColumnDefs: [{
    orderSequence: ["desc", "asc"],
    aTargets: ['_all']
}],
请帮我爱他 2024-10-01 03:48:22

最后获得空白的唯一方法是进行一些修改(因为排序工作正常)。

不要从服务器返回空白值,而是返回类似以下内容的内容:“[空白]”

我还没有测试过它,但我很确定方括号会出现在字母数字代码之后。方括号传统上也象征着尚未完成或确认的事情。

The only way to get your blank ones last would be a bit of a hack (since the sort is working correctly).

Instead of returning blank values from your server, return something like: "[Blank]"

I haven't tested it, but I'm pretty sure square brackets will come after alphanumeric codes. Also square brackets traditionally symbolises something that hasn't been completed or confirmed yet.

中二柚 2024-10-01 03:48:22

这对我有用:

       settings = {
           aoColumnDefs: [
            {
                orderSequence: ["desc", "asc"],
                aTargets: ['_all']
            }
        ]};

        $('.dataTable').DataTable(settings);

This works for me:

       settings = {
           aoColumnDefs: [
            {
                orderSequence: ["desc", "asc"],
                aTargets: ['_all']
            }
        ]};

        $('.dataTable').DataTable(settings);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文