Fill search box on button click

Fill search box on button click

pfiorellapfiorella Posts: 8Questions: 2Answers: 0
edited July 2014 in Free community support

Maybe I'm making this harder than it is but how do you fill the search box with a term on a button click?

Right now I use a pretty ugly 'hack' to get it to work:

$(document).ready( function () {   
    var table = $('#example').DataTable();
     $('#btnSearch').click(function (){
           table.destroy();
           var tbl = $('#example').DataTable({
               "oSearch": {"sSearch": "Initial search"}
           });
       });
} );

Basically on my button click I destroy the current table and initialize a new one with an initial search term. There has to be an easier way though.

This question has an accepted answers - jump to answer

Answers

  • DaimianDaimian Posts: 62Questions: 1Answers: 15
    Answer ✓

    Use search(). Example: table.api().search("search value").draw()

  • pfiorellapfiorella Posts: 8Questions: 2Answers: 0

    Wow. That was easy. Small thing, I had to remove the .api() so it looks like:

    $(document).ready( function () {  
        var table = $('#example').DataTable();
        $('#btnSearch').click(function (){
               table.search("search value").draw();
           });
    } );
    

    Now, is there a way to have that search value be visible in the search field? Right now it searches the table but the field is blank.

  • DaimianDaimian Posts: 62Questions: 1Answers: 15

    If you are using the default Datatable search box you shouldn't need to.

    Working example: http://live.datatables.net/dakosaf/1/edit

  • pfiorellapfiorella Posts: 8Questions: 2Answers: 0

    You're right, I was using an older version of the datatables.js.

    Thanks!

  • DaimianDaimian Posts: 62Questions: 1Answers: 15

    No problem :)

This discussion has been closed.