DataTables - Filtering and Comparing on Multiple Columns

Viewed 983

Using DataTables plugin. My Table data is correct. I want to filter that data as such:

  • Show rows in which column 4 value equals 'Ok' (This is Working)
  • Of Those rows show only the rows in which column 7 value is different from column 8 value (This is not working)

(Filter Code)

else if (input.value == "IncorrectQuantity") {
                table
                    .columns().search('')
                    .column(4).search('Ok', true, false)
                    .columns([7, 8])
                    .data()
                    .filter(function (value, index) {
                        console.log(value);
                        return value[0] != value[1] ? true : false;
                    })
                    .draw();
            }

The output I get from console.log(value) (column7 + column8 data) is: enter image description here

So I would want to not display the rows with (17-17 and 15-15). I was expecting value[0] to be the first line and value[1] the second, But no, if I do console.log(value[0]) I will get 3 and null. So I have no idea on how to actually compare the column 7 and 8 values.

Edit

Following @Frenchy's answer

                /*
                    table
                    .columns().search('')                                   //clear other searchs
                    .column(4).search('Ok', true, false)                    //search column 4 value = "Ok"
                    .flatten()                                              //reduces 2D array structures to 1D structure
                    .data()                                                 //provides access to Data
                    .filter(function (value, index) {                       //filter won't actually change which rows are displayed [https://datatables.net/reference/api/filter()]
                        console.log(value.quantity, value.quantityArrival); //value will be an object so i can access it's properties directly
                        return value.quantity != value.quantityArrival;     //value.quantity = Column 7    &&    value.quantity = Column 8
                    })
                    .draw();*/

                //SOLUTION - Custom Filter
                $.fn.dataTable.ext.search.push(
                    function (settings, data, dataIndex) {
                        console.log(data);          //Will print entire row
                        return data[7] != data[8];  //values for column 7 and 8
                    }
                );

                table
                    .columns().search('')                   //clear other searchs
                    .column(4).search('Ok', true, false)    //search column 4 value = "Ok"
                    .draw();

                $.fn.dataTable.ext.search.pop();            //apply custom filter:only display row if column7!=column8
1 Answers

following what you say, value is an array and the first value is col7, the second is col8, so, the syntax seems to be:

                .columns([7, 8]).flatten()
                .data()
                .filter(function (value, index) {
                    console.log(value[0] != value[1], index);
                    return value[0] != value[1];
                })

using custom filter

else if (input.value == "IncorrectQuantity") {
     
    $.fn.dataTable.ext.search.push(
      function(settings, data, dataIndex) {
        return data[0] != data[1];
      }
    );
    
    table
       .columns().search('')
       .column(4).search('Ok', true, false)
       .columns([7, 8]).flatten()
       .data()
       .draw();

  $.fn.dataTable.ext.search.pop();
Related