Search code examples
javascriptrdatatablesdt

Some strings disappear in DT when using filters with searchHighlight option


There is an issue connected with DT filtering when using individual column filters + the searchHighlight option.

Steps to reproduce:

  1. Use this code to generate a simple DT table with a single character column:
library(DT)

df <- data.frame(
  test_chr = paste0('test', 1:10)
)

datatable(
  df,
  filter = "top",
  options = list(
    searchHighlight = TRUE
  )
)

изображение

  1. Type test1 in tst_chr individual column filter; изображение

  2. Cancel search by erasing search query or by clicking the clear button;

  3. The word test disappears in nearly all rows. изображение


Solution

  • Here is a solution. Use this callback and this works:

    library(DT)
    
    df <- data.frame(
      test_chr = paste0('test', 1:10)
    )
    
    datatable(
      df,
      filter = "top",
      options = list(
        searchHighlight = TRUE
      ),
      callback = JS(
        "table.on( 'draw', function() {
            var body = $( table.table().body() );
            body.unhighlight();
            body.highlight( table.search() );  
        } );
        "
      )
    )