column search problem

column search problem

culterculter Posts: 102Questions: 24Answers: 0
edited September 2019 in Free community support

Hi, I have following code to filter data in one column in my (server-side processing) datatable. The column can have values 0,1 or 2, but users can see words instead of numbers, so I need to switch from words to numbers. I have following code which works, but has some bugs.

When I write e.g. 'op', it displays the rows with 'open' in this column, but when I delete these characters, the table re-draws, but still displays just rows with 'open' in this column. When I enter for example 'c', only data with 'closed' are displayed, which is correct, but when I delete this 'c', the table displays rows with 'open' values. When I enter 'abcde', still rows with 'open' are displayed :(

I'm not very familiar with javascript, thank you for any help.

$('#c6_search').on( 'keyup', function() {

        statusVal = document.getElementById('c6_search').value;

        if (statusVal == ''){
                table.search('').draw();
        } else if('open'.includes(statusVal)){
                table
                        .columns ( 6 )
                        .search ( 0 )
                        .draw();
        } else if ('closed'.includes(statusVal)){
                table
                        .columns ( 6 )
                        .search ( 1 )
                        .draw();
        } else if ('solved'.includes(statusVal)){
                table
                        .columns ( 6 )
                        .search ( 2 )
                        .draw();
}

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @culter ,

    That's not a good way to go. If the user types "s", since "s" is in "closed", it wouldn't search for "solved". The best is to use columns.render and return the correct string you want for the type "filter".

    Cheers,

    Colin

  • culterculter Posts: 102Questions: 24Answers: 0

    Thank you Colin. Please, could you be more specific how to use columns.render in this case?

  • culterculter Posts: 102Questions: 24Answers: 0

    I've solved it by modifying the 6th row to this:

    table.search('', true, false).draw();

    as described in the coment here:

    https://datatables.net/reference/api/search()

This discussion has been closed.