0

Is there any way to programatically search a JQuery Data Table using the search box?

I currently have something like this:

$('#calibhistory_click').tab('show');

setTimeout(function() {
    $('#calibhistory').find('input[type="search"]').val("hi");
}, 5000);

This does input the text into the box however it does not search because I believe the search function only runs if it senses keyboard input via the user. Is there any way to programatically call the search function using JS/JQuery?

Thanks in advance.

Edit:

Here's how I initialize my table in my main.js

$(".dynamic-wide-table").DataTable({
        "processing": true,
        "scrollX": true,
        "scrollY": 530,
        "scrollCollapse": true,
        "lengthMenu": [
            [100, 400, 1000, 5000, -1],
            [100, 400, 1000, 5000, "All"]
        ]
    });

The problem is, there are MANY of these tables per page. How do I access the table I want?

3 Answers 3

1

Have a look at the datatables search function.

Example from the documentation:

var table = $('#example').DataTable();

// #myInput is a <input type="text"> element
$('#myInput').on( 'keyup', function () {
    table.search( this.value ).draw();
} );

So the line you're looking for is table.search( this.value ).draw();

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

5 Comments

Unfortunately I don't have an object of the table. Just the table on the page in HTML.
I'm not sure what you mean by that - are you using the jquery dataTables plugin?
regular expressions?
I have a main.js file where I create my data table object. This page contains many tables on it. However I need to access only one of these tables. If I access my object from main.js how it will have any idea which table I am talking about? By the way, I use codeiginiter which creates many tables of this one object.
For example, I am trying to do this now, $('#calibhistory').find('.dynamic-wide-table').search("hi").draw(); where calibhistory is the specific tab with the table I want.
1

You're very nearly there, just trigger the keyup event after setting the val of the input:

$('#calibhistory').find('input[type="search"]').val("grail").trigger("keyup");

Working JSFiddle.

Hope that helps.

Comments

0

Try This

$('#DataTable_Id'+'_filter input').val('your_text').trigger("keyup");

Comments

Your Answer

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