簡體   English   中英

按隱藏列對數據表進行排序

[英]Sort datatable by hidden column

我有datatableid, firstName, lastName, phone, updated領域。

問題:我只向datatable添加了四個字段(id,firstName,lastName和phone)。 Updated字段已隱藏。

問:如何排序datatable通過updated領域?

我的代碼:

   $('#table').dataTable({
        sDom: '<"top"fi>tS',
        sScrollY: ($(window).height() - 250) + "px",
        bPaginate: false,
        bDeferRender: true,
        bAutoWidth: false,
        oLanguage: {
            sInfo: "Total: _TOTAL_ entities",
            sEmptyTable: "No pending entities"
        },
        "aoColumnDefs": [
            { "iDataSort": 4, "aTargets": [ 0 ] }
        ],
        "aoColumns": [
            { "sWidth": "10%" },
            { "sWidth": "40%" },
            { "sWidth": "30%" },
            { "sWidth": "20%" },
            { "sTitle": "updated ", "bVisible":false }
        ],
        fnCreatedRow: function( nRow, aData, iDataIndex ) {
            $(nRow).attr('id', aData[0]);
        }
    });

table.fnAddData([id, firstName, lastName, phone, updated]);

從文檔:

iDataSort您希望在選擇此列進行排序時執行排序的列索引(從0開始!)。 例如,這可用於對隱藏列進行排序。

Default: -1 使用自動計算的列索引

Type: int

 // Using aoColumnDefs $(document).ready( function() { $('#example').dataTable( { "aoColumnDefs": [ { "iDataSort": 1, "aTargets": [ 0 ] } ] } ); } ); // Using aoColumns $(document).ready( function() { $('#example').dataTable( { "aoColumns": [ { "iDataSort": 1 }, null, null, null, null ] } ); } ); 

你可以在這里簡單地使用{ "iDataSort": 4 } (4是隱藏字段的索引)

var data = [
["1","john","mathew","1234",[]],
["2","Mary","rose","1234","1"],
];

添加隱藏字段並向表中添加數據

aaData: data,
aoColumns :[
        { "sTitle": "id","bSortable": false },
        { "sTitle": "firstName","bSortable": false, },
        { "sTitle": "lastName", "bSortable": false,},
        {"sTitle": "phone","bSortable": false},
        {"sTitle": "updated ", "bVisible":false },
        ]

要添加隱藏字段,請使用"bVisible":false

我在運行時對隱藏列進行排序時遇到了問題,不知道方法是否有效。 我使用以下行來通過CSS隱藏列

td:nth-of-type(2) {
display: none;
}

其中2是你的列,為你的<th class="mycolum1">分配一個類並使用jquery對它進行排序

$("#button").click(function(){
$(".mycolumn").click();
})

請原諒我,如果方法無效,但在我的情況下,它是100%可接受的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM