简体   繁体   English

shiny DT,更新值后更改单元格颜色,不被选择覆盖

[英]shiny DT, change cell color after updated value, not overwritten by selection

Basically what I want is a combination of these two answered posts基本上我想要的是这两个回答帖子的组合

Change backgorund color of cell of data table while its value is edited in Rshiny 在 Rshiny 中编辑其值时更改数据表单元格的背景颜色

Shiny with DT Select rows, keep cell color Shiny 与 DT Select 行,保持单元颜色

so I want the cell color to change after each edit on the client side, but when the rows with an edited cell are selected, I need the selection highlight to not overwrite the cell colorization, (so that it looks like this https://i.stack.imgur.com/K2Gjv.png ). so I want the cell color to change after each edit on the client side, but when the rows with an edited cell are selected, I need the selection highlight to not overwrite the cell colorization, (so that it looks like this https:// i.stack.imgur.com/K2Gjv.png )。

Difference between my problem and the one here Shiny with DT Select rows, keep cell color is that in my case the cell colors which need to keep their cell colors can not be hardocoded as they are selected by the client. Difference between my problem and the one here Shiny with DT Select rows, keep cell color is that in my case the cell colors which need to keep their cell colors can not be hardocoded as they are selected by the client.

code which enables the cell colorization after cell edit (code from @StéphaneLaurent (I only changed a minor thing so that now selections are possible)), however here the selection "overwrites" the yellow cells.在单元格编辑后启用单元格着色的代码(来自@StéphaneLaurent 的代码(我只更改了一个小东西,以便现在可以进行选择),但是这里的选择“覆盖”了黄色单元格。 Is it even possible to achieve what I want?甚至有可能实现我想要的吗?

library(shiny)
library(shinyjs)
library(DT)

js <- HTML(
  "function colorizeCell(i, j){
    var selector = '#dtable tr:nth-child(' + i + ') td:nth-child(' + j + ')';
    $(selector).css({'background-color': 'yellow'});
  }"
)

colorizeCell <- function(i, j){
  sprintf("colorizeCell(%d, %d)", i, j)
}

ui <- fluidPage(
  useShinyjs(),
  tags$head(
    tags$script(js)
  ),
  br(),
  DTOutput("dtable")
)

dat <- iris[1:5, ]

server <- function(input, output, session){
  
  output[["dtable"]] <- renderDT({
    datatable(dat, editable = TRUE)
  }, server = FALSE)
  
  observeEvent(input[["dtable_cell_edit"]], {
    info <- input[["dtable_cell_edit"]]
    i <- info[["row"]]
    j <- info[["col"]]
    runjs(colorizeCell(i, j+1))
  })
  
}

shinyApp(ui, server)
library(shiny)
library(shinyjs)
library(DT)

css <- HTML(
  "table.dataTable tr.selected td.yellow {
     background-color: yellow !important
   }
   td.yellow {
     background-color: yellow !important
   }"
)

js <- HTML(
  "function colorizeCell(i, j){
    var selector = '#dtable tr:nth-child(' + i + ') td:nth-child(' + j + ')';
    $(selector).addClass('yellow');
  }"
)

colorizeCell <- function(i, j){
  sprintf("colorizeCell(%d, %d)", i, j)
}

ui <- fluidPage(
  useShinyjs(),
  tags$head(
    tags$style(css),
    tags$script(js)
  ),
  br(),
  DTOutput("dtable")
)

dat <- iris[1:5, ]

server <- function(input, output, session){
  
  output[["dtable"]] <- renderDT({
    datatable(dat, editable = TRUE)
  }, server = FALSE)
  
  observeEvent(input[["dtable_cell_edit"]], {
    info <- input[["dtable_cell_edit"]]
    i <- info[["row"]]
    j <- info[["col"]]
    runjs(colorizeCell(i, j+1))
  })
  
}

shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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