2014-01-29 64 views
9

我有datatableid, firstName, lastName, phone, updated字段。按隱藏列排序數據表

問題:我添加到datatable只有四個字段(ID,名字,姓氏和電話)。 Updated字段被隱藏。

問題:如何通過updated場排序datatable

我的代碼:

$('#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]); 
+0

你想排序表啓動嗎? – dcodesmith

+0

@dcodesmith是的。但是,如果我可以隨時進行分類,這會很好。 –

+0

好吧,表格中的更新列的位置是什麼,我知道它隱藏了,但它應該有索引 – dcodesmith

回答

8

From the documentation:。當選擇排序此列在被執行

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

如何設置排序順序:desc,asc? –

+0

試試這個..「aaSorting」:[[4,「desc」]], – sree

+2

這現在肯定不推薦使用......你應該通過HTML 5使用'data-sort'。請參閱http:// stackoverflow。com/a/28730668/1408137 – Sablefoste

2

,你可以簡單地使用{ "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

+0

如何添加隱藏字段? –

+0

我更新了我的答案檢查它 – chriz

+0

我會在一小時內檢查它 –

1

我被排序在運行時隱藏的列面臨的一個問題,不知道該方法是有效還是無效。我用下面的線通過CSS隱藏的列

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

其中2是您的專欄,將類分配給您的<th class="mycolum1">,並使用jQuery像

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

打擾一下排序,如果該方法是無效,但在我的情況下,它是100%可以接受的。