I have implemented the following shiny table, where users can filter the whole table or specific columns. However, while the smart search works for the search of the whole table, I can't enable the smart search for the columns. Could someone please give me a hint what I can do to enable smart search also for the columns? Thank you in advance. enter image description here

output$shiny_table =
   DT::renderDT({

     # Show the filtered data table
     DT::datatable(

        # Call the filtered table as function
        data = table_filtered(),

        # Load and include extension modules
        extensions = c("FixedHeader"),

        # Each column has its own search box
        filter = list(position = "top",
                      clear = TRUE,
                      plain = TRUE),

        # Further options
        options = list(

           # Mark found search terms
           searchHighlight = TRUE,
           search = list(smart = TRUE, regex = TRUE, caseInsensitive = TRUE),

           # Special column properties
           columnDefs = list(
           
              # For specific columns: enable smart search
              list(targets = c("Title", "Authors", "Journal"),
                   search = list(smart = TRUE, regex = TRUE, caseInsensitive = TRUE)),

              # For specific columns: hide the search fields
              list(targets = c(" ", "Year", "Type", "Citations", "DOI"),
                   searchable = FALSE)
           )
        )
     )

   }, server = TRUE)

推荐答案

columnDefs没有search选项.你可以试试这个:

library(DT)

js <- c(
  "function(settings) {",
  "  var instance = settings.oInstance;",
  "  var table = instance.api();",
  "  var $inputs = instance.parent().find('.form-group input');",
  "  $inputs.off('keyup search input').on('keyup', function() {",
  "    var value = $(this).val();",
  "    if(value !== '') {",
  "      var index = 1 + $inputs.index(this);", # add one if row names
  "      var column = table.column(index);",
  "      column.search(value, false, true, true).draw();", # options: regex, smart, case insensitive
  "    }",
  "  });",
  "}"
)

dat <- data.frame(
  car = c("Mazda", "Mazda RX4", "Mazda RX4 Wag", "Ford", "Mercedes"),
  pet = c("dog", "dog", "cat", "cat", "cat"),
  day = Sys.Date() + 0:4
)

datatable(
  dat, filter = "top", 
  options = list(
    initComplete = JS(js),
    columnDefs = list(
      list(targets = 1, searchable = FALSE)
    )
  )
)

Jquery相关问答推荐

提交表单时运行自定义验证脚本

如何用 jQuery / AJAX 替换表格的行

DataTables - this.api 用于回调函数中的多个表

当输入值以编程方式更改时触发更改事件?

jQuery $.browser 是否已弃用?

使用 jQuery 将行添加到表的 tbody

将输入更改为大写

JQuery html() 与 innerHTML

将参数附加到 URL 而不刷新

jquery:更改URL地址而不重定向?

使用 Typescript 时如何停止类型 JQuery 上不存在属性语法错误?

Jquery Ajax 错误处理忽略中止

如果不是 jQuery,Javascript 中的美元符号是什么

javascript过滤对象数组

如何重新加载/刷新 jQuery 数据表?

jQuery或javascript查找页面的内存使用情况

如何确定滚动高度?

D3 和​​ jQuery 有什么区别?

如何使用 JQuery 删除onclick?

清除表jquery