Column filtering does not use column type for filtering

Column filtering does not use column type for filtering

mimicmimic Posts: 43Questions: 0Answers: 0
edited March 2009 in Bug reports
Column filtering does not use column type (sType) for filtering. For example in a column defined as HTML it still searches HTML code. (While global filtering works as expected.)

(On 1.4.3.)

Replies

  • allanallan Posts: 57,265Questions: 1Answers: 9,121 Site admin
    Good call - I've put a fix in for this with my development version of 1.5, so it will be in the next beta. If you want to know the required changes so you can put it into your 1.4.x install, let me know.

    Allan
  • mimicmimic Posts: 43Questions: 0Answers: 0
    Great. Yes, please, if it is not too much, can you tell me which changes are required.
  • allanallan Posts: 57,265Questions: 1Answers: 9,121 Site admin
    No problem:

    [code]/* Updated function */
    /*
    * Function: _fnFilterColumn
    * Purpose: Filter the table on a per-column basis
    * Returns: -
    * Inputs: object:oSettings - dataTables settings object
    * string:sInput - string to filter on
    * int:iColumn - column to filter
    * bool:bEscapeRegex - escape regex or not
    */
    function _fnFilterColumn ( oSettings, sInput, iColumn, bEscapeRegex )
    {
    if ( sInput === "" )
    {
    return;
    }

    var iIndexCorrector = 0;
    var sRegexMatch = bEscapeRegex ? _fnEscapeRegex( sInput ) : sInput;
    var rpSearch = new RegExp( sRegexMatch, "i" );

    for ( i=oSettings.aiDisplay.length-1 ; i>=0 ; i-- )
    {
    var sData = _fnDataToSearch( oSettings.aoData[ oSettings.aiDisplay[i] ]._aData[iColumn],
    oSettings.aoColumns[iColumn].sType );
    if ( ! rpSearch.test( sData ) )
    {
    oSettings.aiDisplay.splice( i, 1 );
    iIndexCorrector++;
    }
    }
    }

    /* Updated function */
    /*
    * Function: _fnBuildSearchArray
    * Purpose: Create an array which can be quickly search through
    * Returns: -
    * Inputs: object:oSettings - dataTables settings object
    * int:iMaster - use the master data array - optional
    */
    function _fnBuildSearchArray ( oSettings, iMaster )
    {
    /* Clear out the old data */
    oSettings.asDataSearch.splice( 0, oSettings.asDataSearch.length );

    var aArray = (typeof iMaster != 'undefined' && iMaster == 1) ?
    oSettings.aiDisplayMaster : oSettings.aiDisplay;

    for ( i=0 ; i
  • mimicmimic Posts: 43Questions: 0Answers: 0
    Thanks. It works.
  • mimicmimic Posts: 43Questions: 0Answers: 0
    Hm, it does not really work. Something broke with filtering with regex on a column. It does not use it as a regex. (On one other table where I do not use any HTML type columns.)
  • mimicmimic Posts: 43Questions: 0Answers: 0
    edited March 2009
    Aha, the problem is in _fnDataToSearch function, where you add " " (space) to every value and because of that existing regex does not match anymore.

    It would be probably best to move this space adding from _fnDataToSearch function to _fnBuildSearchArray function. So:

    oSettings.asDataSearch[i] += _fnDataToSearch( sData, oSettings.aoColumns[j].sType ) + ' ';

    and:

    function _fnDataToSearch ( sData, sType )
    {
    if ( sType == "html" )
    {
    return sData.replace(/\n/g," ").replace( /<.*?>/g, "" );
    }
    else if ( typeof sData == "string" )
    {
    return sData.replace(/\n/g," ");
    }
    return sData;
    }
  • allanallan Posts: 57,265Questions: 1Answers: 9,121 Site admin
    Yup - sounds good to me. I've made that change now.

    Thanks,
    Allan
  • SpringerSpringer Posts: 3Questions: 0Answers: 0
    Does the filter recognize a sType that has been added by a plug in? (Numbers with HTML)
  • allanallan Posts: 57,265Questions: 1Answers: 9,121 Site admin
    Hi Springer,

    Yes indeed - although to do anything useful with it you will need to add a filtering plug-in function, otherwise it will just filter it as a string. Have a look at the filtering plug-in page ( http://datatables.net/development/filtering ) for information on how to do this.

    Regards,
    Allan
  • SpringerSpringer Posts: 3Questions: 0Answers: 0
    Thank you
This discussion has been closed.