4

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

               }")
M.Qasim
  • 1,827
  • 4
  • 33
  • 58

1 Answers1

6

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'
           }
        }")
Kipras Kančys
  • 1,617
  • 1
  • 15
  • 20
  • Works perfectly... Thanks! – M.Qasim Nov 21 '17 at 00:09
  • Doesn't work for me, just copying and pasting the same code above. using rhandsontable v0.3.6 – Mridul Garg Nov 13 '18 at 18:28
  • @KiprasKančys, can you please enlighten me on how to check for string length in a cell or cells in a column and update background color if it exceeds a value? – RanonKahn Jun 05 '19 at 16:02
  • Try this: `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' } }")` – Kipras Kančys Jun 06 '19 at 06:04