0

I am using Datatables jQuery plugin to display data. Here is what I need:

  • When the user gives input to the search box the pagination goes away and shows all the records at once.
  • When the user clears the search box the table should go back to its original state with the pagination, etc.

Is there a way to do this? I've tried doing the following:

var $dt = $('#datatable').DataTable();
$("input[type=search]").keyup(function() {
    var search_string = $(this).val();
    if(search_string.length > 0) {
        $dt.destroy();
        $('#datatable').DataTable({
            "paging": false
        });
    } else {
        $('#datatable').DataTable();
    }           
});

But after I start typing, the Datatable reloads and the cursor is not inside the search box.

7
  • 1
    Not that familiar with codeignitor, but that shouldn't really matter - can't you just do a .destroy() on the table when you search, with new options, and then put back the original options when you remove the search? Commented Oct 30, 2016 at 13:01
  • @junkfoodjunkie Please see my edit. Commented Oct 30, 2016 at 13:39
  • ah. Right. What if you don't call destroy() first? Just set pagination to false? Commented Oct 30, 2016 at 13:49
  • how do I do that ? Without calling DataTable() Commented Oct 30, 2016 at 13:51
  • Just remove the $('#datatable').DataTable().destroy(); line, and test? Commented Oct 30, 2016 at 13:52

1 Answer 1

1

You can use

$('#datatable').DataTable().page.len(-1).draw();

to change table page length. Example:

var $dt = $('#datatable').DataTable();
$("input[type=search]").keyup(function() {
    var search_string = $(this).val();
    if(search_string.length > 0) {
        // Show all records on one page.
        $dt.page.len(-1).draw();
    } else {
        // Reset page length.
        $dt.page.len(10).draw();
    }           
});

Source: https://datatables.net/reference/api/page.len()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.