Server Side processing,filtering and ajax call

Server Side processing,filtering and ajax call

duedue Posts: 2Questions: 0Answers: 0
edited February 2010 in General
Hi,
I'm succesfully using datatables, with server side processing and filtering but I need an optimization:

[code]
jQuery(document).ready(function() {
oTable=jQuery('#my_table).dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "ajax_ads/process_qry.php"
} );
[/code]

Now an Ajax call to process_qry.php is made on every keystroke of the user in the filter field, Is it possible to make the call only when the user hit the Enter key ??? Thank you in advance !!

Replies

  • gautegaute Posts: 4Questions: 0Answers: 0
    From what I can understand there's no way to do this in the API. But in the _fnFeatureHtmlFilter-method you can add a if (e.keyCode == 13)

    The method will then look like:

    [code]
    function _fnFeatureHtmlFilter ( oSettings )
    {
    var nFilter = document.createElement( 'div' );
    if ( oSettings.sTableId !== '' )
    {
    nFilter.setAttribute( 'id', oSettings.sTableId+'_filter' );
    }
    nFilter.className = oSettings.oClasses.sFilter;
    var sSpace = oSettings.oLanguage.sSearch==="" ? "" : " ";
    nFilter.innerHTML = oSettings.oLanguage.sSearch+sSpace+'';

    var jqFilter = $("input", nFilter);
    jqFilter.val( oSettings.oPreviousSearch.sSearch.replace('"','"') );
    jqFilter.keyup(function(e) {

    if (e.keyCode == 13)
    _fnFilterComplete( oSettings, {
    "sSearch": this.value,
    "bEscapeRegex": oSettings.oPreviousSearch.bEscapeRegex
    } );
    } );

    jqFilter.keypress( function(e) {
    /* Prevent default */
    if ( e.keyCode == 13 )
    {
    return false;
    }
    } );

    return nFilter;
    }
    [/code]

    I only have version 1.5.4 to look at right now, so the method may have changed, but I tried it and it seems to work. I'm sure Allan will have a better solution...
  • duedue Posts: 2Questions: 0Answers: 0
    Thank you !! It works greatly for global filtering,

    Now I've to find how to apply the same solution to the column based filter.

    Thank you again ...
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    You might quite like the fnSetFilteringDelay ( http://datatables.net/plug-ins/api#fnSetFilteringDelay ) plug-in, which will add a small delay to the 'key press' search - hopefully saving your server! This approach could be adopted for detecting an 'enter' key as well, which would save you from modifying the core.

    Allan
This discussion has been closed.