Rhandsontable条件格式-如何根据特定的属性值突出显示行? [英] Rhandsontable conditional formatting- How to highlight rows based on specific attribute value?
问题描述
我想根据一个值对整个行应用颜色突出显示,并保留rhandsontable的复选框功能.在下面的简单示例中,我希望第3行为粉红色,第4行为绿色.
I'd like to apply color highlighting for a entire row depending on a value and retain check boxes functionality of rhandsontable. In the simple example below, I'd like row 3 to be pink and row 4 to be green.
library(rhandsontable)
DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
hot_cols(renderer = "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if (value == 'C') {
td.style.background = 'pink';
} else if (value == 'D') {
td.style.background = 'green';
}
}")
####Checkboxes Present
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300)
推荐答案
问题是您使用NumericRenderer尝试将应用的列转换为数值.我的解决方案可能不是最佳解决方案,但确实可以解决问题.
The issue is that you use a NumericRenderer which tries to convert the applied columns to numeric. My solution may not be optimal, but it does the job.
library(rhandsontable)
DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
small = letters[1:10],
stringsAsFactors = FALSE)
# Text Renderer
text_renderer <- "
function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
# This is the column which you want to check for coloring
var col_value = instance.getData()[row][2]
if (col_value == 'C') {
td.style.background = 'pink';
} else if (col_value == 'D') {
td.style.background = 'green';
}
}"
# Renderer for the bool column
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';
}
}
"
# Entire row highlighted and checkbox attribute preserved
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
hot_col(col = c(2, 3, 4), renderer = text_renderer) %>%
hot_col("bool", renderer = bool_renderer)
诀窍是创建两个渲染器:
The trick is that you create two renderers:
- 专门用于bool列的CheckboxRenderer
- 其余列的TextRenderer.
instance.getData()[row][2]
检索行索引row
处的大列的值.因此,渲染器针对每一行检查if语句中的条件是否为TRUE.
instance.getData()[row][2]
retrieves the value of the big column at row index row
. So the renderer checks for each row whether the condition in the if-statement is TRUE.
欢呼
这篇关于Rhandsontable条件格式-如何根据特定的属性值突出显示行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!