【发布时间】:2016-09-20 11:51:43
【问题描述】:
我想使用自定义渲染器函数将一些样式应用于 Handsontable 中的特定单元格。必须使用 if 条件应用样式,我需要检查handsontable 中另一个单元格的值。
有没有办法使用自定义渲染器来实现这一点?
【问题讨论】:
我想使用自定义渲染器函数将一些样式应用于 Handsontable 中的特定单元格。必须使用 if 条件应用样式,我需要检查handsontable 中另一个单元格的值。
有没有办法使用自定义渲染器来实现这一点?
【问题讨论】:
great demo 在他们的文档中介绍了如何实现条件格式。
【讨论】:
这是一个例子,例如根据特定列中给定的值为整行着色。使用instance.getData(),您可以检索 rhandsontable 对象中的完整数据。您可以使用索引访问特定单元格。
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)
【讨论】: