jquery datatable按图像排序

时间:2017-06-26 21:04:53

标签: jquery datatables

我有一个绑定ok的jquery数据表 如果status = 0,它的数据源有一列(状态)我显示红色图像 如果1显示图像黄色,如果2显示图像绿色 一切都好,但我需要能够对该列进行排序(状态列值)

     var table;var columnArrayToHide = [
3,4,5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,16,17, 18, 19, 20,21,22,23,24,25,26,27,28,29,30
    ]; 
    $(document).ready(function () {
        BindDataTable();
    });
    function BindDataTable() {
        $.ajax({
            type: "Post",
            url: 'Query.asmx/LoadCarGrid',
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            success: function (data) {
                table = $('#table').DataTable({
               var myData = JSON.parse(data.d);
                myData.sort(function (a, b) {
                    if (a.carStatus < b.carStatus) return -1;
                    if (a.carStatus > b.carStatus) return 1;
                    return 0;
                });
                    "dom": '<"top"i>rt ',
                    "bInfo" : false,
                    "aaData": myData ,
                    "columns": [

                    {"data":null},
                     { "data": "CarName" }, { "data": null },
                     { "data": "CarGroups" }, { "data": "CarColor" }, { "data": "Departement" },
                      { "data": "CarModels" },

                { "data": "id" }, { "data": "CarTypeName" }, { "data": "CarStatus" }],
                    "columnDefs": [
                        {
                            "targets": columnArrayToHide,
                            "visible": false,
                            "searchable": true
                        },{
                            "targets": 0,

                            "data": null,
                            "defaultContent":
                                '   <input type="checkbox" class="Check"/>' 

                        }, {
                            "targets": 2,
                            "data": null,

                            render: function (data, type, row) {
                                switch (data.CarStatus) {
                                    case 2: return ' <img  class="img2"    src="Images/Green_mark.png" />'
                                        ; break;
                                    case 1: return  ' <img   class="img1"   src="Images/yellow_mark.png" />'; break;
                                    case 0: return  ' <img   class="img0"   src="Images/Red_mark.png" />'
                                        ; break;


                                }


                            }

                        }
                    ] 

    });

    },
    error: function (err) {
        //  
    }
    })
    }

1 个答案:

答案 0 :(得分:0)

按如下方式对数据进行排序:

data = data.sort(function(a, b) 
{
    if( a.carStatus<b.carStatus) return -1;
    if( a.carStatus>b.carStatus) return 1;
    return 0;
});