加载数据后搜索数据表

时间:2017-03-07 10:12:03

标签: datatables-1.10 jquery-datatables-rails

我在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()然后重新绘制表格,它只会创建一个无限循环?

1 个答案:

答案 0 :(得分: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"  }
    ]
});