是否可以在DT数据表中设置默认选中的复选框? [英] Is it possible to set the tickboxes selected by default in a DT datatable?

查看:69
本文介绍了是否可以在DT数据表中设置默认选中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下是否有可能在DT数据表中默认选中所有行的复选框(而不是取消选中)。

I would like to ask if it is possible to have the tickboxes of all the rows selected by default -instead of deselected- in a DT datatable.

library(DT)
library(tidyverse)
dataTableOutput("irisTable")
output$irisTable <- renderDataTable(
    iris %>% rowid_to_column("Row") %>% mutate(Row = ""),
    rownames = FALSE,
    extensions = "Select",
    options = list(
        columnDefs = list(list(className = "select-checkbox", targets = 0, orderable = FALSE)),
        select = list(style = "multi", selector = "td:first-child")
        ))


推荐答案

是的,您可以编写自定义的 JS 像这样的函数:
在这里,我们预选择第1,3和4行(请注意,计数从0开始)

Yes, you can write the custom JS Function like so: Here we pre-select the rows 1,3 and 4 (note that the count starts from 0)

library(DT)
library(tidyverse)
library(shiny)

ui <- fluidPage(
  dataTableOutput("irisTable")
)

jsfunc <- "function() {arrIndexes=[1,3,4]; $('#irisTable tbody tr').filter(function(index) {return arrIndexes.indexOf(index) > -1;}).click()}"

server <- function(input, output){

  output$irisTable <- renderDataTable(
    iris %>% rowid_to_column("Row") %>% mutate(Row = ""),
    rownames = FALSE,
    extensions = "Select",
    options = list(
      initComplete = JS(jsfunc),
      columnDefs = list(list(className = "select-checkbox", targets = 0, orderable = FALSE)),
      select = list(style = "multi", selector = "td:first-child")
    ))
}

shinyApp(ui, server)

这篇关于是否可以在DT数据表中设置默认选中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆