首页 文章

带有“asc”和“desc”的多列排序的dataTable

提问于
浏览
2

我有一个具有多列排序的dataTable,它可以工作,但我需要:

first column "asc" and second column desc -> how is this possible?

这是我的小提琴: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"

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"
        },
    }
});

谢谢和问候;)

2 回答

  • 1

    您需要使用 2D array to achieve multi-column sorting 来存档结果 .

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

    您还可以更改格式[columnIndex,“asc | desc”](例如[1,“desc”]进行排序 .

    解决方案小提琴:https://jsfiddle.net/ShirishDhotre/a3utn0ek/7/

    检查这是否有助于解决您的问题 .

  • 0

    这一个现在工作完美:)

    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"
            },
        }
    });
    

相关问题