如何为rhandsontable重复hot_cell?热细胞?

问题描述 投票:0回答:1

我打算在

hot_cell
应用程序中使用
rhandsontable
中的
shiny
来在复选框为
TRUE
时禁用单元格。

对于我的示例,我使用

mtcars
,最后一列中带有
TRUE
的任何行都应禁用该行的
cyl
列。使用
hot_cell
,我可以将各个行制作为
readOnly
。然而,它仅适用于单个细胞。

有没有像

hot_cells
这样的功能我没有看到?或者,我想到使用
renderer
来完成此操作,但我不确定如何做到这一点。我知道应该有一种方法,但我不确定如何翻译此 JavaScript 来匹配,这是一个 JavaScript 示例:https://jsfiddle.net/ddan/1rhrco48/ 我的代码会记录我的一些失败的尝试使用 JavaScript。

示例代码:

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  rHandsontableOutput("Test")
)

server <- function(input, output, session) {
  
  #reactiveValues - Will store the table after editings
  temp_RV <- reactiveValues("DF" = cbind(mtcars, data.frame("Check" = FALSE)))
  
  #When the table is edited, update the reactiveValue
  observeEvent(input$Test, {
    
    temp_RV$DF <- hot_to_r(input$Test)
    
  })
  
  
  output$Test <- renderRHandsontable({
    
    #Only render if reactiveValue exists
    if(!is.null(temp_RV$DF)) {
      
      rhandsontable(temp_RV$DF) %>%
        #This works
        hot_cell(row = 1, col = "cyl", readOnly = TRUE) %>%
        #Below only works for a single cell, but if there are multiple it won't work. I need a "hot_cells?"
        # hot_cell(which(temp_RV$DF$Check), "cyl", readOnly = TRUE)  %>%
        #I thought maybe it needed "cyl" multiple times, so I tried below. Did not work. 
        # Makes sense, help specifies this configures a "single cell".
        # hot_cell(which(temp_RV$DF$Check), rep("cyl", sum(temp_RV$DF$Check)), readOnly = TRUE)
        #Below is using a renderer. I've tried things like td.disabled = 'true'; td.style = 'disabled'; td.editable = 'false';
        #These don't work when tried, and I'm not seeing a td.style that works for this.
        #td = disabled; will disable essentially the whole table, so this doesn't quite work. 
        hot_col(col = "cyl", renderer = paste0("
         function (instance, td, row, col, prop, value, cellProperties) {
         Handsontable.renderers.NumericRenderer.apply(this, arguments);
         var col_value11 = instance.getData()[row][11]
         
         if (col_value11) {
            td.style.background = '#4c433c';
         }  else {
            td.style.background = '#93d7ff';
          }
         
         }
         "))
      
    }
    
  })
}

shinyApp(ui, server)
r shiny rhandsontable
1个回答
0
投票

renderer
内添加
cellProperties.readOnly = true;
cellProperties.readOnly = false;
:

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  rHandsontableOutput("Test")
)

server <- function(input, output, session) {
  
  #reactiveValues - Will store the table after editings
  temp_RV <- reactiveValues("DF" = cbind(mtcars, data.frame("Check" = FALSE)))
  
  #When the table is edited, update the reactiveValue
  observeEvent(input$Test, {
    temp_RV$DF <- hot_to_r(input$Test)
  })
  
  output$Test <- renderRHandsontable({
    
    #Only render if reactiveValue exists
    if(!is.null(temp_RV$DF)) {
      
      rhandsontable(temp_RV$DF) %>%
        hot_col(col = "cyl", renderer = paste0("
         function (instance, td, row, col, prop, value, cellProperties) {
           Handsontable.renderers.NumericRenderer.apply(this, arguments);
           var col_value11 = instance.getData()[row][11]
         
           if (col_value11) {
             td.style.background = '#4c433c';
             cellProperties.readOnly = true;
           } else {
             td.style.background = '#93d7ff';
             cellProperties.readOnly = false;
           }
         
         }
         "))
    }
  })
}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.