简体   繁体   中英

How to trigger edit on single click in R Shiny DT datatable

I have a Shiny app with an editable DT table. Everything is working perfectly on desktop: I double click on a cell, edit the value, the value is processed in the background, and the whole table is updated appropriately.

However, on Chrome for Android double-click doesn't seem to be an option, so I can't edit the cells in my table.

After much searching for solutions, I tried to add a callback to trigger a doubleclick on a single click, but it doesn't have any effect. I give a minimal example below.

library(shiny)
library(DT)
ui <- fluidPage( mainPanel( DTOutput("table") ) )
server <- function(input, output, session) {
  NumDays <- 14
  DayNum <- seq_len(NumDays)
  DF <- data.frame(Day=DayNum, Week=((DayNum-1) %/% 7 + 1), Steps=rep(0, NumDays), row.names=NULL)
  proxy <- dataTableProxy('table')
  
  js <- "table.on('click', 'tr', function() { 
  table.cell(this).trigger('dblclick.dt');
  });"

  output$table <- renderDT(datatable(DF, rownames = FALSE, selection = list(mode = 'none'), editable = list(target='cell'), options = list(scrollY = 600, searching = FALSE, paging=FALSE, ordering=FALSE, info=FALSE), callback = JS(js)))
  
  proxy <- dataTableProxy('table')
  observeEvent(input$table_cell_edit, {
    info <- input$table_cell_edit
    i <- info$row;    j <- info$col+1;    v <- info$value
    currentvalue <- abs(as.numeric(v))
    DF[i, j] <<- currentvalue + 10
    replaceData(proxy, DF, rownames = FALSE)
  })
}

shinyApp(ui = ui, server = server)

I just tweaked your example with a bit jquery and got it to work - also note I changed 'tr' to 'td':

library(shiny)
library(DT)
ui <- fluidPage( mainPanel( DTOutput("table") ) )
server <- function(input, output, session) {
  NumDays <- 14
  DayNum <- seq_len(NumDays)
  DF <- data.frame(Day=DayNum, Week=((DayNum-1) %/% 7 + 1), Steps=rep(0, NumDays), row.names=NULL)
  proxy <- dataTableProxy('table')
  
  js <- "table.on('click', 'td', function() { 
    $(this).dblclick();
  });"
  
  output$table <- renderDT(datatable(DF, rownames = FALSE, selection = list(mode = 'none'), editable = list(target='cell'), options = list(scrollY = 600, searching = FALSE, paging=FALSE, ordering=FALSE, info=FALSE), callback = JS(js)))
  
  proxy <- dataTableProxy('table')
  observeEvent(input$table_cell_edit, {
    info <- input$table_cell_edit
    i <- info$row;    j <- info$col+1;    v <- info$value
    currentvalue <- abs(as.numeric(v))
    DF[i, j] <<- currentvalue + 10
    replaceData(proxy, DF, rownames = FALSE)
  })
}

shinyApp(ui = ui, server = server)

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM