ChrisW
ChrisW

Reputation: 81

Change background of specific row

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.

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

Upvotes: 6

Views: 1923

Answers (1)

MrFlick
MrFlick

Reputation: 206232

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

Upvotes: 4

Related Questions