Option "searchDelay" triggering once before specified delay

Option "searchDelay" triggering once before specified delay

ffradegradaffradegrada Posts: 10Questions: 2Answers: 0

Hi!

I am having an issue with the "searchDelay" option with a server-side processing data table.

The search delay is working, but it's always making two calls. One immediately after pressing a key, and then the delay is working ok!

For example: if i write in the search box "Allan", the datatables ajax call is triggered once with "&seach=A" and the other "&search=Allan" (after the specified delay).

Here's my code:

$('#table').DataTable({
serverSide: true,
ajax: url,
processing : false,
searching : true,
searchDelay : 500,
ordering : true,
pageLength : 10,
lengthChange : false,
language : {
"url" : "js/dataTables.i18n.es.json"
},
dataSrc : "data",
columns : columns
});

Thanks!

Answers

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    This is a known bug I'm afraid. Its something that I very much need to look at but haven't had a chance to do so yet - sorry!

    Allan

  • ffradegradaffradegrada Posts: 10Questions: 2Answers: 0

    Oh, ok, I'll try to fix it and send the code! :)

  • MattSkeldonMattSkeldon Posts: 19Questions: 1Answers: 0

    Was this ever resolved?

    Regards MS

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Its something that will be addressed in the next major update to DataTables. The search delay will use a debounce rather than a throttle.

    Allan

  • MattSkeldonMattSkeldon Posts: 19Questions: 1Answers: 0

    In the mean time what I have done is during the initialisation of the data table, detach all the .dt events from the input the reattach .dt.keydown but listen specifically for enter key then use the api to trigger the search.

    The one thing I'm having to do is hardcode the reference to the search input to remove / add the events.

    Is there a means within the api to obtain this element?

    eg $(api.elements.searchinput)

    I will post my code I have tomorrow if this isn't clear however I'm currently away from my code

    Regards MS

  • MattSkeldonMattSkeldon Posts: 19Questions: 1Answers: 0
     "initComplete": function () {
                    var api = this.api();
                    $("#MyTableID_search")
                        .off(".DT")
                        .on("keyup.DT, blur", function (e) {
                            if (e.keyCode === 13) {
                                api.search(this.value).draw();
                            }
                        });
                },
    

    however what I am looking for is

     "initComplete": function () {
                    var api = this.api();
                    $(api.elements.searchinput)
                        .off(".DT")
                        .on("keyup.DT, blur", function (e) {
                            if (e.keyCode === 13) {
                                api.search(this.value).draw();
                            }
                        });
                },
    

    as this allows my solution to become generic without having to repeat this.

    Regards MS

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    There isn't really an API method for this yet I'm afraid. The closest is: $( 'div.dataTables_filter input', api.table().container() ).

    The table().container() gives the table container element, and the selector will pick out the input element for the filter. So it will work for any table regardless of id, but it isn't quite as trivial as api.dom().search() or something like that (not a bad idea though!).

    Allan

  • MattSkeldonMattSkeldon Posts: 19Questions: 1Answers: 0

    That will do a whole lot nicer than having to write the selector each time! :smiley:

This discussion has been closed.