ColumnFiltering

ColumnFiltering

isaacdvoryisaacdvory Posts: 5Questions: 2Answers: 0

Hi,

I've activated columnFiltering , as seen here: https://datatables.net/extensions/fixedheader/examples/options/columnFiltering.html

But I activated it for only some of the columns by adding class to the columns I want and then this JS:

$('#items-table thead tr:eq(0) th.filterable').each(function (i) {

my issue is that while the filter works, it does not find any results... I tried playing with it and found that if the first column is part of the "filterable" class, everything work fine, if it doesn't, there are no results for none of the columns...

No results:
<th>Image</th>
<th class="filterable">Type</th>
<th class="filterable">Colour</th>
<th class="filterable">Size</th>

With results:

            <th class="filterable">Image</th>
            <th class="filterable">Type</th>
            <th class="filterable">Colour</th>
            <th class="filterable">Size</th>

Answers

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    each(function (i)

    The i is the loop counter which will result in the column index for this line:
    if ( table.column(i).search() !== this.value ) {

    One option is to loop through all the columns using each() and in the loop check for the column having the class filterable. If it does then build the search input. This way i is both the loop index and column index.

    There might be other ways to do this. Also I believe this has been asked before and you might find some threads with other answers.

    Kevin

  • isaacdvoryisaacdvory Posts: 5Questions: 2Answers: 0

    Thanks!
    So I removed "filtered" from this:
    $('#items-table thead tr:eq(0) th.filterable').each(function (i) {

    and put instead
    if ($(this).hasClass('filterable'))

    and now it works!

    I tried searching for a way to activate the filter only on specific columns , but couldn't find how... only found results that talk about the "searchable" option, not column filters.

    thanks for your help

This discussion has been closed.