2

我正在尝试使用该rhandsontable库生成一个表,其中我结合了包的两个漂亮功能:(1)使用客户renderer参数突出显示行,以及(2)布尔字段的复选框列类型。

就其本身而言,这两个功能都可以正常工作。例如,下面显示了行突出显示的工作原理

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

row_highlight = c(5, 7)

rhandsontable(df,
              row_highlight = row_highlight) %>%
hot_cols(renderer = "
  function(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);

    tbl = this.HTMLWidgets.widgets[0]

    hrows = tbl.params.row_highlight
    hrows = hrows instanceof Array ? hrows : [hrows] 

    if (hrows.includes(row)) {
      td.style.background = 'pink';
    }

    return td;
}") 

下面显示了复选框功能的工作原理

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

rhandsontable(df, row_highlight = row_highlight) %>%
  hot_col(col = 'bool', type = 'checkbox')

我的问题:您如何结合这两个功能(即,生成具有突出显示的行和功能复选框的表格)?

有人会认为下面会起作用,但复选框不会呈现(而是显示为trueand false):

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

row_highlight = c(5, 7)

rhandsontable(df,
              row_highlight = row_highlight) %>%
  hot_cols(renderer = "
      function(instance, td, row, col, prop, value, cellProperties) {
        Handsontable.renderers.TextRenderer.apply(this, arguments);

        tbl = this.HTMLWidgets.widgets[0]

        hrows = tbl.params.row_highlight
        hrows = hrows instanceof Array ? hrows : [hrows] 

        if (hrows.includes(row)) {
          td.style.background = 'pink';
        }

        return td;
    }") %>%
  hot_col(col = 'bool', type = 'checkbox')

组合尝试失败的结果

有没有办法在 R 的 rhandsontable 中结合复选框和行突出显示?

4

1 回答 1

3

应将不同的渲染器应用于具有文本/数字逻辑变量的列,以便结合复选框功能和行突出显示

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

row_highlight = c(5, 7)

rhandsontable(df,
              row_highlight = row_highlight) %>%
  hot_col(col = names(df)[!names(df) %in% "bool"], 
          renderer = "
          function(instance, td, row, col, prop, value, cellProperties) {
            Handsontable.renderers.TextRenderer.apply(this, arguments);

            tbl = this.HTMLWidgets.widgets[0]

            hrows = tbl.params.row_highlight
            hrows = hrows instanceof Array ? hrows : [hrows] 

            if (hrows.includes(row)) {
              td.style.background = 'pink';
            }

            return td;
          }") %>%
  hot_col(col = "bool",
          renderer = "
          function(instance, td, row, col, prop, value, cellProperties) {
            Handsontable.renderers.CheckboxRenderer.apply(this, arguments);

            tbl = this.HTMLWidgets.widgets[0]

            hrows = tbl.params.row_highlight
            hrows = hrows instanceof Array ? hrows : [hrows] 

            if (hrows.includes(row)) {
              td.style.background = 'pink';
            }

            return td;
          }")

在此处输入图像描述

于 2018-06-08T12:46:01.613 回答