【问题标题】:rhandsontable with row highlighting and checkbox columns具有行突出显示和复选框列的 rhandsontable
【发布时间】:2018-09-19 17:47:24
【问题描述】:

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

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

【问题讨论】:

    标签: javascript r shiny handsontable rhandsontable


    【解决方案1】:

    应将不同的渲染器应用于具有 text/numericlogical 变量的列,以便结合复选框功能和行突出显示。 p>

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      相关资源
      最近更新 更多