fnFilter and regex not playing nice for me

fnFilter and regex not playing nice for me

chris4betachris4beta Posts: 3Questions: 0Answers: 0
edited July 2010 in General
I have a very frustrating problem that is making no sense to me. I have a table with column index 3 containing strings such as 'AVAIL', 'SHPD', 'RTND', etc. I'm trying to use regex to filter either AVAIL OR SHPD with no luck.

Example:
[code]
asset_filter = 'AVAIL|SHPD';
asset_table.fnFilter(asset_filter, 3, false);
[/code]

Which matches nothing, but when asset_filter = 'AVAIL', it finds those ok, it's just when I add the pipe that it doesn't work. I've even tried asset_filter = ^(AVAIL|SHPD)$, which is correct regex but doesn't match anything. Even ^(AVAIL)$ doesn't match anything when it should.

The data table is initialized with:
[code]
asset_table = $(".asset_list_table").dataTable({
"bPaginate": false,
"bInfo": false,
"aaSorting": [[3,'asc'], [0,'asc']],
"aoColumns": [
{"sType": "num-html"},
{"sType": "string"},
{"sType": "string"},
{"sType": "string"},
]
});
[/code]

Nothing else has been done to the table. Can you see something I'm doing wrong? I'm using 1.7 beta 5.

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    edited July 2010
    Try passing in false as the fourth parameter to fnFilter. This will disable DataTables' "smart" filtering (which it tries to allow words to match across a whole string, rather than in sequence).

    This is new in the 1.7 betas, which is why it's not fully documented yet.

    Allan
  • chris4betachris4beta Posts: 3Questions: 0Answers: 0
    I've tested it a little more. Using v1.7 beta 5:
    [code]asset_table = $(".asset_list_table").dataTable({});
    asset_table.fnFilter("AVAIL|SHPD", 3, false);[/code]
    and
    [code]asset_table.fnFilter("AVAIL|SHPD", 3, false, false);[/code]
    return zero results, despite there being many 'AVAIL' and 'SHPD' in column 3.

    However, using v1.6.2:
    [code]asset_table.fnFilter("AVAIL|SHPD", 3, false);[/code]
    works fine, along with all other regex filters I try. Looks like a regression bug?
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Hi,

    Actually not quite - sorry I forgot to explain this properly. I've altered the filtering interface a little bit in the 1.7 series to hopefully make a bit more sense - however it does mean that there is a slight backwards compatibility issue with older version when you are trying to do regex filtering.

    The new prototype for the fnFilter function is:

    this.fnFilter = function( sInput, iColumn, bRegex, bSmart, bShowGlobal )

    sInput - the filter you want to apply
    iColumn - the column you want to apply it to, or null for global
    bRegex - true if you want it to be treated as regex, false if not !!! this is basically the boolean inverse of before !!!
    bSmart - have DataTables try to do smart filtering or not - it can interfere with your own regex
    bShowGlobal - have DataTables show the filtering into in the user input boxes or not

    So if you pass 'true' as the third parameter - hopefully that will do the trick for you.

    This is a bit about this in the upgrade notes: http://datatables.net/upgrade/1.7

    Regards,
    Allan
  • chris4betachris4beta Posts: 3Questions: 0Answers: 0
    :) Very good, sir! Everything is working as it should, and DataTables is a perfect fit for what I needed. I will be donating shortly.

    Thank you.
  • pedroAraujopedroAraujo Posts: 1Questions: 0Answers: 0
    allan, thanks a lot!
    your explanation was very useful, that version problem and the boolena switch on the function argumenst is kind of tricky.. :P

    best regards
  • vishwanathvishwanath Posts: 1Questions: 0Answers: 0
    edited May 2012
    hi
    i have a column in datatable as status which is having two value i:e working , not working .
    i am using fnfilter on that column ,
    for not working it is working fine
    but when i select working it does not able to filter the datatable i;e it gives me both working as well as not working rows .
    i used oTable.fnFilter(selectedValue,3,true,false,true,false);

    the selectedValue in above i get from option i selected in select option .
  • ombakombak Posts: 4Questions: 0Answers: 0
    hai Allan, I have same problem with chris4beta, but I still don't get the solution.
    I try like what you show, but not work. this my code:

    [code]
    var tbl_penugasan_guru = $("#tbl_penugasan_guru").dataTable({
    "fnDrawCallback": function( oSettings ) {
    /* show dialog when click
    */
    $( "a.create-kelas" ).bind('click', function() {
    $("#dialog-form").dialog( "open" );

    /* filter function
    */
    fnFilterColumn();
    });
    },
    "bFilter": true,
    "bSort": false,
    "bStateSave": true,
    "bServerSide": true,
    "bAutoWidth": false,
    "bProcessing": true,
    "bLengthChange": false,
    "bInfo": false,
    "iDisplayLength": <?php echo ($this->settings_lib->item('site.list_limit')) ? $this->settings_lib->item('site.list_limit') : 15; ?>,
    "sPaginationType": 'bootstrap',
    "sAjaxSource": 'penugasan_guru/populate_penugasan_guru',
    "aoColumns": [
    { 'sName' : 'g.nama' },
    { 'sName' : 'p.pelajaran' },
    { 'sName' : 'pg.rombel_id' },
    { 'sName' : 'ta.tahun' },
    { 'sName' : 'p.tingkat', 'bVisible': false },
    ],
    "fnServerData": function (sSource, aoData, fnCallback) {
    /* validasi token dari CI */
    aoData.push( { "name": "ci_csrf_token", "value": $("input[name=ci_csrf_token]").val() } );

    $.ajax ({
    'dataType': 'json',
    'type' : 'POST',
    'url' : sSource,
    'data' : aoData,
    'success' : fnCallback
    });
    },
    });

    /* filter function
    */
    function fnFilterColumn ()
    {
    var asset_filter = '1|2';
    $("#tbl_kelas").dataTable().fnFilter(asset_filter, 2, true, true);
    }
    [/code]

    when asset_vilter = '1'; the code work.


    regrads;
This discussion has been closed.