columns([0, 1]).search filters everything

columns([0, 1]).search filters everything

glimpsed_chaosglimpsed_chaos Posts: 113Questions: 22Answers: 4

I built check box filter to allow users to toggle see normal items (value of 0) and abnormal ( value greater than 0). These values span across several columns.

If all columns for a row have a value of 0 then it should be filtered out when the check box is checked. I can get it to work for 1 column but not multiple columns. Instead it filters all the rows out.

https://datatables.net/reference/api/columns().search() shows that this should work -

table.columns( [0, 1] ).search( 'Fred' ).draw();

In my code, I have done similar but cannot get it function right. It filters everything out.

//This works 
table.column(2).search(_checked, true, false, false).draw(false);
       
//This works
table.columns([1]).search(_checked, true, false, false).draw(false);
       
//This does not work - 
table.columns([1,2]).search(_checked, true, false, false).draw(false);

http://live.datatables.net/cesayede/2/

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586
    Answer ✓

    Hi @glimpsed_chaos ,

    It's not working because

    table.columns([1,2,3,4]).search('1').draw();
    

    will only return rows where all four columns contain a '1'. It's the same as saying:

    table.columns(1).search('1').draw();
    table.columns(2).search('1').draw();
    table.columns(3).search('1').draw();
    table.columns(4).search('1').draw();
    

    To do an OR search across the columns, you need a custom search plugin. Kevin did a good one here from this thread.

    Cheers,

    Colin

  • glimpsed_chaosglimpsed_chaos Posts: 113Questions: 22Answers: 4

    Got it! Thank you for your time.

  • glimpsed_chaosglimpsed_chaos Posts: 113Questions: 22Answers: 4

    This works now. I had to swap some of the logic from Kevin's example. But now I get all 3 rows as expected when I click the checkbox if any column (1 - 4) has a value greater than 0 in a cell.

    http://live.datatables.net/cesayede/4/

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    Excellent, glad all working.

This discussion has been closed.