Volodymyr Bakhmatiuk Volodymyr Bakhmatiuk - 3 months ago 25
Javascript Question

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]);

Answer

From the documentation:.

iDataSort The column index (starting from 0!) that you wish a sort to be performed upon when this column is selected for sorting. This can be used for sorting on hidden columns for example.

Default: -1 Use automatically calculated column index

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
    ]
  } );
} );