rhandsontable具有行突出显示和复选框列

时间:2018-04-10 07:40:34

标签: javascript r shiny handsontable rhandsontable

我正在尝试使用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')

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

有人会认为以下内容可行,但复选框不会呈现(而是显示为truefalse):

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')

Result of failed combination attempt

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

1 个答案:

答案 0 :(得分:0)

应使用 text / numeric 逻辑变量将不同的渲染器应用于列,以便将复选框功能和突出显示的行组合在一起。

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;
          }")

The resulting rendered table