How do I use .search() to look for multiple values?

How do I use .search() to look for multiple values?

robs8383robs8383 Posts: 2Questions: 1Answers: 0

I have the following code. I would like to draw the table using data found in column 1 that contains both "Backlog" OR "Stretch". However, the | doesn't work. I've also tired ||. I've tried using an array but that didn't work either. If I search for just "Backlog" or just "Stretch" it works fine. I'd like it return the results when multiple values are found.

$(document).ready(function() { 
    $('#example').DataTable().column(1).search("Backlog" | "Stretch").draw(); 

    } ); 

Is there also way to search for all values except one specific value (i.e. !Backlog )?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 14,086Questions: 25Answers: 3,322
    Answer ✓

    Sounds like you may want to use a regex search as described here:
    https://datatables.net/reference/api/search()

    Kevin

  • robs8383robs8383 Posts: 2Questions: 1Answers: 0
    edited December 2017

    Thank you @kthorngren . That pointed me in the right direction!

    This is my solution:

    $('#example').DataTable({"iDisplayLength": 100, "search": {regex: true}}).column(1).search("backlog|Stretch|Solid|NIR", true, false ).draw(); 
    

    This allowed to 'pre-filter' the data in column 1 on backlog, Stretch, Solid and NIR.

  • asadnaeemasadnaeem Posts: 4Questions: 1Answers: 0

    @robs8383 It is working great. Thanks a lot

  • asadnaeemasadnaeem Posts: 4Questions: 1Answers: 0

    $('#example').DataTable({"iDisplayLength": 100, "search": {regex: true}}).column(1).search(val.join('|'), true, false).draw();

  • agarnagarn Posts: 25Questions: 4Answers: 1

    Doesn't work for me. I need to match any of the two words in my string. I am using servide side processing as true in my datatable.
    var ActivityTable = $('#recentActivity').DataTable({
    "sPaginationType": "full_numbers",
    "bProcessing": true,
    "bServerSide": true,
    "bJQueryUI": false,
    "iDisplayLength": 19,
    "dom": 'rt<"tableFooter"ip>',
    "aaSorting": [[0, "desc"]],
    "autoWidth": false,
    "ajax": {
    "type": "POST",
    "url": '@Url.Action("GetDataTables2", "ActivityLogReport")'
    },
    "autoWidth": false,
    "oLanguage": {
    "sSearch": "Search all columns:"
    },
    "columns": [
    { "name": "Id", "data": "id", "title": "Id", "orderable": false, "searchable": false, "visible": false },
    { "name": "Date", "data": "date", "title": "Date", "orderable": true, "searchable": true, "visible": true, "width": "15%" },
    { "name": "User", "data": "user", "title": "User", "orderable": true, "searchable": true, "visible": true, "width": "10%" },
    { "name": "Description", "data": "description", "title": "Description", "orderable": true, "searchable": true, "visible": true, "width": "75%" }
    ],
    "buttons": []
    });

    //external button to filter datatable records
    $('#ShowPublishedOrProcessed').click(function () {
    var descriptionColumn = ActivityTable.column("Description:name");
    descriptionColumn.search('published|Processes', true, false).draw();
    });

    It returns only first string (published) data. Description column looks like as follows:
    2020: Item published successfully. Item name - X1.
    2020: Item published successfully. Item name - X2.
    2020: Processes uploaded csv count information for item: X5
    2020: Item added. Title: test
    2020: Item 'Test'has changed type from: to: B2

  • kthorngrenkthorngren Posts: 14,086Questions: 25Answers: 3,322

    Do you have your server script setup to support regex searches?

    Are you using a Datatables provided server script?

    Kevin

  • agarnagarn Posts: 25Questions: 4Answers: 1

    Hi Kevin,

    I am not sure how to I check if server script setup exists. I think I am using datatables provided server script only.

    Thanks,
    Nikita

  • kthorngrenkthorngren Posts: 14,086Questions: 25Answers: 3,322

    The Datatables supplied server side scripts don't support regex searches. Please see this thread for more details.

    Kevin

  • agarnagarn Posts: 25Questions: 4Answers: 1

    Got it.Thanks Kevin.

  • kthorngrenkthorngren Posts: 14,086Questions: 25Answers: 3,322

    @amariwan

    $('#datatable1').DataTable().column([5]).search("0" | "1").draw();

    This statement is likely not working the way you expect. The | is a bitwise OR operator. The "0" | "1" is doing an OR operation. I think you will want this to be a string something like "0 | 1". Your server script will need to interpret this to build the query string.

    Kevin

  • kthorngrenkthorngren Posts: 14,086Questions: 25Answers: 3,322

    @amariwan Since you have Server Side Processing enabled its up to your server script to perform the searching. Are you using a Datatalbes supplied server script or your own? Maybe post the script so we can see what you are doing.

    Kevin

  • amariwanamariwan Posts: 1Questions: 0Answers: 0

    Thank you for your answer @kthorngren,

    i'm sorry, i can't post my script otherwise i'll lose my job.

    My code looks like this.
    http://www.abrandao.com/2014/03/server-side-datatables-with-sqlite-or-mysql-via-pdo/comment-page-1/#comment-79006

    Aland

Sign In or Register to comment.