Reputation: 917
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
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
Reputation: 1746
There is a great demo on how to implement conditional formatting in their docs.
Upvotes: 0