How can we search multiple values in dt.column.search() function

How can we search multiple values in dt.column.search() function

Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1
dt.column(23).search('21').draw();

currently i am getting id of my categories in a column which is hidden. I can search for a particular value, in our case 21 would give me the output but what if i want to search more than one value at a time. lets say : dt.column(23).search('21|22').draw();
I tried this method any many differnt but no result. Hope anybody could help me with that :smile:

Currently, I have declared a button to filter one value which needs to be for two value.
table.button().add(
{
text: 'OSC',
action: function (e, dt, node, config) {
dt.column(23).search('14').draw();
},
},
});

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin
    edited September 2023

    dt.column(23).search('21|22', true, false).draw();. Turn off the smart filter and turn on regex. See search() / column().search().

    Allan

  • Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1
    edited September 2023

    Thank you that helped!

    Another question, is it possible to get new data in datatable when a user adds a newentry on a different device but same table, without actually refreshing the page or ajax.
    Currenlt, I am refreshing the ajax file which causes the table to get back on the top which is a problem to the users who might be working on the last entry of the page.

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,953

    Currenlt, I am refreshing the ajax file which causes the table to get back on the top

    How are you doing this? I assume you are using ajax.reload(). You can stay on the same page by passing in false as the second parameter. See the second example in the docs. IF someone is interacting with the table this will likely still interfere but will stay on the same page.

    Another option is to use jQuery ajax() to fetch the updates and use row.add() or rows.add() to add the new rows. Use draw(), passing false as the parameter, to stay on the page but update sorting and searching. This will likely interfere with someone interacting with the table.

    You might need to use a global variable a a flag to decide whether to refresh the table or not. Set the flag false for example if the user is interacting with the table. When they are done then set the flag to true. If the flag is true then the table can be updated. Depending on how the user interacts with the table you may need to temporarily turn off the interaction will the ajax request is outstanding.

    Kevin

  • Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1
    edited September 2023

    Alright thanks for letting me know will take a look at that.

    For the first question, I created a regex function which does not seem to be working. I created a button when pressed filter a particular data from a column.I have defined it as follow:

       table.button().add(
                null, {
                extend: 'collection',
                collectionLayout: 'dropdown',
                text: 'Filter',
                autoClose: true,
                buttons: [
                    {
                        text: 'OSC',
                        action: function (e, dt, node, config) {
                            dt.columns().search('').draw();
                            var value = "4|14";
                            filterColumn(23, value);
                        },
                    },]
    

    And created a function to filter a value form the column

       function filterColumn(i, value) {
            $('#assyntCx_Table').DataTable().column(i).search(
                value,
                true
            ).draw();
        }
    

    I have a datatable instace where I have defined a function passing custom value which is working fine: https://live.datatables.net/ucilib/80/edit
    But the same concept doesn't work for me.
    Am i missing any library or something.

    Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

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

    The code above looks like it should do the trick. Could you update your example please so that it demonstrates the issue you want support with, it doesn't have any of the code (such as the button) that you're querying.

    Colin

  • Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1

    {
    text: 'Launches',
    action: function (e, dt, node, config) {
    dt.columns().search('').draw();
    var value = "74|75|76|77";
    dt.column(24).search(value).draw();
    AddToPageTitle("Filtered to Launch Entries");
    },
    },

    well right now i am just using a pipe operator to filter multiple values from a column.
    Serverside processing is also set to fasle. Weird, it was working fine the other day but now not working again.

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    That looks like it should work. If it isn't, we'd need a link to a page showing the issue.

    Allan

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,953
    edited October 2023 Answer ✓

    As Allan explain ed earlier you need to enable regex searching and disable smart searching. See the search() docs for the different modes and the column().search() docs. You will want something like this:

    var value = "74|75|76|77";
    dt.column(24).search(value, true, false).draw();
    

    Kevin

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    Doh - I missed that the arguments weren't being passed in. Thanks Kevin!

    Allan

  • Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1

    Right now I have added a button to filter data from a column which is also working fine but the problem here is it only works with serverside set to true,but when false it does not work and I have a huge chunk of data of about 6k entries which is making it really slow. Is there an way where the regex still works when serverside is true or something similar

    var table = $('#assyntCx_Table').DataTable({

            dom: "lBfrtip",
            //AJAX
            "serverSide": true,
            stateSave: false,
            //processing: true,
            ajax: {
                url: "../ajax/at/DailyLog.php",
                type: "POST",
                deferRender: true,
                beforeSend: function () {
                    // Here, manually add the loading message.
                    $('#assyntCx_Table > tbody').html(
                        '<tr class="odd">' +
                        '<td valign="top" colspan="16" class="dataTables_empty">Loading&hellip;</td>' +
                        '</tr>'
                    );
                },
            },
            scrollY: "65vh",
            scrollX: true,
            scrollCollapse: false,
            responsive: false,
            columns: [
                {
                    //0
                    data: "id",
                    class: "text-center",
                },
    

    ]
    });

    THis is my custom button to filter entries
    buttons: [
    {
    text: 'YCC Entries',
    action: function (e, dt, node, config) {
    dt.columns().search('').draw();
    dt.column(26).search('3|1', true, false).draw();
    AddToPageTitle("YCC Entries");
    },]

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,953

    Your description is confusing as I'm not sure if you mean that the regex search doesn't work with serverSide: true. I suspect that is the case as the server script would need to perform regex searching of the server data.

    I don't use any of the Datatables supplied server side processing scripts but my understanding is they don't support regex searching as it could have performance impacts with the queries. You will need to update the server side processing script you are using to perform regex queries.

    Kevin

  • Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1

    What I mean is "dt.column(26).search('3|1', true, false).draw();" I cannot use this when serverside is enabled but i can use "dt.collumn(26).search('1').draw(). when it is enabled so basically what i mean is how can i search multiple value with serverside enabled.

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,953
    edited November 2023

    As I said the search is controlled by your server script. Are you using a Datatables supplied server side processing script? If so which one?

    The script will need to be modified to perform regex or multi-value searches. How its done is dependent on the server script language and the data source being used. For example you might need to use REGEXP if using MySql.

    Kevin

  • Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1

    <?php
    include_once("./utils.php");
    $userID = check_session();

    include("../lib/DataTables.php");

    use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

    $update = 'd M Y';

    Editor::inst($db, 'ops_dailyLog OD', 'OD.id')
    ->field(

        Field::inst('OD.id', 'id'),
        Field::inst('OD.date', 'Date'),
        Field::inst('OD.time', 'Time'),
        Field::inst('OD.controller', 'control1'),
        Field::inst('U.username', 'Controller'),
        Field::inst('EET.type', 'entryType')
    

    )
    ->debug(true)
    ->process($_POST)
    ->json();

    <?php > ?>

    this is my server side script if that helps

Sign In or Register to comment.