【问题标题】:DataTables - How to order a column using custom condition?DataTables - 如何使用自定义条件对列进行排序?
【发布时间】:2018-10-22 17:18:38
【问题描述】:

在我的一个基于 PHP 的项目中,我需要使用自定义条件或自定义顺序而不是 DataTable 的 默认顺序(升序或降序)来订购

我有四种状态:紧急、高、中、低


我想要的自定义顺序是:

在上升过程中,它将是:低、中、高、紧急
在下降过程中,它将是:紧急、高、中、低


所以,在 DataTables 上进行搜索后,我找到了一个在 DataTables 中可以正常工作的解决方案。这里是[稍微定制以满足我的标准]:

$.fn.dataTable.ext.type.order['ticket-priority-pre'] = function ( d ) {
    switch ( d ) {
        case 'Low'      :   return 1;
        case 'Medium'   :   return 2;
        case 'High'     :   return 3;
        case 'Urgent'   :   return 4;
    }
    return 0;
};

$( document ).ready( function() {
    //initializing datatables
    $('#ticketList').DataTable({
        "paging"    :   false,
        "info"      :   false,
        "searching" :   false,
        "order": [[ 0, "desc" ]],
        "columnDefs": [ {
            "type": "ticket-priority",
            "targets": -1
        } ]
    });
});

但我得到的顺序是:

升序:高、低、中、紧急
降序:紧急、中、低、高


由于某种未知原因,LowHigh 的顺序错误。

谁能帮我解决我的代码中的问题或能够提供解决方案?

  • 谢谢

【问题讨论】:

    标签: javascript php jquery datatable


    【解决方案1】:

    阅读下一个例子:

    https://datatables.net/examples/plug-ins/sorting_auto.html

    我想,你需要这样做:

    $.fn.dataTable.ext.type.detect.unshift(
        function ( d ) {
            return (d === 'Low' || d === 'Medium' || d === 'High' || d === 'Urgent') ?
                'ticket-priority' :
                null;
        }
    );
    
    $.fn.dataTable.ext.type.order['ticket-priority-pre'] = function ( d ) {
        switch ( d ) {
            case 'Low'      :   return 1;
            case 'Medium'   :   return 2;
            case 'High'     :   return 3;
            case 'Urgent'   :   return 4;
        }
        return 0;
    };
    
    $( document ).ready( function()
    {
        // Initializing datatables.
    
        $('#ticketList').DataTable({
            "paging"    :   false,
            "info"      :   false,
            "searching" :   false,
            "order"     : [[ 0, "desc" ]],
            "columnDefs": [ {
                "type": "ticket-priority",
                "targets": -1
            } ]
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 2013-12-15
      • 1970-01-01
      • 2016-06-29
      • 2020-04-09
      • 2022-11-29
      相关资源
      最近更新 更多