Can't make search function work

Can't make search function work

Javier64Javier64 Posts: 6Questions: 2Answers: 0

Hi, I'm fairly new to Datatables, and I'm having an issue when I try to make a search/filter on some columns. Any help would be appreciated.

This is the table (the columns are loaded before in var columnDefs), followed by the search function:

   dtable = $("#TablaMinutas")
        .DataTable(
          {
              ajax: {
                  url: minutaAjax.configuracion.URLSERVICE + "escriturasPresentadas/consultaMinutas",
                  timeout: minutaAjax.configuracion.TIMEOUT,
                  type: "POST",
                  dataType: "json",
                  data: function ( d ) {
                      return JSON.stringify(d);
                   },     
                   beforeSend: function (request) {
                       request.setRequestHeader("token", minutaAjax.getToken());
                   },                  
                  contentType: minutaAjax.configuracion.CONTENTTYPE,
                  error: function (xhr, error, thrown) {                
                      
                      minutaAjax.error(xhr, error, thrown);
                      $('#TablaMinutas_processing').hide();                       
                      
                  }
              },              
              serverSide: true,
              searching: false,
              processing: true,
              deferRender: true,
              fnServerParams: function (aoData) {

                  aoData.cuit = $('#formPresentar').find('input[name="cuit"]').val();
                  aoData.anio = $('#formPresentar').find('input[name="anio"]').val();   
                  aoData.numero = $('#formPresentar').find('input[name="numero"]').val();           
              },      
              order: [[ 1, "asc" ]],
            columnDefs: columnDefs,
           
        select: {
            style: 'single'
        },
        pagingType: 'full'
    });

    $('.filter-button').on('click', function () {
        //clear global search values
        dtable.search('');
        $('.filter').each(function(){ 
        if(this.value.length){
            dtable.column($(this).attr('data-column-index')).search(this.value);
        }
        });

        dtable.draw();
    });


This is the search button in jsp:

<div>

                        <label for="anio" class="label">Año Escritura</label>
                        <input placeholder="Año Escritura" type="number" name="anio" id="anioEscritura" class='filter' data-column-index='1'/>
            </div>
            <div>

                <label for="numero" class="label">Nro Escritura</label>
                <input placeholder="Número Escritura" type="number" name="numero" id="numeroEscritura" class='filter' data-column-index='2'/>

            </div>
            <div>
                <input type="button" value="Buscar" class="filter-button"/>
            </div>

From what I could gather, I think the problem resides in the table data beeing loaded dinamically. When I try to debug the code,

dtable.column

returns "anonymous"

Answers

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    Are you able to perform the search by putting a value in only one input?

    With the code you have the search is essentially an "and" search between the two columns. If you want "or" between the two columns then you will probably need to create a Search Plugin to perform the "or" operation.

    Using dtable.search(''); will clear the global search but not the column search. If you want to clear the column search then use something like this: dtable.columns([1,2]).search('');

    If this doesn't help maybe yu can put together a test case showing what you are trying to do with more details of how you want the search to perform:
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • Javier64Javier64 Posts: 6Questions: 2Answers: 0
    edited February 2018

    Kevin, thanks for your reply. The search should be with an AND, since I need both values to be searched. Here's an example of what I'm trying to accomplish:

    http://jsfiddle.net/yLxowxen/20/

    But in my case, the different values should be mandatory. Also in my case the table data is filled dinamically.

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946
    edited February 2018

    It seems to work. If I enter the following values two results are shown - which is what I would expect:

    Name: a
    Position: soft
    Age: 8
    

    Is the search not working as you desire? Maybe you can provide exact steps to show the issue.

    Kevin

  • Javier64Javier64 Posts: 6Questions: 2Answers: 0

    Yes, the example I provided works. It's what I'm trying to accomplish, but when I apply it to my case, it doesn't do anything. As I said, when I debug the code, dtable.column returns "anonymous", which I don't know if it should. That's why I thought it may be related to the table being filled dinamically.

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    Sorry, missed that you had serverSide enabled. Your server code will need to handle the column searches. What are you using for your server side script?

    Kevin

  • Javier64Javier64 Posts: 6Questions: 2Answers: 0

    Mmm I see. I'm working with Java 7

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    Your server side script will need the ability to process the parameters sent via server side processing. More info can be found here:
    https://datatables.net/manual/server-side

    Kevin

This discussion has been closed.