Datatables does not responding to search() method

Datatables does not responding to search() method

miguel_telecomiguel_teleco Posts: 3Questions: 1Answers: 0

Hi everyone,

I am working with .search() method in Datatables. Currently I am using it to look for a value in row 0 when a user clicks on a defined buttom. However, when user clicks on this buttom and he wants to go back to watch all table entries again (deleting input text in search box), Datatables does not draw all entries. Here is my code:

regExSearch ='^'+event+'$';

// Place the value into the datatable input filter and set focus there.
var tableInput = $('#tableID_filter > label > input').get(0);
$(tableInput).val(event);
$(tableInput).focus();

// Apply the match case (with regex) search manually.
var table = getDatatableInstance('#tableID');
table.column(0).search( regExSearch, true, false ).draw();

And JS code:

$(div).dataTable( {
    "responsive": true,
    "data": dataSet,
    "lengthMenu": [[5, 10, 50, 100, -1], ['5', '10', '50', '100', gettext('All')]],
    "bSort": sortable,
    "order": [[ 0, "asc" ]],
    "pageLength": pageLength,
    "columns": columns,
    "columnDefs": [
        {
            "targets": columnsToHide,
            "visible": false,
            "searchable": false
        }
    ],
    "buttons": [
        {
            "extend": 'csv',
            "name" : 'csv',
            "exportOptions": {
                "columns": columnsToExport
            }
        },
        {
            "extend": 'pdf',
            "name" : 'pdf',
            "exportOptions": {
                "columns":  columnsToExport
            },
            "orientation": orientation
        }
    ]
});

The rest of Datatables are responding correctly so I don't know why when an user dele the search text from input box, datatable does not draw all entries again.

I noticed that if I delete .column(0) from search everything goes nice. So the problem exists just when .column(0) is used.

Could someone help me?

Thanks beforehand.

Regards, Mike.

Answers

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    Hi Mike,

    I'm happy to take a look at a test case that shows the issue and help debug that. I'm not sure what is going wrong at the moment.

    Allan

  • miguel_telecomiguel_teleco Posts: 3Questions: 1Answers: 0

    Hi Allan,

    thanks for you answer. I will be waiting until new notices about this issue.

    Regards, Mike.

  • kthorngrenkthorngren Posts: 21,352Questions: 26Answers: 4,955

    Instead of using the global search input you might need to create an input specific for column 0. Then use an event handler that calls a function to update the column 0 filter. This example might give you some ideas:
    https://datatables.net/examples/api/regex.html

    Kevin

  • miguel_telecomiguel_teleco Posts: 3Questions: 1Answers: 0
    edited January 2018

    Hi Kevin,

    I am using a specific search for column number 0 (or that is what I think I did). Event handler does not make any change in datatables so if we look up to "function filterColumn ( i ) " that you provided me in your example, we can see search() method is the same as mine.

    Finally, your example do this search: $('#example').DataTable().column( 0 ).search(regex, true, false); By the same way my search do next : table.column(0).search( regExSearch, true, false ).draw();

    Thanks for you reply.

  • kthorngrenkthorngren Posts: 21,352Questions: 26Answers: 4,955

    I took your code as a basis for an example here and combined with the regex search example:
    http://live.datatables.net/nokinune/1/edit

    With your code the default global search input is populated when clicking a button. Deleting the text in that column does not update the table. Which I believe is the problem you are describing.

    If you comment this line:
    var tableInput = $('#example_filter > label > input').get(0);

    and uncomment this line:
    var tableInput = $('#col0_filter').get(0);

    The input next to the "Search" button will be used. This has an event handler that calls the "filterColumn()" function. Which is the same search you are doing. Deleting the text in this input does update the table.

    Is this what you are looking to do?

    Kevin

This discussion has been closed.