DataTables DT:单击的单元格的重置值 [英] DataTables DT: reset value of clicked cell
问题描述
我想添加单击表格单元格后发生的事情的功能(例如,打开模式)。因为(假设我的dt具有ID dt) input $ dt_cell_clicked
保持不变,直到单击新的单元格,所以在重新单击该事件时无法执行相同的事件
I wanted to add the functionality of something happening after a table cell was clicked (e.g. open a modal). Because (suppose my dt is has the ID "dt") input$dt_cell_clicked
stays the same until I click a new cell, I cannot execute the same event on re-clicking that cell.
我尝试解决此问题,并使用javascript手动重置 input $ dt_cell_clicked
。此方法有效,但是DT中似乎有一个内部updatemarker,它注意到我之前单击了该单元格,并且没有将 input $ dt_cell_clicked
的值设置为单击的值。
I tried working around it resetting input$dt_cell_clicked
with javascript manually. This works, but there seems to be an internal updatemarker in DT that noticed I clicked the cell before and does not set the value of input$dt_cell_clicked
to the clicked value. Is there a workaround or is this a bug?
谢谢!
最小示例:
library(shiny)
library(shinyjs)
ui <- fluidPage(
h2("Last clicked:"),
verbatimTextOutput("last_clicked"),
actionButton("reset", "Reset clicked value"),
h2("Datatable:"),
DT::dataTableOutput("dt"),
useShinyjs(),
extendShinyjs(text = paste0("shinyjs.resetDTClick = function() { Shiny.onInputChange('dt_cell_clicked', null); }"))
)
server <- function(input, output) {
# the last clicke value
output$last_clicked <- renderPrint({
str(input$dt_cell_clicked)
})
output$dt <- DT::renderDataTable({
DT::datatable(head(mtcars, 2))
})
observeEvent(input$dt_cell_clicked, {
validate(need(length(input$dt_cell_clicked) > 0, ''))
alert("You clicked something!")
})
observeEvent(input$reset, {
js$resetDTClick()
})
}
shinyApp(ui, server)
推荐答案
在提示使用代理(格雷戈·德·克里利亚)后,我发现了一种绕行的解决方法:最后选择的单元格。观察到最后一个选定的单元格,并根据新的选择执行某些操作。
After the hint of using a proxy (Gregor de Cillia), I found a workaround using a detour: the last selected cell. This last selected cell is observed and some action is executed upon a new selection. We can reset the selection by using a DT proxy.
library(shiny)
ui <- fluidPage(
h2("Last clicked:"),
verbatimTextOutput("last_clicked"),
actionButton("reset", "Reset clicked value"),
h2("Datatable:"),
DT::dataTableOutput("dt")
)
server <- function(input, output) {
# the last clicked=selected value
output$last_clicked <- renderPrint({
str(input$dt_rows_selected)
})
output$dt <- DT::renderDataTable({
DT::datatable(head(mtcars, 2), selection = 'single')
})
# do some action after selecting a value
observeEvent(input$dt_rows_selected, {
validate(need(!is.null(input$dt_rows_selected), ''))
print("You clicked something!")
})
myProxy = DT::dataTableProxy('dt')
# reset last selected value using the proxy
observeEvent(input$reset, {
DT::selectRows(myProxy, NULL)
})
}
shinyApp(ui, server)
这篇关于DataTables DT:单击的单元格的重置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!