Problems with 2 datatables on the same page and filtering

Problems with 2 datatables on the same page and filtering

informaxinformax Posts: 4Questions: 2Answers: 0

Hello, i have 2 different datatables on the same page and i have 2 diferent forms to filter the data of each datatable.
I`m using the $.fn.dataTable.ext.search.push() method, and it works fine when only one table. What should i do to make it work with 2 datatables?. Each of one has his own name and are attached to different html tables. Im using editor too.

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,769

    You will probably need to create a search API as described in the Alphabet input search - Part III blog.

    Kevin

  • informaxinformax Posts: 4Questions: 2Answers: 0
    edited January 2017

    Thanks, but thats not my problem. I allready have the search function attached to the $.fn.dataTable.ext.search.push() event.
    Then problem is, i need different search functions for each datatable and when i have both datatables, the search function doesnt works

    `$.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
    var minTemp = $('#fechaInicio').val();
    var temp = minTemp.split('/');
    var min = new Date(temp[2], temp[1],temp[0],0,0,0,0);

                    var maxTemp =  $('#fechaFin').val();
                    temp = maxTemp.split('/');
                    var max = new Date(temp[2], temp[1],temp[0],0,0,0,0);
    
                    var dateTemp =  data[3];
                    temp = dateTemp.split('/');
                    var date = new Date(temp[2], temp[1],temp[0],0,0,0,0);
    
                    var employee = $('#employeeSelect option:selected').html();
                    var idEmployee = $('#employeeSelect').val();
    
                    if ( ( isNaN( min ) && isNaN( max ) &&  idEmployee == 0 ) ||
                         ( isNaN( min ) && date <= max ) ||
                         ( min <= date   && isNaN( max ) ) ||
                         ( min <= date   && date <= max ) ||
                         ( employee == data[0]))
                    {
                        return true;
                    }
                    return false;
            }
        );`
    
  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    You've found the major flaw in how the DataTables search plug-ins work at the moment. You need a bit of a hack

    function ( settings, data, dataIndex ) {
      if ( settings.nTable.id !== 'myId' ) {
        return true;
      }
      ...
    }
    

    That will let you check in the function for the table id, and thus if that function should apply to the specified DataTable or not. So you need to have similar functions for your other tables.

    A pain I know. That is something that will be addressed in the next major version.

    Allan

This discussion has been closed.