Set default filter on first load?

Set default filter on first load?

bwpcbwpc Posts: 10Questions: 0Answers: 0
edited August 2012 in General
I am not using the built in search. I have my own form above the grid. I am using Knockout to bind that form to an object. When the user presses search, I am calling "searchResultsTable.fnFilter(ko.ToJSON(searchCriteria);". This is passing the full json for my criteria to the server where I parse it and apply the filter.

Problem is sometimes, I have an initial filter in place. But I don't know how to get this json into the table. If the user clicks on a column to sort, that filter json is not getting passed to the server.

But if the user changes the filter, searches, my method above is called, the sort and the filter json are passed to the server. If the user then clicks to sort, the filter json is still included in the call to the server.

So how can I get the default state of my filter into the grid when it's first created?

Replies

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin
    Use the oSearch initialisation parameter :-)

    Allan
  • bwpcbwpc Posts: 10Questions: 0Answers: 0
    I already tried that, but it's not expecting json I think. I get a javascript error at:

    jqFilter.val( oPreviousSearch.sSearch.replace('"','"') );

    oPreviousSearch contains my json string and sSearch is null;

    I am using an ajaxSource and it's my server that knows how to process the search criteria.
  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin
    edited August 2012
    How to set a default filter:

    [code]
    $('#example').dataTable( {
    "oSearch": {
    "sSearch": "mySearchValue"
    }
    } );
    [/code]

    DataTables search expects a string input (how would a user input a JSON search string?). If you need to send extra data to the server in the Ajax request then you can use fnServerParams .

    Allan
  • bwpcbwpc Posts: 10Questions: 0Answers: 0
    Thanks! That worked. My very first call back to the server now has my default search json.

    My problem was I was just setting my json string into oSearch directly.

    The user doesn't input a json string, but the user does use MY custom search interface and that wraps everything up into json. I will still look into fnServerParams for other data that I need to get across.
  • deviddevid Posts: 2Questions: 0Answers: 0
    Hi ,
    I tried the code but my result is displaying based on the default test.How can i avoid this.Please hel me...
  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin
    edited August 2012
    @devid - please see http://datatables.net/forums/discussion/9719/how-to-ask-for-help and link us to a test case showing the problem.

    Allan
This discussion has been closed.