Multi column filter (max, min)

Multi column filter (max, min)

GlyndwrGlyndwr Posts: 122Questions: 33Answers: 1

I am trying to filter multiple columns (max, min). I found how to do this with one column and tried to adjust it for two; however, it does not work. Can someone please review this and let me know how to correct it please. The first column works and when I comment out the first column (lines 3 to 13) the second column works so it is close.

    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
            var min = parseInt( $('#minWeight').val(), 10 );
            var max = parseInt( $('#maxWeight').val(), 10 );
            var weight = parseFloat( data[5] ) || 0; // use data for the weight column

            if ( ( isNaN( min ) && isNaN( max ) ) ||
                 ( isNaN( min ) && weight <= max ) ||
                 ( min <= weight   && isNaN( max ) ) ||
                 ( min <= weight   && weight <= max ) )
            {
                return true;
            }

            var minkJ = parseInt( $('#minkJ').val(), 10 );
            var maxkJ = parseInt( $('#maxkJ').val(), 10 );
            var kJ = parseFloat( data[6] ) || 0; // use data for the kJ column

            if ( ( isNaN( minkJ ) && isNaN( maxkJ ) ) ||
                 ( isNaN( minkJ ) && kJ <= maxkJ ) ||
                 ( minkJ <= kJ   && isNaN( maxkJ ) ) ||
                 ( minkJ <= kJ   && kJ <= maxkJ ) )
            {
                return true;
            }

            return false;
        }
    );

Replies

  • colincolin Posts: 15,236Questions: 1Answers: 2,598

    Hi @Glyndwr ,

    Would you be able to create a test case for this one, that would be helpful to debug it. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,083Questions: 26Answers: 4,908
    edited October 2018

    Haven't tried it but I'm thinking what you need to do is remove the return for the weight column and use a variable instead. Then use that variable to determine if you want to return true or false for the kJ column. Something like this:

    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
            var min = parseInt( $('#minWeight').val(), 10 );
            var max = parseInt( $('#maxWeight').val(), 10 );
            var weight = parseFloat( data[5] ) || 0; // use data for the weight column
     
            var weightCol = false;
    
            if ( ( isNaN( min ) && isNaN( max ) ) ||
                 ( isNaN( min ) && weight <= max ) ||
                 ( min <= weight   && isNaN( max ) ) ||
                 ( min <= weight   && weight <= max ) )
            {
                weightCol = true;
            }
     
            var minkJ = parseInt( $('#minkJ').val(), 10 );
            var maxkJ = parseInt( $('#maxkJ').val(), 10 );
            var kJ = parseFloat( data[6] ) || 0; // use data for the kJ column
     
            if ( ( isNaN( minkJ ) && isNaN( maxkJ ) ) ||
                 ( isNaN( minkJ ) && kJ <= maxkJ ) ||
                 ( minkJ <= kJ   && isNaN( maxkJ ) ) ||
                 ( minkJ <= kJ   && kJ <= maxkJ ) )
            {
                return weightCol ? true : false;  //true if both weight col and kJ col are true 
            }
     
            return false;
        }
    );
    

    Again I haven't tried this but it should return true if both the weight and kJ columns are in the range. If you want an OR type result instead of AND then you will probably need to use another variable for the kJ column and test for this in the final return on line 29.

    If this doesn't help then, as Colin mentioned, build a simple test case so we can help you arrange your code.

    Kevin

  • GlyndwrGlyndwr Posts: 122Questions: 33Answers: 1

    Hi Colin,

    I will read up on how to create a test case for future (and bookmark) thank you.

    Hi Kevin,

    Thanks, that works a treat.

    Kind regards,

    Glyn

This discussion has been closed.