jquery - 在 Shiny 的 selectInput 中自定义下拉宽度

标签 jquery html r shiny

下面的代码,采用自this question ,防止下拉菜单换行文本,并设置所有下拉菜单的宽度。

有没有办法自定义每个selectInput下拉列表的宽度?

library(shiny)

ui <- (fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userInput","Select User", c(1,2,3),
                  selected=1),
      selectInput("LongInput", "Long Strings", c("This is a long long string that is long.",
                                                 "This is a long long string that is longer."))
    ),

    # allows for long texts to not be wrapped, and sets width of drop-down
    tags$head(
      tags$style(HTML('
                      .selectize-input {
                      white-space: nowrap;
                      }
                      .selectize-dropdown {
                      width: 660px !important;
                      }'
              )
            )
          )
        )
      ))

server <- function(input, output, session) {}

shinyApp(ui, server)

最佳答案

如果我理解你的权利,你需要类似的东西

   library(shiny)

ui <- (fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userInput","Select User", c(1,2,3),
                  selected=1),
      selectInput("LongInput", "Long Strings", c("This is a long long string that is long.",
                                                 "This is a long long string that is longer."))
    ),
    
    # allows for long texts to not be wrapped, and sets width of drop-down
    tags$head(
      tags$style(HTML('
                      .selectize-input {
                      white-space: nowrap;
                      }
                      #LongInput + div>.selectize-dropdown{
                      width: 660px !important;
                      }
                      #userInput + div>.selectize-dropdown{
                                            width: 300px !important;
                      }
                      '
              )
      )
      )
      )
      ))

server <- function(input, output, session) {}

shinyApp(ui, server)

LongInput 设置为 660px,userInput 设置为 300px

更新

你也可以这样做 例如,您有 df 输入名称和大小

df1=data.frame(name=c("LongInput","userInput"),px=c(600,300))

所以尝试一下

library(shiny)

ui <- (fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userInput","Select User", c(1,2,3),
                  selected=1),
      selectInput("LongInput", "Long Strings", c("This is a long long string that is long.",
                                                 "This is a long long string that is longer."))
    ),
    
    uiOutput("din_css")
    
      )
      ))

server <- function(input, output, session) {
  df1=data.frame(name=c("LongInput","userInput"),px=c(600,300))
  
output$din_css=renderUI({
    tags$head(
      tags$style(HTML(paste0('
                      .selectize-input {
                      white-space: nowrap;
                      }',
                      paste(apply(df1,1,function(i){
                           paste0("#",i[["name"]],"+ div>.selectize-dropdown{
                            width: ",i[["px"]],"px !important;
                           }")
                      })
                      ,collapse='/n')      )
      )
      )
    )
  })
}

shinyApp(ui, server)

关于jquery - 在 Shiny 的 selectInput 中自定义下拉宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37298498/

相关文章:

javascript - jsTree选择独立于层次结构的复选框

javascript - 如何使用javascript设置div标签的高度和宽度?

javascript - 如何将数据表中动态添加的行包含到 DOM 树中,以便它的行为与页面加载时加载的任何其他行一样?

javascript - jquery-ui datepicker 更改 z-index

javascript - 使用 Javascript/jQuery 将 HTML 拆分为多列

javascript - jQuery:Ajax - 如何找到最后一个调用

php - 使用 PHP 将目标 ="_blank"添加到 HTML 的最佳方法

r - 指示特定值第一次出现的新变量

r - 使用 2 个数据框进行计算

r - 如何使用 facet_grid 或 facet_wrap 保持条的厚度均匀并切换 strip 位置?