【问题标题】:color row based on cell value in rhandsontable基于 rhandsontable 中单元格值的颜色行
【发布时间】:2018-01-18 00:43:45
【问题描述】:

我正在努力根据单元格值在我闪亮的应用程序中为整行 rhandsontable 着色。

在下面的例子中,我想格式化整行而不是一个单元格。

library(rhandsontable)

DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10],
                small = letters[1:10],
                dt = seq(from = Sys.Date(), by = "days", length.out = 10),
                stringsAsFactors = FALSE)

col_highlight = 2
row_highlight = c(5, 7)

rhandsontable(DF, width = 550, height = 300) %>%
  hot_cols(renderer = "
               function (instance, td, row, col, prop, value, cellProperties) {
               Handsontable.renderers.TextRenderer.apply(this, arguments);

               if(value == 'F' | value == 'f') {
               td.style.background = 'pink';
               } else if(value == 'J' | value == 'j') {
               td.style.background = 'lightgreen';
               } else if(value == 'X' | value == 'x') {
               td.style.background = 'lightblue'}

               }")

【问题讨论】:

    标签: r rhandsontable


    【解决方案1】:

    渲染器的思想是分别应用于所有单元格,因此单元格值不能同时是'F'和'f'。对于每一行,您必须选择第 2 列以检查值是否为“F”,并选择第 3 列以检查值是否为“f”。

    rhandsontable(DF, width = 550, height = 300) %>%
    hot_cols(renderer = "
               function (instance, td, row, col, prop, value, cellProperties) {
               Handsontable.renderers.TextRenderer.apply(this, arguments);
    
               if(instance.getData()[row][2] == 'F' | instance.getData()[row][3] == 'f'){
                    td.style.background = 'pink';
               } else if(instance.getData()[row][2] == 'J' | instance.getData()[row][3] == 'j') {
                    td.style.background = 'lightgreen';
               } else if(instance.getData()[row][2] == 'X' | instance.getData()[row][3] == 'x') {
                    td.style.background = 'lightblue'
               }
            }")
    

    【讨论】:

    • 完美运行...谢谢!
    • 对我不起作用,只是复制和粘贴上面相同的代码。使用 rhandsontable v0.3.6
    • @KiprasKančys,您能告诉我如何检查一个或多个单元格中的字符串长度,并在超过某个值时更新背景颜色吗?
    • 试试这个:rhandsontable(data.frame(a = c('a' , 'ab', 'abc', 'abcd'))) %>% hot_cols(renderer = " function (instance, td, row, col, prop, value, cellProperties) { Handsontable.renderers.TextRenderer.apply(this, arguments); if(instance.getData()[row][0].length > 2) { td.style.background = 'pink' } }")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多