排序后的单元颜色重置
我正在尝试将一个单元格在数据表中更改其背景颜色后。 stéphane's>stéphane's解决方案在一定程度上起作用,一旦表格或过滤表,颜色就会消失。有没有办法跟踪编辑的单元格,并且由于浏览器端在浏览器端被操纵而进行了颜色?
以下是Stéphane开发的解决方案,也是一个很好的可再现示例。
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, selection = "none")
}, 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)
I am trying to have a cell in a Datatable change its background color once it is edited. While Stéphane's solution works to an extent, the colors disappear once the table is sorted or filtered. Is there a way to track the edited cell and have it colored as the Datatable is manipulated on the browser end?
Below is the solution as developed by Stéphane and a good reproducible example to start with.
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, selection = "none")
}, 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您正在为可见的行着色,这不必与物理行对应。
一种可能的解决方案是在 server 方面进行着色,这样的
想法是:为:
_changed
的辅助列(在突变
调用)反应性
,该保存当前数据,包括用户完成的更改。renderdt
函数中,您可以参考reactiveVal
(而不是静态data.frame.frame
),隐藏助手列并使用格式>格式>格式
根据关联*_更改
列中的相应值对彩色单元格进行彩色。ObserveEvent
来保持客户端和服务器保持同步,在其中我们通过相应的*_更改
列来标记一个单元格,并更改值客户端的更改。nb 请注意,当JS基于0时,您的r是1,因此您需要通过
-1 -1
来抵消目标索引定位在JS中为0。无论出于奇怪的原因,这都不适用于formatstyle
函数。更新
以保持过滤器持续的更新,您需要存储它们并将其存储重新应用它们:
主要的想法是,渲染后,我们存储搜索字符串 - 既用于总体搜索(
dtable_search
),又要用于潜在的列特定搜索(dtable_search_columns
) 。然后,我们要做的就是将相同的搜索应用于datatableProxy
对象。The problem is that you are coloring the visible row, which need not to correspond with the physical row.
One possible solution is to do the coloring on the server side, like this:
The idea is to:
_changed
(done in themutate
call)reactive
which holds the current data including changes done by the user.renderDT
function you refer to thereactiveVal
(instead of the staticdata.frame
), hide the helper columns and useformatStyle
to color cells according to the corresponding value in the associated*_changed
column.observeEvent
, where we flag a cell as changed via the corresponding*_changed
column and also change the value to refelct the changes on the client side.N.B. Be aware that you R is 1 based while JS is 0 based, hence you need to offset your target index by
-1
(becasue I removed the row names whihc would otherwise be positioned at 0 in JS). This, for whatever strange reason, does not apply for theformatStyle
function.Update
To keep the filters persistent, you need to store them and to re-apply them:
The main idea is that upon rendering, we store the search strings - both for the overall search (
dtable_search
) and for potential column specific searches (dtable_search_columns
). Then all we have to do is to apply the same searches to thedataTableProxy
object.