Custom filter option and server-side processing.

Custom filter option and server-side processing.

andrew_mandrew_m Posts: 31Questions: 0Answers: 0
edited March 2009 in General
Hi.
First of all thanks for the awesome component :)
We have a grid with paging and sorting on the page, which requires addition custom filtering. We cant use standard filtering textbox, so we placed our own controls (checkboxes) above grid. Is there any way to intercept ajax url, which uses by datatables to retrieve information from server, and add additional "get" field to request?

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Hi Andrew,

    Yup - there certainly is. What you need to do is override the built in get function by using the parameter 'fnServerData' - I'm afraid that this isn't fully documented yet since it's only in 1.5 beta (need to update the documentation for 1.5 final), but what you can do is something like this:

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "media/examples_support/server_processing.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    aoData.push( { "name": "more_data", "value": "my_value" } );
    // etc
    $.getJSON( sSource, aoData, function (json) { fnCallback(json) } );
    }
    } );
    } );
    [/code]

    There is a little bit more detail about this function override in this thread: http://datatables.net/forums/comments.php?DiscussionID=53 - but this example will hopefully point you in the right direction :-)

    Allan
  • andrew_mandrew_m Posts: 31Questions: 0Answers: 0
    Thanks alot Alan for the response. This will definitely help us.
    One more question. Is it possible to determine filter value for the specified column? And how if possible?
    And last thing, well its just more like a suggestion... It would be really cool (maybe optionaly) if indexed variables of ajax Url appear like 'name[i]' instead of 'name_i'. Both aspnet mvc and monorail have array parameter binders, so it would be possible to have server actions like
    public JsonResult GetData(int iColumns, bool[] bEscapeRegex, int[] iSortCol, int[] iSortDir, string[] sSearch)
    {
    for (int i = 0; i < iColumns; i++){
    string s = sSearch[i];
    // etc...
    }
    }
    instead of loooong action parameter list.
    Please consider this only as a suggestion, and thanks for the excelent work :)
    PS. Excuse my poor english.
  • andrew_mandrew_m Posts: 31Questions: 0Answers: 0
    One more thing :)
    Is there any callback function, which fires right after table's dom created? I tried fnInitComplete, but seems it fires only after initialization from html markup, and not from ajax response. I'm asking because i'm trying to marry datatables with tableDnD plugin, and tableDnD requires complete built table, which datatable builds after ajax response.
  • andrew_mandrew_m Posts: 31Questions: 0Answers: 0
    Please ignore my last post:) I added tableDnD initialization right after call-back function call from your example and it works as expected.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Hi Andrew,

    1. The individual filter column information should be passed to you by the Ajax call. You should get:

    sSearch_(int) - Individual column filter
    bEscapeRegex_(int) - Individual column filter is regex or not

    for each column - which will tell you the search information for each one.

    2. I'm somewhat reluctant to include language specific information in the http variables. Although the current method might require a little bit of processing on each platform, it is at least not-platform specific. Also, I'm weary of including language syntax in http vars... Sounds like a possible security risk.

    3. When using the server-side processing option, the fnDrawCallback function is called on each draw (i.e. whenever the DOM is recreated).

    Regards,
    Allan
  • andrew_mandrew_m Posts: 31Questions: 0Answers: 0
    Sorry, probably my explanation was not clear. What i wanted to say is there any way to determine filter values for table columns on the client side? :)
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Oh I see! Yes indeed, you can use the fnSettings() API function to get the settings object for the table in question, and then look at the 'aoPreSearchCols' parameter. This is an array of data (objects) maintained internally by DataTables. Each object (one for each column) has two parameters "sSearch" and "bEscapeRegex".

    Allan
  • andrew_mandrew_m Posts: 31Questions: 0Answers: 0
    Thanks for your help Allan.
This discussion has been closed.