Resetting per-column searches

Resetting per-column searches

stephanbstephanb Posts: 36Questions: 11Answers: 0

I've gone through several similar posts and code snippets but I still can't get this to work.
I have a few INPUT form fields to perform searches, this all works well. I also have a button (#clearall) that will reset the values of the text field, but it won't do is clear and redraw the DataTable. What am I missing in the onclick handler?

$('#clearall').on('click', function () {
    $('#namesearch, #fpmsearch, #asmsearch, #issesearch').val('');

    table.columns().every( function () {
        this
        .search('')
        .draw();
    } );    
});

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,179Questions: 26Answers: 4,923

    Looks like it should work. I made a simple test case that shows your columns().every() loop works:
    http://live.datatables.net/wosiyaya/1/edit

    Note that I moved the draw() outside the loop to execute once for more efficiency.

    Maybe you can update the test case to replicate the issue.

    Kevin

  • stephanbstephanb Posts: 36Questions: 11Answers: 0

    That example doesn't work 100% either. I works the first time because you load it with a predefined "London" search. Clicking the clear button works.

    But if you search something through the text box it doesn't clear.

  • kthorngrenkthorngren Posts: 21,179Questions: 26Answers: 4,923
    Answer ✓

    But if you search something through the text box it doesn't clear.

    Where the search text comes from doesn't matter. See this example with a search input:
    http://live.datatables.net/wosiyaya/2/edit

    The code you posted seems to work. There is something else on your page causing the column search to not clear. As I asked before please update the test case or post a link to your page so we can help debug.

    Kevin

  • stephanbstephanb Posts: 36Questions: 11Answers: 0

    Yup, fair enough. Let me figure this out. Thanks for setting up the live for me.

  • stephanbstephanb Posts: 36Questions: 11Answers: 0
    edited March 2021

    All good. I forgot to include my new onclick handler in the same ready(){} function scope where I moved all my other DataTables code. As a result it didn't know about "table" variable.

    Interesting enough it didn't complain about it in the console until I started to debug the code line by line.
    Thanks guys.

This discussion has been closed.