12

I'm using datatable 1.10.15 with server side processing. I want to set value for a column search when table initializes. I tried this but without success:

$('#dataTable').DataTable({
    ...
    columns:[
        ....
        {name:'name', search:{value:'q'}}
        ....
    ]
});
1
  • 1
    According to the datatable documentation, you can use the "searchCols" parameter. See my answer below Commented Dec 5, 2019 at 21:01

4 Answers 4

19

You can try:

$('#example').dataTable( {
  "search": {
    "search": "Fred"
  }
});

It will initialize the datatable with Fred in search column.

Working Fiddle

Reference

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

6 Comments

Thanks but it's a global search. i want to make search only for certain column
Make your query accordingly
@MayankPandeyzyes I see it filling the textbox, but the data is not filtered. I got it to work by creating the datatable with "deferLoading:true" and then setting the search box, then using DataTable.draw() to make the ajax call with the search param. Works great.
You can create a function to render the server-side datatable and pass the initial value at the time of page load and write your query by considering this value.
This is not the exact answer for the question, but it is what I was searching. Server side pagination also works. Thanks
|
7

Here can be used SearchCols.

Basically the same as the search option, but in this case for individual columns, rather than the global filter, this option defined the filtering to apply to the table during initialisation.

The array must be of the same size as the number of columns, and each element be an object with the parameters search and escapeRegex (the latter is optional). 'null' is also accepted and the default will be used.

Example:

$('#example').dataTable( {
  "searchCols": [
    null,
    { "search": "My filter" },
    null,
    { "search": "^[0-9]", "escapeRegex": false }
  ]
} );

This is the only right solution when you can set a default search value when initializing a datatable. it's worth the datatable documentation.

More info: https://datatables.net/reference/option/searchCols

Working example: http://live.datatables.net/piqidoqo/603/edit

4 Comments

I'm not sure what you mean by your comment on my answer. Mine sets the search on a specific column, not the global filter.
That is not a global filter. You don't have to define a variable for data table. So can be defined "search value" by initialization of your datatable.
Yeah still not sure what you're talking about regarding the global filter.
I want to share my answer with other users, because searchCols in this case for individual columns, rather than the global filter, this option defined the filtering to apply to the table during initialisation. If you don’t understand what Global Filter is, then you should read the datatable documentation and don't set downvote.
3

I had to do this same thing. I got it to work by creating the datatable with loading deferred, then fill the search text, and then make the ajax call with the search parameter.

var Datatable = DataTable({
 deferLoading:true
 ... other options
});

//set search text on specific column
DataTable.columns(columnIndex).search('default search text'); 

//make ajax to call server
DataTable.draw(); 

Comments

0

This might work! But, here instead of name of that column you have to give position.

$( document ).ready(function() {
   var table = $('#dataTable').DataTable();
   table
        .columns( 3 )
        .search( "Your_Value" )
        .draw();
});

here 3 is just a example. Use your column position instead of 3 Reference Here

2 Comments

I tried this solution before and it works but i have some problem. I don't use pagination and it loads all data first and only then redraw table with filter. If i have many records it slow down the page. But for now it's the best solution i found
If you don't want to load whole data at first, Change your SQL query. give that name filter in query itself.

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.