morlandi / django-ajax-datatable

A Django app which provides the integration of a Django project with the jQuery Javascript library DataTables.net
MIT License
204 stars 64 forks source link

set search delay for fields #32

Open hypy13 opened 3 years ago

hypy13 commented 3 years ago

hi how can i set search delay or set a throttle frequency for searching?

i have put this option but nothing happend https://datatables.net/reference/option/searchDelay

morlandi commented 3 years ago

Good point. I suppose you're talking about column filters; since those are based on a fully custom solution, and do not count in any way on datatable.net, searchDelay can't help here.

Everything happens in file "utils.js". This is the relevant handler:

    function _handle_column_filter(table, data, target) {
        var index = target.data('index');
        var value = target.val();

        var column = table.api().column(index);
        var old_value = column.search();
        console.log('Request to search value %o in column %o (current value: %o)', value, index, old_value);
        if (value != old_value) {
            console.log('searching ...');
            column.search(value).draw();
        }
        else {
            console.log('skipped');
        }
    };

which is trigger as follows:

            var column_filter_row = wrapper.find('.datatable-column-filter-row')
            column_filter_row.find('input,select').off().on('keyup change', function(event) {
                var target = $(event.target);
                _handle_column_filter(table, data, target);
            });

Conceptually should be easy to postpone the call to _handle_column_filter() replacing it with a one-shot timer which in turn will call _handle_column_filter() later on; you also need to restart the timer if new keystrokes are received in the meantime.

I'll keep this Issue open as a reminder, but can't say when I will process it; should you solve this in the meantime, I'm willing to accept PRs