Individual column filtering

Individual column filtering

horus1613horus1613 Posts: 19Questions: 6Answers: 0
edited July 2018 in Free community support

Hi.
I want to do a select filter columns: 4,25,31.
My code:

    initComplete: function () {
        var ik = 0;
        while (ik <= 31) {
            
            if ((ik == 4) || (ik == 25) || (ik == 31)) {
                var column = this.api().column(ik);     
            console.log(column.search())        
            var select = $('<select><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                            var val = $.fn.dataTable.util.escapeRegex(
                                $(this).val()
                            );
     
                            column
                                .search( val ? '^'+val+'$' : '', true, false )
                                .draw();
                } );
                column.data().unique().sort().each( function ( d, j ) {
                    if( column.search() == d){
                        select.append( '<option selected value="'+d+'">'+d+'</option>' );
                    }
                    else{
                    select.append( '<option value="'+d+'">'+d+'</option>' );
                    }
                } );    
        }
        ik++;
        }
            },

But it doesn't work if two or more columns are selected at the same time...
I'm depressed...
Help me, please.
PS: and i don't know how to do so that these "selects" don't get into excel.

This question has an accepted answers - jump to answer

Answers

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

    Hi @horus1613 ,

    I don't understand what you mean by "it doesn't work if two or more columns are selected at the same time". Those searches will be ANDed together, so both conditions will have to match.

    Cheers,

    Colin

  • horus1613horus1613 Posts: 19Questions: 6Answers: 0

    @colin
    if I select 2 "select", an empty table is always issued

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

    @horus1613 , is that because no rows match those two search terms? As I said before, they're ANDed together.

    It's probably worthwhile if you create a link to a running test case showing the issue so we can offer some help. 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

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

    Here's a modified version of that fiddle which works. The way you were using the columns variable meant you were always searching on the final column.

    C

  • horus1613horus1613 Posts: 19Questions: 6Answers: 0

    @colin
    thanks, it works!

This discussion has been closed.