在rhansontable中基于单元格值的颜色行 [英] color row based on cell value in rhandsontable

查看:92
本文介绍了在rhansontable中基于单元格值的颜色行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据单元格值在我闪亮的应用程序中为整行的rhandsontable上色.

I am struggling to color full row of rhandsontable in my shiny app based on a cell value.

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

In the following example, I would like to format full row instead of one cell.

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'}

               }")

推荐答案

renderer的思想是将其分别应用于所有单元格,因此同时,单元格值不能为'F'和'f'.对于每一行,您必须选择第2列以检查值是否为'F',然后选择第3列以检查值是否为'f'.

The idea of renderer is that applied to all cells separately, so at the same time cell value can't be a 'F' and 'f'. For every row you have to select column 2 to check if value is 'F' and select column 3 to check if value is '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'
           }
        }")

这篇关于在rhansontable中基于单元格值的颜色行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆