perform ordering after search

perform ordering after search

sunbathersunbather Posts: 8Questions: 4Answers: 0

hello

I have a datatable with two fields for searching through the datatable. On change, the filters call the following function.

function filterBoth () {
        var searchTerm = $('#trm').val().toLowerCase();
        var searchTermName = $('#trm2').val().toLowerCase();

        $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
            if(searchTerm && !searchTermName){
                if (~data[getCulumnIndexByName('col1')].toLowerCase().indexOf(searchTerm)) return true;
                if (~data[getCulumnIndexByName('col2')].toLowerCase().indexOf(searchTerm)) return true;
                if (~data[getCulumnIndexByName('col3')].toLowerCase().indexOf(searchTerm)) return true;
            }
            if(searchTermName && !searchTerm){
                if (~data[0].toLowerCase().indexOf(searchTermName)) return true;
                if (~data[getCulumnIndexByName('col4')].toLowerCase().indexOf(searchTermName)) return true;
                if (~data[getCulumnIndexByName('col5')].toLowerCase().indexOf(searchTermName)) return true;
            }
            else {
                //both
                if ((~data[getCulumnIndexByName('col6')].toLowerCase().indexOf(searchTermName) || ~data[getCulumnIndexByName('col5')].toLowerCase().indexOf(searchTermName) || ~data[0].toLowerCase().indexOf(searchTermName)) && ( ~data[getCulumnIndexByName('col1')].toLowerCase().indexOf(searchTerm) || ~data[getCulumnIndexByName('col2')].toLowerCase().indexOf(searchTerm) || ~data[getCulumnIndexByName('col3')].toLowerCase().indexOf(searchTerm))) return true;
            }
            return false;
        })
        $('#mytable').DataTable().draw();
        $.fn.dataTable.ext.search.pop();
    }

It looks for the term searched terms (two inputs). Either one, the other, or both together (on multiple columns).

When performing a search, the number of rows correctly reduces only showing the correct results. The problem is that if I try to order any of the columns, the filtering is lost. To solve the issue I wrote the following piece of code.

    ``$('#patientsTable').on( 'order.dt', function () {
        filterBoth();
    } );

Whenever an order event is fired, I call my filter function that will perform the filtering again and show the ordered, filtered, results.

My issue is the following. The given code, as it is, loops because apparently fn.dataTable.ext.search uses the ordering at some point.

How can I order what I filtered without losing the filtered results?

Thanks!

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @sunbather ,

    This example here may help. Without seeing a running example, I think the problem is because you've wrapped the filter within a function. If you make the filter generic (as in the example) then it will get applied regardless of when ordering occurs.

    Hope that helps,

    Cheers,

    Colin

  • sunbathersunbather Posts: 8Questions: 4Answers: 0

    Thank you! I've been working on a code that is not my own, and I gave for granted the fact that that was the correct way to implement the filtering. By taking it out of the function it worked as expected.
    Thank you again!

This discussion has been closed.