2

How can I set the default text value for the input field for the columnfilter plugin for the datatables api?

 $(document).ready(function () {

         var t1 = $( '#test').dataTable({

         }).columnFilter();

My table has several columns and I have tried setting the input box to have a default serach value as:

 $("input:eq(6)").val("myDefaultValue");

But this just displays myDefultValue in the search box , but doe not apply the filter.

I also tried triggering change and keydown, but both fail.

I am testing in Chrome, any ideas?

2 Answers 2

3

Sorry for my first post. I red wrong the api doc.

See this page : http://datatables.net/reference/api/column().search()

You need to select the column and then use the search() method.

$( '#test')
    .columns(6) // to select the sixth column
    .search('myDefaultValue')
    .draw();

It should work better.

I delete my first post.

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

1 Comment

This works also! Thanks, and as you point it is a cleaner solution, which directly exploits the existing API.
0

UPDATE: Following the idea from Msieur Toph to try to trigger the search event, I have found the solution (by looking at the columnfilter source code), which is to trigger the keyup event.

$("input:eq(6)").val("myDefaultVal").trigger('keyup');

1 Comment

As I said in the new post I have made, he should better use the search function on the column. Your solution propably works (I guess it does the exact same thing), but it is kind of dirty, no ? I think it is better to use the native API methods.

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.