Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort datatable by hidden column

I have datatable with id, firstName, lastName, phone, updated fields.

Problem: I add to datatable only four fields (id, firstName, lastName and phone). Updated field is hidden.

Question: how to sort datatable by updated field?

My code:

   $('#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]);
like image 844
VB_ Avatar asked Jan 29 '14 12:01

VB_


1 Answers

you can simply use { "iDataSort": 4 } here (4 is the index of your hidden field)

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

To add hidden fields and to add data to table

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

To add hidden fields use "bVisible":false

like image 101
chriz Avatar answered Sep 20 '22 12:09

chriz