How to reset values in individual column searching (text inputs) at a button click

How to reset values in individual column searching (text inputs) at a button click

NewcomsasNewcomsas Posts: 2Questions: 1Answers: 0

I would like to be able to reset all the values in individual column search inputs at the click on a custom 'Reset' button in the table. Could you suggest me how to obtain this behaviour ? TIA

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    columns().search() with a blank ("") value will clear the column search. Make sure to use draw(). Also you will need to clear the search inputs, Datatables doesn't do this.

    Kevin

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

    Hi @Newcomsas ,

    Those column server inputs aren't part of DataTables, they'll be HTML elements that you added yourself, so to clear, just use standard JS /Jquery code.

    Cheers,

    Colin

  • NewcomsasNewcomsas Posts: 2Questions: 1Answers: 0

    Here is a working code. Every other combination did not work.
    I have to clear all the textboxes via Jquery and then call the draw() method for every column and the general search textbox.

       buttons: [
          {
            text: 'Reset',
            className:'btn btn-warning',
            header: true,
            action: function ( e, dt, node, config ) {
    
    
                $("input[type='text']").each(function () {  
                    $(this).val('');  
                })
    
                dt.columns().every( function () {
                    var column = this;
                    column
                            .search( '' )
                            .draw();
                } );
    
                dt.search('').draw();
    
            }
          }
       ],
    
  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    I would suggest removing the .draw() from this:

                 column
                         .search( '' )
                         .draw();
    

    Since you are doing the draw here: dt.search('').draw();

    This might be more efficient (or at least more readable) than using the every loop:
    dt.columns().search('')

    Kevin

This discussion has been closed.