Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programatically update table style #114

Closed
analytichealth opened this issue Apr 22, 2023 · 2 comments
Closed

Programatically update table style #114

analytichealth opened this issue Apr 22, 2023 · 2 comments

Comments

@analytichealth
Copy link

Hey, great package!
Is there a way to update the table style without re-rendering the table?
I have a large table and re-rendering the table for a change to css styles removes scroll position and takes time.

You can see what I tried below. But I am missing the javascript setStyle function.

Thank you

library(excelR)
library(shiny)
library(shinyjs)

shinyApp(
  
  ui = fluidPage(
    useShinyjs(),
    actionButton('rerender_to_style', 'Re-render style'),
    actionButton('update_style', 'Add style'),
    excelOutput("table")
    ),
  
  server = function(input, output, session) {
    
    output$table <- renderExcel(excelTable(data = head(iris)))
    
    # re-render to update style. this works but is not efficient, especially for large tables
    observeEvent(input$rerender_to_style, {
      style = list(A1='background-color:orange; color:green;')
      output$table <- renderExcel(excelTable(data = head(iris), style = style))
    })
    
    # add style with javascript. not working as missing the setSyle function
    observeEvent(input$update_style, {
      runjs("$('#table').jexcel('setSyle', [ { B1:'background-color:red' } ]);")
    })
    
  }
)
@shahreyar-abeer
Copy link

shahreyar-abeer commented May 14, 2023

This could be a solution.

library(excelR)
library(shiny)
library(shinyjs)

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    actionButton('update_style', 'Add style'),
    excelOutput("table")
  ),
  
  server = function(input, output, session) {
    
    output$table <- renderExcel(excelTable(data = head(iris, 20)))
    
    observeEvent(input$update_style, {
      runjs("
        t1 = document.getElementById('table').jexcel;
        t1.setStyle('B1', 'background-color', 'red');
      ")
    })
  }
)

@analytichealth
Copy link
Author

Works great, thanks @shahreyar-abeer 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants