Column search by conditional integer range

Column search by conditional integer range

rdmrdm Posts: 194Questions: 55Answers: 4

Here is a simple question about the table.column(x).search(y) function. How do you filter by a range of integers?

For example:

// if age.val() == "Yes" then search 18 or above.
// if age.val() == "No" then search 17 or below
// if age.val() == "" then no filter

t.column(5).search(/* search integer range depending on age.val() */);

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,450Questions: 26Answers: 4,975
    Answer ✓

    Maybe the Range Search example will help get you started.

    Kevin

  • rdmrdm Posts: 194Questions: 55Answers: 4

    @kthorngren -- I independently found that page but I'm having difficulty getting the $.fn.dataTable.ext.search.push function to trigger on age.change()

  • kthorngrenkthorngren Posts: 21,450Questions: 26Answers: 4,975

    Maybe you can post a test case with what you have and someone can help troubleshoot.

    Kevin

  • rdmrdm Posts: 194Questions: 55Answers: 4

    Here is what is meant to trigger the column filter:

        age.change(function () {
            // trigger age column to filter based on yes/no/all selection.
    
            var t = $(".display").DataTable();
            t.draw();
        });
    

    And this is the function inspired from here.

    I added the console.log statement and see no evidence this function is triggered -- either in the console screen or in the resulting table. The example function is based on a min/max model, whereas mine is based on a greater or less than criterion value, which explains the if logic.

    The problem is that this function does not trigger at all.

    $.fn.dataTable.ext.search.push(
                    function (settings, data, dataIndex) {
    
                        console.log("triggered");
                        var min = 0;
                        var max = 50;
    
                        if (age.val() === "Yes") {
                            min = 18;
                            max = 50;
                        }
                        if (age.val() === "No") {
                            min = 0;
                            max = 17;
                        }
                        var ageCol = parseFloat( data[5] ) || 0; // use data for the age column
     
                        if ( ( isNaN( min ) && isNaN( max ) ) ||
                            ( isNaN( min ) && age <= max ) ||
                            (min <= ageCol   && isNaN( max ) ) ||
                            (min <= ageCol   && age <= max ) )
                        {
                            return true;
                        }
                        if (age.val().length === 0) {
                            return false;
                        }
                        return false;
                    }
                );
    
  • rdmrdm Posts: 194Questions: 55Answers: 4
    Answer ✓

    Disregard. It all stemmed from the lack of a hash sign in an earlier variable declaration.

    Now that I put in the missing hash sign, the function triggers and works as intended.

    var age = $("#Age);
    
  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin

    Thanks for posting back - great to hear you've got it working now.

    Allan

This discussion has been closed.