2017-03-07 106 views
0

我在Rails 4.2項目中使用DataTables 1.10。我有一個js資源文件如下:數據加載後搜索數據表

var table = $('#ticket_details').DataTable({ 
stateSave: true, 
deferRender: true, 
ajax: $('#ticket_details').data('source'), 
"columns": [ 
    { "data": "reference_number" }, 
    { "data": "location" }, 
    { "data": "title" }, 
    { "data": "ticket_category", className: "hidden-xs" }, 
    { "data": "opened_date" }, 
    { "data": "last_update", className: "hidden-sm hidden-xs" }, 
    { "data": "time", className: "hidden-xs", "orderData": 7 }, 
    { "data": "decimal_time", "searchable": false, className: "hidden-lg hidden-md hidden-sm hidden-xs hidden"}, 
    { "data": "open_or_closed", className: "hidden-lg hidden-md hidden-sm hidden-xs hidden"}, 
    { "data": "assigned_to", className: "hidden-md hidden-sm hidden-xs" }, 
    { "data": "icons", className: "hidden-sm hidden-xs" }]});  

setInterval(function() { 
table.ajax.reload(null, false); }, 60000); 

我想有表上搜索我的隱藏的列其說,一個票據是否打開或關閉。我和他們背後的一些JS兩個按鈕來打開和關閉之間切換,但我想它默認打開的門票

$('#ticket_details').dataTable({ 
    "initComplete": function(settings, json) { 
    table.columns(8).search("Open").draw(); 
    } 
}); 

$('#open_toggle').on('click', function() { 
     table.columns(8).search("Open").draw(); 
}); 

$('#closed_toggle').on('click', function() { 
     table.columns(8).search("Closed").draw(); 
}); 

然而,當加載頁面,此表顯示的一切。當然,如果我使用drawCallback()然後重新繪製表格,它只會創建一個無限循環?

回答

0

你可以做一個初步的搜索與search選項link

有些人喜歡:

var table = $('#ticket_details').DataTable({ 
    "search": { 
     "search": "Fred" 
    }, 
    stateSave: true, 
    deferRender: true, 
    ajax: $('#ticket_details').data('source'), 
    "columns": [ 
     { "data": "reference_number" }, 
     { "data": "location" }, 
     { "data": "title" }, 
     { "data": "ticket_category", className: "hidden-xs" }, 
     { "data": "opened_date" }, 
     { "data": "last_update", className: "hidden-sm hidden-xs" }, 
     { "data": "time", className: "hidden-xs", "orderData": 7 }, 
     { "data": "decimal_time", "searchable": false, className: "hidden-lg hidden-md hidden-sm hidden-xs hidden"}, 
     { "data": "open_or_closed", className: "hidden-lg hidden-md hidden-sm hidden-xs hidden"}, 
     { "data": "assigned_to", className: "hidden-md hidden-sm hidden-xs" }, 
     { "data": "icons", className: "hidden-sm hidden-xs" } 
    ] 
}); 
+0

我以前嘗試過這一點。我的問題是,切換按鈕不起作用。您的解決方案讓我意識到,我需要在搜索特定列之前將全局表搜索設置爲空字符串。有什麼辦法讓初始表定義只搜索第10列? – thenapking

+0

我不知道,也許用'columns().search()'[link](https://datatables.net/reference/api/columns().search()) – inye