How do I draw() the filtered result after applying column(0).filter(....)?

How do I draw() the filtered result after applying column(0).filter(....)?

MichaeldkMichaeldk Posts: 2Questions: 1Answers: 0

I'm trying to do a custom filter on a column on DataTables.
column(0).search() works fine, but .filter() does not.

In this case, it's comparing the data to a range of numbers (timestamp).
It's in typescript, since it's done in Angular 4.

    const a = new Date($event[0]).getTime();
    const b = new Date($event[1]).getTime();
    if( b != null ) {
        this.datatableElement.dtInstance.then((dtInstance: DataTables.Api) => {
           const filteredData = dtInstance.column(i).data().filter(function ( value, index ) {
                const d: number = new Date(value).getTime();
                console.log('compare', a, d, b, (a <= d && d <= b));
                return (a <= d && d <= b);
            });             
            dtInstance.draw();
        });
    }

The console.log seems to display the correct data and the right boolean.
Any suggestion on how I update the current table with the filtered result?
I don't seem to able to find any documentation on that part.

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    The filter() documentation states this:

    This method should not be confused with search() which is used to search for records in the DataTable - i.e. the filter method does not change the rows that are displayed in the DataTable.

    filter() will provide a set of data that can be used in your code but not to update the table display.

    Kevin

  • MichaeldkMichaeldk Posts: 2Questions: 1Answers: 0

    That's my point exactly. Can I take the data that the filter returns and render it in the table? Since search does not support "greater than" and Regex doesn't support (dynamic) ranges.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    You might be able to create your own search functions like this example:
    https://datatables.net/examples/api/regex.html

    Or you might need to create a search plugin as described in this blog:
    https://datatables.net/blog/2014-08-26

    Not sure which would be easiest to accomplish what you want.

    Kevin

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    Yes, you would need a custom search plug-in if you want to use logical evaluations such as greater than.

    Allan

This discussion has been closed.