繁体   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