简体   繁体   English

具有“asc”和“desc”多列排序的数据表

[英]dataTable with multi-column ordering with “asc” and “desc”

i have a dataTable with multi-column ordering and it works but I need:我有一个多列排序的数据表,它可以工作,但我需要:

first column "asc" and second column desc -> how is this possible?第一列“asc”和第二列 desc -> 这怎么可能?

here is my fiddle: https://jsfiddle.net/zukii/Lucq6vc5/28/这是我的小提琴: https : //jsfiddle.net/zukii/Lucq6vc5/28/

in this fiddle the column "Rating" is automatic default sorting "asc" and then the column "Price" should be automatic "desc"在这个小提琴中,“评级”列是自动默认排序“asc”,然后“价格”列应该是自动“desc”

var mytable = $('table.dt-tarif').dataTable({
    "paging":   false,
    "info":     false,
    "searching": false,
    "order": [[ 3, "desc" ]],

    "aoColumnDefs": [
        {
            "bSortable": false,
            "aTargets": [0]
        },
        { 
            "type": "currency", targets: 3 
        },
        {
            targets: [ 3 ],
            orderData: [3, 4]
        }
    ],

    "language": {
        "lengthMenu": "Zeige _MENU_",
        "zeroRecords": "Keine Entwürfe vorhanden!",
        "info": "Seite _PAGE_ von _PAGES_",
        "infoEmpty": "Es konnte kein Entwurf gefunden werden.",
        "infoFiltered": "",
        "search": " ",
        "paginate": {
            "first": "Erste",
            "last": "Letzte",
            "next": "Vor",
            "previous": "Zurück"
        },
    }
});

thanks and greetings ;)谢谢和问候 ;)

You needs to use a 2D array to achieve multi-column sorting to archive the result.您需要使用一个二维数组来实现多列排序来归档结果。

var table = $('table.dataTable').DataTable();
table
    .order( [ 3, 'asc' ],[ 4, 'desc' ] )
    .draw();

further you can change the format [ columnIndex, "asc|desc" ] (eg [ 1, "desc" ] for sorting .您还可以更改格式 [ columnIndex, "asc|desc" ] (例如 [ 1, "desc" ] 用于排序。

Solution fiddle: https://jsfiddle.net/ShirishDhotre/a3utn0ek/7/解决方案: https : //jsfiddle.net/ShirishDhotre/a3utn0ek/7/

Check if this help to close your issue.检查这是否有助于解决您的问题。

This one is working perfect now :)这个现在工作完美:)

https://jsfiddle.net/zukii/Lucq6vc5/37/ https://jsfiddle.net/zukii/Lucq6vc5/37/

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"currency-pre": function ( a ) {
    a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
    return parseFloat( a );
},

"currency-asc": function ( a, b ) {
    return a - b;
},

"currency-desc": function ( a, b ) {
    return b - a;
}
} );

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "currency": function ( a ) {
     var x = a.replace(",", ".").replace("€", "");
     return parseFloat( x );
  }});

var mytable = $('table.dt-tarif').dataTable({
    "paging":   false,
    "info":     false,
    "searching": false,
    "order": [[ 3, "desc" ]],

    "aoColumnDefs": [
        {
            "bSortable": false,
            "aTargets": [0]
        },
        { 
            "type": "currency", targets: 3 
        },
        {
            targets: [ 3 ],
            orderData: [3, 4]
        }
    ],

    "language": {
        "lengthMenu": "Zeige _MENU_",
        "zeroRecords": "Keine Entwürfe vorhanden!",
        "info": "Seite _PAGE_ von _PAGES_",
        "infoEmpty": "Es konnte kein Entwurf gefunden werden.",
        "infoFiltered": "",
        "search": " ",
        "paginate": {
            "first": "Erste",
            "last": "Letzte",
            "next": "Vor",
            "previous": "Zurück"
        },
    }
});

you can use:您可以使用:
"order": [ [ 1, "asc" ], [ 3, "desc" ] ] , "顺序": [ [ 1, "asc" ], [ 3, "desc" ] ] ,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM