0

I'm using DataTables 1.10 in a Rails 4.2 project. I have the following in a js asset file:

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

I'd like to have the table searched on my hidden column which says whether a ticket is Open or Closed. I've two buttons with some JS behind them to switch between open and closed, but I'd like it to default to open tickets

$('#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();
});

However when you load the page, the table displays everything. Surely if I used drawCallback() and then redrew the table it would just create an infinite loop?

thenapking
  • 135
  • 14

1 Answers1

2

You can do a initial search with search option link

Some like:

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"  }
    ]
});  
inye
  • 1,786
  • 1
  • 23
  • 31
  • I'd tried this before. My problem was that the toggle buttons wouldn't work. What your solution made me realise was that I needed to set the global table search to an empty string before searching the specific column. Is there any way to have the initial table definition only search column 10? – thenapking Mar 09 '17 at 10:32
  • I do not know, maybe with `columns().search()` [link](https://datatables.net/reference/api/columns().search()) – inye Mar 09 '17 at 11:38