Nikhil Bharadwaj
Nikhil Bharadwaj

Reputation: 917

How to apply style to a cell based on the condition/value of another cell in Handsontable

I want to apply some style to a particular cell in Handsontable using custom renderer functions. Style has to be applied using a if condition,where I need to check the value of another cell in the handsontable.

Is there any way to achieve this using custom renderers?

Upvotes: 0

Views: 492

Answers (2)

Tobias Krabel
Tobias Krabel

Reputation: 686

Here is an example of how e.g. to color a complete row based on a value given in a specific column. With instance.getData() you retrieve the complete data in the rhandsontable object. You can access specific cells using indexing.

library(rhandsontable)

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

text_renderer <- "
  function (instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);
    var col_value = instance.getData()[row][2]
    if (col_value == 'C') {
      td.style.background = 'pink';
    } else if (col_value == 'D') {
      td.style.background = 'green';
    }
  }"

bool_renderer <- "
  function (instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.CheckboxRenderer.apply(this, arguments);
    var col_value = instance.getData()[row][2]
    if (col_value == 'C') {
      td.style.background = 'pink';
    } else if (col_value == 'D') {
      td.style.background = 'green';
    }
  }
"

rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
  hot_col(col = c(2, 3, 4), renderer = text_renderer) %>%
  hot_col("bool", renderer = bool_renderer)

Upvotes: 1

adriennetacke
adriennetacke

Reputation: 1746

There is a great demo on how to implement conditional formatting in their docs.

Upvotes: 0

Related Questions