Create a filter on Blank Values in Individual column filtering

Create a filter on Blank Values in Individual column filtering

kmburkekmburke Posts: 13Questions: 2Answers: 0

In individual column filtering is there a way that we can filter on blank values?

The following JSFiddle has blank values in Position, Age, Salary fields for Name='Airi Satou'. When I filter on any of those fields for blanks values it should return that row.

http://jsfiddle.net/sys_kmburke/evcfespn/

Answers

  • kmburkekmburke Posts: 13Questions: 2Answers: 0

    I still have not found a solution to this.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    In DataTables an empty string means "no filter" - i.e. no filtering is done. If you want to have "" match only data which is actually an empty string, then you could use a regular expression: .search( '^'+this.value+'$', true, false )

    You need to be careful here though, as you would also need to set a default state (probably just by trigger a change event on the inputs when you create them), as you can see in an updated fiddle here:

    http://jsfiddle.net/evcfespn/4/

    Note that there is no filter applied. Then type a character into the "Search Position" input. Then delete it. Note that only Airi Satou is now shown since that is the only row with an empty string there.

    You will also note here that using this input option there is no way to clear the filter, since "" is matching "" rather than not matching anything! You could possibly add a "clear" button next to the filter that will revert the search to a non-regex empty string. But that it up to you :-)

    Allan

  • kmburkekmburke Posts: 13Questions: 2Answers: 0

    Hello Allen,

    Your solution is working great but what would be logic of clear button to revert the search to a non-regex empty string search?

    I tried different ways but none of them are working.

    If you can update your above example with a "clear" button that would be very helpful.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    but what would be logic of clear button to revert the search to a non-regex empty string search?

    Call .search( '' ) (i.e. without any regex or without the flags to se the smart / regex states).

    Allan

This discussion has been closed.