【问题标题】:Rhandsontable: Conditional formatting using functions of 'value' stringsRhandsontable:使用“值”字符串函数的条件格式
【发布时间】:2018-07-08 23:46:10
【问题描述】:

我正在尝试通过调整单元格的子集字符串值来为 R 中的 handonstable 的单个单元格着色:

library(rhandsontable)

DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
                 small = letters[1:10],
                 stringsAsFactors = FALSE)

###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
    hot_cols(renderer = "
             function (instance, td, row, col, prop, value, cellProperties) {
             Handsontable.renderers.NumericRenderer.apply(this, arguments);

             if (value == 'C') {
             td.style.background = 'pink';
             } else if (value == 'D') {
             td.style.background = 'green';
             }
             }")

所以上面的工作正常,但是当我添加一个子集字符串的函数时,整个事情就崩溃了:

library(rhandsontable)

DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
                 small = letters[1:10],
                 stringsAsFactors = FALSE)

###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
    hot_cols(renderer = "
             function (instance, td, row, col, prop, value, cellProperties) {
             Handsontable.renderers.NumericRenderer.apply(this, arguments);


             if (value.substring(0, 1) == 'C') {
             td.style.background = 'pink';
             } else if (value == 'D') {
             td.style.background = 'green';
             }
             }")

我需要使用 substring 函数来确定单元格内容的前几个字母,以便我可以相应地为它们着色。我该怎么做?

我的Java知识也是零。只知道R。

谢谢

亚历克斯

【问题讨论】:

    标签: javascript r conditional-formatting rhandsontable


    【解决方案1】:

    在使用substring 之前,您必须使用toString 方法将值转换为字符串。

    您的代码将如下所示:

    DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
                     small = letters[1:10],
                     stringsAsFactors = FALSE)
    
    ###Checkboxes not Present/Entire row not highlighted
    
        rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
          hot_cols(renderer = "
                   function (instance, td, row, col, prop, value, cellProperties) {
                   Handsontable.renderers.NumericRenderer.apply(this, arguments);
                   if (value.toString().substring(0, 1) == 'C') {
                   td.style.background = 'pink';
                   } else if (value == 'D') {
                   td.style.background = 'green';
                   }
                   }")
    

    希望对你有帮助!

    【讨论】:

    • 谢谢,stackoverflow 还是新手。
    猜你喜欢
    • 2016-08-10
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2014-02-24
    • 2017-04-20
    • 1970-01-01
    相关资源
    最近更新 更多