簡體   English   中英

shiny DT,更新值后更改單元格顏色,不被選擇覆蓋

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

基本上我想要的是這兩個回答帖子的組合

在 Rshiny 中編輯其值時更改數據表單元格的背景顏色

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 )。

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.

在單元格編輯后啟用單元格着色的代碼(來自@StéphaneLaurent 的代碼(我只更改了一個小東西,以便現在可以進行選擇),但是這里的選擇“覆蓋”了黃色單元格。 甚至有可能實現我想要的嗎?

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