【问题标题】:How to change the colour of some rows with rhansontable and checkbox?如何使用 rhansontable 和复选框更改某些行的颜色?
【发布时间】:2022-01-02 17:15:31
【问题描述】:

当用户取消选中其中一个单元格中的复选框时,我想更改整行的颜色。 (可以是多行)

到目前为止,我只能更改包含复选框的单元格的颜色,而不是整行。在这个例子中,整行 6 应该是彩色的。

library(shiny)
library(rhandsontable)

# Define UI for application that draws a histogram
ui <- fluidPage(
    rHandsontableOutput('table')
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    df <- data.frame(alphabet = letters[1:10],
                     include = TRUE)

    output$table <- rhandsontable::renderRHandsontable({

        rhandsontable(df, height = 500) %>%
        hot_col(col = "include",
                renderer = "
                function (instance, td, row, col, prop, value, cellProperties) {
                
                    Handsontable.renderers.CheckboxRenderer.apply(this, arguments);

                    var col_value = instance.getData()[row][1]

                    if (col_value === false) {
                    
                        td.style.background = 'pink';
                    }
                }
            ")
        })
}

# Run the application
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: javascript r shiny rhandsontable


    【解决方案1】:

    解决了。

    library(shiny)
    library(rhandsontable)
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
        rHandsontableOutput('table')
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
    
        df <- data.frame(alphabet = letters[1:10],
                         include = TRUE)
    
        output$table <- rhandsontable::renderRHandsontable({
    
            rhandsontable(df, height = 500) %>%
            hot_col(col = "include",
                    renderer = "
                    function (instance, td, row, col, prop, value, cellProperties) {
                    
                        Handsontable.renderers.CheckboxRenderer.apply(this, arguments);
    
                        var col_value = instance.getData()[row][1]
    
                        if (col_value === false) {
                        
                            td.style.background = 'pink';
                        }
                    }
                ") %>%
            hot_col(col = "alphabet",
                    renderer = "
                    function (instance, td, row, col, prop, value, cellProperties) {
                    
                        Handsontable.renderers.TextRenderer.apply(this, arguments);
    
                        var col_value = instance.getData()[row][1]
    
                        if (col_value === false) {
                        
                            td.style.background = 'pink';
                        }
                    }
                ")
            })
    }
    
    

    【讨论】:

      猜你喜欢
      • 2014-07-06
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 2014-09-17
      相关资源
      最近更新 更多