Custom filtering doesn't work with 0

Custom filtering doesn't work with 0

FlorianeFloriane Posts: 22Questions: 6Answers: 0

Hi,

I am integrating filters on my dataTable. On your exemple here : http://datatables.net/examples/plug-ins/range_filtering.html
When you insert "0" on "Maximum age:", filter doesn't work. Do you know this bug ?

Thank you,
Floriane.

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    What do you mean by " filter doesn't work"? It seems to me that "0" is just ignored, which is the behaviour I would expect.

  • FlorianeFloriane Posts: 22Questions: 6Answers: 0

    Yes, but if I want to search a value equals to 0 I can't, is it right ?
    Why 0 is ignored ? Where can I change this behaviour ?

  • FlorianeFloriane Posts: 22Questions: 6Answers: 0

    I can't find where this behavior is in DataTable.js ... Does someone can help me please?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited June 2014

    You are looking at a range filter. It finds values between x and y. For values equal to x, use a different filter.

  • FlorianeFloriane Posts: 22Questions: 6Answers: 0

    I use the range filter and an equal filter. None of them works with value "0"... Like you said, the "0" is ignored.

  • FlorianeFloriane Posts: 22Questions: 6Answers: 0

    Nobody knows where I can find this behavior ?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    What do you mean by "an equal filter"? It would make more sense to show us your code so we can see what you are doing.

  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    Answer ✓

    Its a bug in the example rather than in DataTables. The filter does run, but the data isn't being correctly filtered out.

    If you update the custom filter to the following code it does work:

    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
            var min = parseInt( $('#min').val(), 10 );
            var max = parseInt( $('#max').val(), 10 );
            var age = parseFloat( data[3] ) || 0; // use data for the age column
    
            if ( ( isNaN( min ) && isNaN( max ) ) ||
                 ( isNaN( min ) && age <= max ) ||
                 ( min <= age   && isNaN( max ) ) ||
                 ( min <= age   && age <= max ) )
            {
                return true;
            }
            return false;
        }
    );
    

    I'll update the example for 1.10.1.

    Thanks,
    Allan

  • FlorianeFloriane Posts: 22Questions: 6Answers: 0

    Ok, everything works fine.

    Thanks Allan !

This discussion has been closed.