我正在创建一个包含数据表的闪亮仪表板。在此数据表中,我创建了一个列,其中包含具有完全相同的ID 'inf'
的按钮(使用columnDefs的选项),并且我使用onclick()
处理了按钮事件以打开特定的modalDialog(下面的代码)。问题在于,由于我所有的按钮都具有相同的ID,因此onclick
函数仅在一个按钮中起作用(请参见Demo)。那么如何使所有按钮都打开同一个modalDialog?或如何为按钮赋予不同的ID?
server.R
df = mtcars
df$Description = NA
df = df[,c(12,1,2,3,4,5,6,7,8,9,10,11)]
output$table <- DT::renderDataTable(
df,
options = list( paging = FALSE, scrollY = 354,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': 'white', 'color': 'black', 'font-weight' : 'bold'});",
"}"),
columnDefs = list(
list(className = 'dt-center',
targets = 1,
data = NULL,
defaultContent = "<button id='inf' type='button' class='btn btn-default action-button shiny-bound-input'><i class= 'fa fa-question-circle'/></button>"
)
)
),
selection = 'single',
style = 'bootstrap'
)
onclick("inf",{
showModal(
modalDialog( h4(input$table_rows_selected))
)
})
答案 0 :(得分:0)
library(shiny)
library(DT)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
DTOutput("table")
)
server <- function(input, output){
df = mtcars
df$Description = sapply(1:nrow(df), function(i){
sprintf("<button id='inf%d' type='button' class='btn btn-default action-button shiny-bound-input'><i class= 'fa fa-question-circle'/></button>",i)
})
df = df[,c(12,1,2,3,4,5,6,7,8,9,10,11)]
output$table <- renderDT(
df, escape = FALSE,
options = list( paging = FALSE, scrollY = 354,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': 'white', 'color': 'black', 'font-weight' : 'bold'});",
"}")
),
selection = 'single',
style = 'bootstrap'
)
for(i in 1:nrow(df)){
onclick(paste0("inf",i), {
showModal(
modalDialog( h4(input$table_rows_selected))
)
})
}
}
shinyApp(ui, server)