use regexp to search in global datatable search

use regexp to search in global datatable search

trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2
edited September 2016 in Free community support

Hello,

I would like to use regexp in the global search (the input field which appears on the top of the datatable) but it doesn't work. I can't understand why.

I use column searching (which works perfectly) and accent neutralization (it works perfectly).
I also wrote a function called adaptFilterForWildcard(str) which changes "XX" to "XX^" and "XX" to "^XX" to allow the user to use the * wildcard naturally. It works perfectly on column search, but NOT in global search.

I can't see what difference I make between column search (which works perfectly) and GLOBAL search, which does not works when I type a search string like liè* to get all rows which begins with "liè" or "lie" or "lié" and so on...

EDIT. This could be linked to this question. Is that a bug inside DataTables ? I'm looking forward to you...

Could you please help me ? Many thanks in advance...

T.

Here is my table initialisation :

// I use the accent neutralisation ; it works :-)
jQuery.fn.DataTable.ext.type.search.string = function ( data ) {
        return ! data ?
            '' :
            typeof data === 'string' ?
                data
                    .replace( /έ/g, 'ε')
                    .replace( /ύ/g, 'υ')
                    .replace( /ό/g, 'ο')
                    .replace( /ώ/g, 'ω')
                    .replace( /ά/g, 'α')
                    .replace( /ί/g, 'ι')
                    .replace( /ή/g, 'η')
                    .replace( /\n/g, ' ' )
                    .replace( /[áÁ]/g, 'a' )
                    .replace( /[éÉ]/g, 'e' )
                    .replace( /[íÍ]/g, 'i' )
                    .replace( /[óÓ]/g, 'o' )
                    .replace( /[úÚ]/g, 'u' )
                    .replace( /ê/g, 'e' )
                    .replace( /î/g, 'i' )
                    .replace( /ô/g, 'o' )
                    .replace( /è/g, 'e' )
                    .replace( /ï/g, 'i' )
                    .replace( /ü/g, 'u' )
                    .replace( /ã/g, 'a' )
                    .replace( /õ/g, 'o' )
                    .replace( /ç/g, 'c' )
                    .replace( /ì/g, 'i' ) :
                data;
    };

// table initialization
var tableDONNEES = $("#tableDONNEES").DataTable( {
        "ajax": {
            url: "scripts/myscript",
            type: "GET",
            data: function ( d ) {
                d.operation = "something";
            }
          },
          "search": {
            "regex": true
          },
        "initComplete": function(settings, json){
            
            // réalise les recherches quand on tape des choses
            tableDONNEES.columns().eq( 0 ).each( function ( colIdx ) {
                $( 'input', tableDONNEES.column( colIdx ).header() ).on( 'keyup change', function (ev) {
    
                    var filter = adaptFilterForWildcard(this.value); // this function changes "*XX" to "XX^" and "XX*" to "^XX"

                    // procède à la sélection
                        tableDONNEES
                            .column( colIdx )
                            .search( jQuery.fn.DataTable.ext.type.search.string(filter), true)
                            .draw();
                    
                } );
                $('input', tableDONNEES.column(colIdx).header()).on('click', function(e) {
                e.stopPropagation();
            });
            } );
            //GLOBAL SEARCH : THIS DOES NOT WORK AS EXPECTED
              $('.dataTables_filter input', tableDONNEES.table().container()).keyup( function () {
                  var filter = adaptFilterForWildcard(this.value);
                tableDONNEES
                  .search( jQuery.fn.DataTable.ext.type.search.string( filter ), true )
                  .draw()
              } );
        },
        autoWidth: false,
        scrollX: true,
        "pageLength": 10,
        "order": [ 1, 'asc' ],
        "columns": [...]
    } );

This discussion has been closed.