Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dataTables HTML5 data-search cell attribute does not work

Basically i'm trying to do something like in this example

https://datatables.net/examples/advanced_init/html5-data-attributes.html

My code is below

var dTable = $('#ajaxresults').DataTable({
    "order": [[1, "desc" ]],
    "sPaginationType": "full_numbers",
    "ajax": "/users.json",
    createdRow: function(row, data, index) {
        if(data[6]=="Special") {
            $(row).find('td').eq(0).attr("data-search", "Special");
        }
    }
});

However once the dataset is loaded on page and i'm trying to search for Special nothing returns but if i view it in dev tools i can clearly see the data-search attribute being set to the column, from what i've read i think i'm adding the data-search column attribute after the initialization and that's why the search would not work ? but i've searched the entire documentation and i don't see other ways to implement this does anyone have any ideas on this ?

P.S. i forgot to tell that the "special" word will not appear in any column just added as data-search attribute to some users, so it's not really visible but should be searchable trough the data-search attribute ..

like image 827
Claudiu D. Avatar asked May 08 '16 03:05

Claudiu D.


1 Answers

The HTML5 data-* attributes works if all cells in a column have the data-* attribute at the time the table is initialised. You inject data-search after each row and all its cells is inserted - thats why data-search doesnt work in this case.

You could use a render method instead.

columnDefs : [
  { targets: [0], 
    render: function ( data, type, full, meta ) {
       if (type === 'filter') {
          return full[6] == "Special" ? "Special" : data
       } else {
          return data
       }
    }
  }
]

This will do the same, returning "Special" for col#0 when the user is typing in the searchbox, and if col#6 == "Special".

  • https://datatables.net/manual/data/orthogonal-data
  • https://datatables.net/reference/option/columns.render
like image 163
davidkonrad Avatar answered Sep 18 '22 23:09

davidkonrad