rhandsontable change background of specific row

2019-06-23 20:52发布

I'm using rhandsontable in R shiny and I would like to change the color of the row with "Sum" in the first column. Since the number of rows is not fixed, selecting the "Sum" row based in its row number does not work. I have tried the attached code, which unfortunately does not work. Many thank for your help !

library(shiny)
library(rhandsontable)
runApp(list(server=
  shinyServer(function(input, output, session) {

     CF_amt <- as.data.frame(matrix(0.0, nrow=5, ncol=10));
     CF_type <- data.frame(source = c("Row1","Row2","Row3","Row4","Sum"),
                            stringsAsFactors = FALSE);
     CF_names <- c("source","C1","C2","C3","C4","C5","C6","C7","C8","C9","C10");
     CF_tbl <- cbind(CF_type,CF_amt);

     values <- reactiveValues(data = CF_tbl)

     output$table <- renderRHandsontable({ 
       rhandsontable(values$data,rowHeaders = NULL,colHeaders=CF_names) %>% 
         hot_cols(fixedColumnsLeft = 1, renderer = "
             function (instance, td, row, col, prop, value, cellProperties) {
               Handsontable.renderers.TextRenderer.apply(this, arguments);
               if (col == 0) {
                td.style.background = '#F0F0F0';
               }
               else if(this.instance.getData()[row][0] == 'Sum'){
                td.style.background = '#F00000';
               }
            }"
       ) 
    })
  })
,ui=
  shinyUI(navbarPage("Test",
    tabPanel("HOT",
       fluidPage(fluidRow(
         column(12,rHandsontableOutput("table"))
      ))
   )
  ))
))

1条回答
干净又极端
2楼-- · 2019-06-23 21:25

Looks like you have a javascript error. Try changing

this.instance.getData()[row][0]

to

instance.getData()[row][0]

because instance is passed as parameter to your render function.

enter image description here

查看更多
登录 后发表回答