【问题标题】:How to apply style to a cell based on the condition/value of another cell in Handsontable如何根据 Handsontable 中另一个单元格的条件/值将样式应用于单元格
【发布时间】:2016-09-20 11:51:43
【问题描述】:

我想使用自定义渲染器函数将一些样式应用于 Handsontable 中的特定单元格。必须使用 if 条件应用样式,我需要检查handsontable 中另一个单元格的值。

有没有办法使用自定义渲染器来实现这一点?

【问题讨论】:

    标签: javascript handsontable


    【解决方案1】:

    great demo 在他们的文档中介绍了如何实现条件格式。

    【讨论】:

      【解决方案2】:

      这是一个例子,例如根据特定列中给定的值为整行着色。使用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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-25
        • 2019-06-16
        • 2019-05-21
        • 2013-12-31
        • 1970-01-01
        • 2020-08-11
        • 1970-01-01
        相关资源
        最近更新 更多