更改 bslib 卡中全屏按钮(“展开”)的文本

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

如何更改全屏中展开按钮(由

full_screen = TRUE
创建)和关闭按钮的文本(我基本上需要翻译成另一种语言)?

enter image description here enter image description here

MWE:

library(shiny)
library(bslib)

# Define UI
ui <- fluidPage(
  theme = bs_theme(bootswatch = "minty"),
  titlePanel("Table in a card()"),
    
    mainPanel(
      card(
        title = "Data Table",
        full_screen = TRUE,
        tableOutput("mytable")
      )
  )
)

# Define server logic
server <- function(input, output) {
  # Generate sample data
  data <- data.frame(
    Name = c("John", "Jane", "Alice", "Bob", "Charlie", "David"),
    Age = c(25, 30, 35, 40, 45, 50),
    Score = c(80, 75, 90, 85, 88, 92)
  )
  
  # Render table
  output$mytable <- renderTable({
    filtered_data <- data
  })
}

# Run application
shinyApp(ui = ui, server = server)
r shiny bslib
1个回答
0
投票

下面是使用

JS
的解决方案,我目前看不到直接使用
bslib
内置函数来执行此操作的可能性。但是,请注意,如果您可以选择操作
bslib
函数,那么您也可以尝试此操作,例如
'Expand'
被硬编码在 bslib:::full_screen_toggle().

enter image description here

library(shiny)
library(bslib)

# Define UI
ui <- page_fluid(
  tags$script(
    HTML("$(document).ready(function(){
            // The 'Expand' text can be directly manipulated
            $('.card.bslib-card > bslib-tooltip > div').text('Erweitern');

            // The 'Close' text needs a mutation observer on the card because it
            // is only accessible if the full-screen mode is active.
            // We replace the html if the card has data-full-screen = true.
            var observer = new MutationObserver(function(mutations) {
                $('.bslib-full-screen-exit').html($('.bslib-full-screen-exit').html().replace('Close', 'Schließen'))
            });
            observer.observe(document.getElementsByClassName('card bslib-card')[0], { 
              attributes: true, 
              attributeFilter: ['data-full-screen'] });
          });")
    ),
  theme = bs_theme(bootswatch = "minty"),
  titlePanel("Table in a card()"),
  
  mainPanel(
    card(
      title = "Data Table",
      full_screen = TRUE,
      tableOutput("mytable")
    )
  )
)

# Define server logic
server <- function(input, output) {
  # Generate sample data
  data <- data.frame(
    Name = c("John", "Jane", "Alice", "Bob", "Charlie", "David"),
    Age = c(25, 30, 35, 40, 45, 50),
    Score = c(80, 75, 90, 85, 88, 92)
  )
  
  # Render table
  output$mytable <- renderTable({
    filtered_data <- data
  })
}

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