我正在关注如何从单击的行中获取元素的this example。这样可以正常工作,除非它不突出显示所选行。这是代码:
library(DT)
library(shiny)
runApp(shinyApp(
ui = fluidPage(DT::dataTableOutput('tab'), verbatimTextOutput('row')),
server = function(input, output) {
output$tab = DT::renderDataTable({
datatable(iris, selection = 'single',
callback = JS("table.on('click.dt', 'tr',
function() {
$(this).toggleClass('selected');
Shiny.onInputChange('row', table.rows('.selected').data().toArray());
});")
)
})
output$row = renderText({input$row})
}
))
答案 0 :(得分:0)
为什么DT input$..._rows_selected
中不存在?
喜欢
library(shiny)
library(DT)
runApp(shinyApp(
ui = fluidPage(DT::dataTableOutput('tab'), verbatimTextOutput('row')),
server = function(input, output) {
output$tab = DT::renderDataTable({
datatable(iris, selection = "single"
)
})
output$row = renderText({if(length(input$tab_rows_selected)>0) paste(input$tab_rows_selected,as.character(iris[input$tab_rows_selected,]))})
}
))
例如你有dunamic数据
dt1=reactive({
return(iris[iris$Species %in% c("setosa","versicolor"),])})
可能还有其他数据操作
比您的代码
runApp(shinyApp(
ui = fluidPage(DT::dataTableOutput('tab'), verbatimTextOutput('row')),
server = function(input, output) {
dt1=reactive({
return(iris[iris$Species %in% c("setosa","versicolor"),])})
output$tab = DT::renderDataTable({
datatable(dt1(), selection = "single"
)
})
output$row = renderText({if(length(input$tab_rows_selected)>0) paste(input$tab_rows_selected,as.character(dt1()[input$tab_rows_selected,]))})
}
))
我的DT
版本0.1.45
不知道为什么,但我认为$(this).toggleClass('selected');
删除所选的删除类在标准DT包中添加(toggleClass
使用了两次而你什么都没看到。)
尝试
JS("table.on('click.dt', 'tr',
function() {
Shiny.onInputChange('row', table.rows(this).data().toArray());
});")
或设置selection='none'