我想突出显示
rhandsontable
中满足特定条件的所有行。
不幸的是,我在网上既没有找到解决方案,也没有找到建议的解决方案。
这是我的可重现示例:
library(tidyverse)
library(rhandsontable)
rhandsontable(mtcars) %>%
hot_col(col = "cyl", renderer = "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if ( value === 4 ) {
td.style.background = '#ff000090';
}
}
")
在此示例中,
cyl
值是4
的所有行都应以红色突出显示。但是,我不希望单个单元格被染成红色,而是整行。
有人可以帮助我用 Javascript 进行调整吗?
这是一种解决方案,不是最优雅的,但它有效。我必须将 cyl 列放在最后一个位置来为整行着色,并识别满足条件的 td。
rhandsontable(mtcars[, c(setdiff(names(mtcars), "cyl"), "cyl")]) %>%
hot_col(col = "cyl", renderer = '
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if (value === 4) {
$(td).parent().find("td").css("background-color", "#ff000090");
}
}
')
我根据这个问题找到了解决方案:
根据一列中的字符串值对 rhandsontable 中的整行进行着色
mtcars %>%
rhandsontable(
row_highlight = which(.$cyl == 4) -1
) %>%
hot_cols(renderer = "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
if (instance.params) {
hrows = instance.params.row_highlight
hrows = hrows instanceof Array ? hrows : [hrows]
hcols = instance.params.col_highlight
hcols = hcols instanceof Array ? hcols : [hcols]
if (hrows.includes(row)) {
td.style.background = '#ff000090'
}
}
}"
)