3

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 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

5 Comments

value[index][0] is undefined. Also I would like to point that value[0] has actually both values from column 7 and 8 (row 0), and value [1] from row 1 and so on. But I don´t know how to compare the values if they are both inside value[n]. Seems DataTables would have something to iterate the data
@HenriquePombo i haveadded flatten(), could you try?
i am not sure you could redraw the datatable you have to use a custom filter
Custom Filter did the job. Regarding .filter(){} after flatten, it's possible to access value's properties directly e.g: value.quantity (my column7); but I have no ideia how to use that to actually change the display the data in the table (without the custom filter, that is). I have edited my answer to apply the help you provided (deleting some useless stuff I had added before), Thank You for the help
glad to help, so i have discovered flatten() too!! but you have right you could avoid with the custom filter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.