Custom filter, search in a type option field and type input fields?

Custom filter, search in a type option field and type input fields?

cris19ncris19n Posts: 55Questions: 18Answers: 0

I am doing a custom search, a filter is of the option type and two more of the input type are entered, how do I do the initial data filter with the option field that brings a default value (that the user can change by drop-down list), and that if another option is selected or if something is written in the two input fields, start searching.

How can I do it by following the following example of datatables?

/* Custom filtering function which will search data in column four between two values */

$.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;
    }
);

$(document).ready(function() {
    var table = $('#example').DataTable();

    // Event listener to the two range filtering inputs to redraw on input
    $('#min, #max').keyup( function() {
        table.draw();
    } );
} );

these are my fields:

     <div class="row">

                            <div class="col-sm-2">
                                <div class="col-sm-2">
                                    <label for="id_ano_search">AÑO:</label>
                                </div>

                                    <select class="form-control input-sm" id="id_ano_search2" require>

                                        <?php
                                            /**AÑO DESDE EL QUE SE PUEDE INICIAR BÚSQUEDA EN LA BD */
                                            $anoAnt=2019;
                                            /**ULTIMO AÑO DONDE O AÑO ACTUAL DE LAS OPCIONES */
                                            $anoAct=date("Y");

                                            echo '<option value="'.$anoAct.'">'.$anoAct.'</option>';

                                            $d1 = DateTime::createFromFormat('Y', $anoAnt);
                                            $d2 = DateTime::createFromFormat('Y', $anoAct);

                                            $interval = $d1->diff($d2);
                                            $i2=$interval->format('%Y');

                                            if ($i2>0) {

                                                for ($i = 1; $i <= $i2; $i++) {
                                                    $anoAct=$anoAct-1;
                                                    echo '<option value="'.$anoAct.'">'.$anoAct.'</option>';
                                                }

                                            }

                                        ?>

                                    </select>
                            </div>

                            <div class="col-sm-2">
                                <div class="col-sm-2">
                                    <label for="id_lote_search">LOTE:</label>
                                </div>
                                    <input type="text" class="form-control input-sm" id="valor_a_comparar" placeholder="INGRESE LOTE">
                            </div>

                            <div class="col-sm-2">
                                <div class="col-sm-2">
                                    <label for="id_viaje_search">VIAJE:</label>
                                </div>
                                    <input type="text" class="form-control input-sm" id="valor_a_comparar" placeholder="INGRESE VIAJE">
                            </div>



                        </div>

this is the id of my table:
id="search_mp"

I want to search column 1,3,5

Answers

  • rf1234rf1234 Posts: 3,144Questions: 92Answers: 433

    I am doing a custom search, a filter is of the option type and two more of the input type are entered, how do I do the initial data filter with the option field that brings a default value (that the user can change by drop-down list), and that if another option is selected or if something is written in the two input fields, start searching.

    I don't understand this very long sentence. I had it translated into my native language as well and I still didn't get it. Can you explain what you mean please.

This discussion has been closed.