Delay Search only partially working

Delay Search only partially working

egglesworthegglesworth Posts: 2Questions: 1Answers: 0

I am using DT within an RShiny app with a large datatable. I am using a regex search for character columns, so it takes 2 or 3 seconds for each search. I therefore put in a delay search, but it seems to be triggering once with the first letter of the search, as per this question.

Is there a solution (or a workaround that would work in RShiny?)

https://datatables.net/forums/discussion/37988/option-searchdelay-triggering-once-before-specified-delay

Answers

  • rf1234rf1234 Posts: 2,996Questions: 87Answers: 421
    edited February 2023

    Have you seen throttled search? Here is an example from my own coding with a delay of 1 second, if I recall it correctly:

    https://datatables.net/reference/api/DataTable.util.throttle()

    $('.filterHead', ctrTable.table().header()).each( function (i) {
        var title = (lang === 'de' ? 'Suche ' : 'Search ') + $(this).text();
        var column = ctrTable.column(i);
        var throttledSearch = $.fn.dataTable.util.throttle(                        
            function (e) {
                var code = e.which;
            // 8: backspace, 46: delete, 13: enter, 32: space, 
            // 59 or 186: semi-colon,
            // 188: comma, 189: dash or minus, 190: period
                if ( code == 13 ) { //enter key
                    e.preventDefault();
                }
                var txt = $(this).val();
                if ( txt.length > 1 || code == 8 || code == 46 ) {
                    column
                        .search(txt)
                        .draw();
                }
            },
            1000
        );
        $('<input type="text" placeholder="'+title+'" class="text-muted"/>')
            .appendTo( $(this).empty() )
            .on( 'keyup change', throttledSearch); 
    } ); 
    
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    Hi,

    This is sort of intended - the idea was to start a process as soon as the user does some kind of interaction, and then debounce from there. In retrospect that was a mistake, we should just do a delay debounce rather than a throttle. I plan to change that for v2, but wasn't planning to change it before.

    It would require a tweak in the DataTables source if you wanted to make that change before then.

    Allan

  • rf1234rf1234 Posts: 2,996Questions: 87Answers: 421

    @allan, thanks for clarifying. I had found out that searchDelay isn't working too - and got a combination of searchDelay and throttled search running.

  • egglesworthegglesworth Posts: 2Questions: 1Answers: 0

    Thanks for the responses - is there a way to get the above to work in R Shiny?

Sign In or Register to comment.