Search Excluding Words

Search Excluding Words

ManiHInManiHIn Posts: 17Questions: 4Answers: 0

Hello,
Is there any possibilitiy to exclude words in datatables search like in google Minus or a NOT?
Thanks for help
Manuel

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @ManiHIn ,

    There's nothing currently that does that. You could develop a filter plugin that does it, perhaps with some clever regexs.

    Cheers,

    Colin

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    I am not so sure. At least if you do the search using serverSide processing you can manipulate what is passed on to the server for searching. Hence you could easily filter out words or symbols from your search string.

    Here is an example showing how to manipulate the search. In this case it is about dates and numbers that need to be in MySQL-format for server side search.

    var rateTable = $('#tblRate').DataTable( {
        dom: "Bfrltip",
        processing: true,
        serverSide: true,    //server side only works well with type "POST" !!!
        ajax: {
            url: 'actions.php?action=tblRate',
            type: 'POST',
            data: function ( d ) {
        //allow searching for dates with server side processing
                var dFs = ['D/M/YYYY', 'DD/MM/YYYY', 'D/M/YY', 'DD/MM/YY', 'D/M', 'DD/MM', 'D/M/', 'DD/MM/'];
                var toBeFormat = ''; var sepCounter = 0;
        //No commas allowed as date separator; if English: no period either!
                if ( ( ! ( d.search.value.indexOf(',') >= 0 ) ) &&
                     ( ! ( lang !== 'de' && d.search.value.indexOf('.') >= 0 ) )    )   {
                    if ( moment(d.search.value, dFs).isValid() ) {
                        toBeFormat = 'MM-DD';
                        for (i=0; i < d.search.value.length; i++) {
                            //counting the separators in the search string
                            if ( isNaN (d.search.value.substr(i, 1)) ) {
                                sepCounter++;
                        //if we find two separators and the second one is not at the
                        //end of the string we have a long date otherwise only a short one
                                if ( sepCounter === 2 && i < (d.search.value.length-1) ) {
                                    toBeFormat = 'YYYY-MM-DD';
                                }
                            }                        
                        }
                        if (sepCounter > 0) { //we need to have found at least one separator
                            d.search.value = moment(d.search.value, dFs).format(toBeFormat);
                        } else {
                            toBeFormat = '';
                        }
                    }
                }
        //not a date: we check for a number
                if (toBeFormat <= '') {
                    var number;
                    if (lang == 'de') {
                        number = d.search.value.toString().replace( /[\.]/g, "" );
                        number = d.search.value.toString().replace( /[\,]/g, "." );
                    } else {
                        number = d.search.value.toString().replace( /[\,]/g, "" );
                    }
                    if ( ! isNaN( parseFloat(number) ) ) {             
                        d.search.value = number;
                    }
                }
            }
        },
        pageLength: 20,
        lengthMenu: [5, 10, 20, 50, 100, 200, 500],
        columns: [
            {   data: "rate.ref_rate" },
            {   data: "rate.currency" },
            {   data: "rate.ref_rate_period" },
            {   data: "rate.date" },
            {   data: "rate.rate" },
            {   data: "rate.update_time" }
        ],
        order: [[ 3, 'desc' ]],
        select: {
            style: 'multi'
        },            
        buttons: [
            {   extend: "create", editor: rateEditor },
            {   extend: "edit",   editor: rateEditor },
            {   extend: "remove", editor: rateEditor, name: "remove" },
                        "colvis"
        ]
    } );
    
  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    https://datatables.net/manual/server-side#Sent-parameters

    search.value is what you need to manipulate or columns[i].search.value for specific column search

This discussion has been closed.