Multiple Filters

Multiple Filters

zgoforthzgoforth Posts: 473Questions: 87Answers: 2

Link to test case: https://jsfiddle.net/BeerusDev/phncg5t7/47/

Link to relevant post: https://datatables.net/forums/discussion/comment/67893/#Comment_67893

Hello,

I have three tables, the first two I want to apply individual filters too, in the myTable1 I want the filter to only show Items when this condition is true:

if(bdayBool === "Yes"){
      return true;
      }

in my second table, I only want to show items when the following condition is true:

      if(hireBool === "Yes"){
      return true;
      }

When I apply the first filter to the first table, it works fine, but when I create a new filter search, to apply a filter to the second table. Nothing shows? So in my test case I have the second filter commented out.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 13,816Questions: 25Answers: 3,252
    $.fn.dataTable.ext.search.push(
        function( settings,searchData, data, dataIndex ) {
        var bdayBool = searchData[2];
        if ( settings.sTableId === 'myTable1' ) {
          if(bdayBool === "Yes"){
          return true;
          }
        }
    });
    

    This search plugin is going to run for every table. You are returning true for myTable1. Even though you don't specifically have a return statement for other conditions the value will be the same as false. So for the other tables it will be false - filtering out all the rows. There are lots of ways you can fix this. Here is one:

    $.fn.dataTable.ext.search.push(
        function( settings,searchData, data, dataIndex ) {
        var bdayBool = searchData[2];
        if ( settings.sTableId === 'myTable1' ) {
          if(bdayBool === "Yes"){
          return true;
          } else {
            return false;
         }
         return true;
        }
    });
    

    Kevin

  • zgoforthzgoforth Posts: 473Questions: 87Answers: 2

    This still only applies to the first table. So I am confused how I am going to have no filter applied to the third table.

    $.fn.dataTable.ext.search.push(
        function( settings,searchData, data, dataIndex ) {
        var bdayBool = searchData[2];
        var hireBool = searchData[4];
        if ( settings.sTableId === 'myTable1' ) {
          if(bdayBool === "Yes"){
          return true;
          } else {
            return false;
         }
        if ( settings.sTableId === 'myTable2' ) {
          if(hireBool === "Yes"){
          return true;
          } else {
            return false;
         }
         return true;
        }
        
    });
    
  • kthorngrenkthorngren Posts: 13,816Questions: 25Answers: 3,252

    I mistakenly put the return true on line 11 - inside the if ( settings.sTableId === 'myTable1' ) { .. }. It should be outside between lines 11 and 12. You need to do the same and move your second if statement after the first if block.

    Kevin

  • zgoforthzgoforth Posts: 473Questions: 87Answers: 2
    edited September 23

    Fixed that but still cannot get the filter for myTable 2 to work as well -.-.

    Anything after the last return true; is unreachable.

    https://jsfiddle.net/BeerusDev/phncg5t7/70/

  • kthorngrenkthorngren Posts: 13,816Questions: 25Answers: 3,252
    edited September 23 Answer ✓

    As I said you need to move it outside the if ( settings.sTableId === 'myTable1' ) { .. } block. Also for that table searchData[4] doesn't exists, ie, var hireBool = searchData[4]; will be undefined. This is the third column in that table so use searchData[2].

    https://jsfiddle.net/x4eo7fmc/

    Kevin

  • zgoforthzgoforth Posts: 473Questions: 87Answers: 2

    Oh my goodness, I had it right, I was just assuming the searchdata was pulling from the object not the columns... I just had a brain fart, thanks for the clarification Kevin.

  • kthorngrenkthorngren Posts: 13,816Questions: 25Answers: 3,252
    Answer ✓

    The search plugin docs explain the parameters. The forth has the original data for the row if you need to access data not placed in a table column.

    Kevin

  • zgoforthzgoforth Posts: 473Questions: 87Answers: 2
    edited September 23

    Thanks!

Sign In or Register to comment.