首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改rhandsontable中几行的背景色

更改rhandsontable中几行的背景色
EN

Stack Overflow用户
提问于 2016-09-28 16:01:11
回答 1查看 1.5K关注 0票数 1

使用

代码语言:javascript
复制
library("rhandsontable")
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%    
hot_col(c(1,3), 
  renderer = "function(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.TextCell.renderer.apply(this, arguments);
    td.style.background = 'lightblue';
  }"
)

您可以定义所选列的背景色,此处列1和3。

对选定的行也可以这样做吗?

如果我直接引用行,这是可行的:

代码语言:javascript
复制
library("rhandsontable")
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_cols(
  renderer = "function(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.TextCell.renderer.apply(this, arguments);
    if (row==1 || row==3 || row==4) td.style.background = 'lightblue';
  }"
)

但是,我想要提供的向量中有行索引,类似这样的内容(这不起作用,大概是因为渲染器函数看不到myindex):

代码语言:javascript
复制
myindex <- c(1, 3, 4)
rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
hot_cols(
  renderer = "function(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.TextCell.renderer.apply(this, arguments);
    if (row in myindex) td.style.background = 'lightblue';}
  }"
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-18 07:12:16

这可能不是最有效的答案,但它有效:您可以使用paste()/paste0()将r对象添加到javascript代码中,如下所示:

代码语言:javascript
复制
myindex <- c(1, 3, 4)

rhandsontable(data.frame(ID=1:5,var1=rnorm(5), var2=letters[1:5])) %>%
  hot_cols(
    renderer = paste0(
      "
      function(instance, td, row, col, prop, value, cellProperties) {
        Handsontable.TextCell.renderer.apply(this, arguments);
        var row_index = ", paste("[", paste(myindex, collapse = ","), "]"), ";
        for (i = 0; i < row_index.length; i++)
        if (row == row_index[i]) {
          td.style.background = 'lightblue';
        }
      }
      "
    )
  )
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39752455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档