R Rhandsontable 根据条件突出显示整行

问题描述 投票:0回答:1

我想突出显示

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 进行调整吗?

javascript r conditional-formatting rhandsontable
1个回答
0
投票

这是一种解决方案,不是最优雅的,但它有效。我必须将 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");
    }
  }
  ')

© www.soinside.com 2019 - 2024. All rights reserved.