我有一个闪亮的选择菜单,如下所示。
selectInput(
"mySelectMenu",
"",
c(1,2),
selected = NULL,
multiple = FALSE
)
在此元素上观察到操作,如下所示
observeEvent(input$mySelectMenu,{
currentIndividual<-as.numeric(input$mySelectMenu)
toggleFunction(currentIndividual)
},ignoreInit=TRUE)
在检查器中,我可以看到包含此下拉内容的div。它的HTML看起来像这样
<div class="selectize-dropdown-content">
<div data-value="1" data-selectable="" class="option selected">318_2007</div>
<div data-value="2" data-selectable="" class="option">320_2007</div>
<div data-value="3" data-selectable="" class="option">321_2007</div>
<div data-value="4" data-selectable="" class="option">344_2009</div>
<div data-value="5" data-selectable="" class="option">346_2009</div>
<div data-value="6" data-selectable="" class="option">355_2009</div>
</div>
它在视觉上看起来像这样
最后,我想在某些情况下更改其中某些菜单项的外观。一个特定的场景......我想让'data-value =“2”'的div有 BOLD RED 文本。我怎样才能在div中使用data-value ='2'专门添加/删除下面的CSS类(.menuItemInactive)?我已经在使用shiny.js,并乐意使用它或任何其他包来完成这项任务。
.menuItemInactive{
font-weight:bold;
color:red;
}
答案 0 :(得分:1)
您可以使用 shinyjs 包和runjs方法实现此目的。 在第二个observeEvent函数中,检查selectInput中选择的值,然后相应地更改该div的css属性。
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- fluidPage(
shinyjs::useShinyjs(),
tabItem(tabName = "comp",
fluidRow(
selectInput("mySelectMenu", "",c(1,2,3),selected = NULL, multiple = FALSE
)))
)
server <- function(input, output, session) {
observeEvent(input$mySelectMenu,{
currentIndividual<-as.numeric(input$mySelectMenu)
# toggleFunction(currentIndividual)
},ignoreInit=TRUE)
observeEvent(input$mySelectMenu, {
if (input$mySelectMenu == 1) {
runjs(paste0('$(".selectize-input").css({"color": "red", "font-weight": "bold"})'))
}
if (input$mySelectMenu == 2) {
runjs(paste0('$(".selectize-input").css({"color": "blue", "font-weight": "bold"})'))
}
if (input$mySelectMenu == 3) {
runjs(paste0('$(".selectize-input").css({"color": "green", "font-weight": "bold"})'))
}
})
}
shinyApp(ui, server)
如果选择了某个项目,则只应用css,如果打开下拉框,则所有项目仍为黑色。你想要它总是有所需的颜色,或者这个解决方案适合你吗?
答案 1 :(得分:1)
然后我认为@kluu和我以前的答案的组合可以解决这个问题。 但是像这样,项目的顺序决定了下拉模式中的颜色,而不是它们的数据值。
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- fluidPage(
shinyjs::useShinyjs(),
inlineCSS(".selectize-dropdown-content > .option:nth-child(1) {
font-weight:bold;
color:red;
}
.selectize-dropdown-content > .option:nth-child(2) {
font-weight:bold;
color:blue;
}
.selectize-dropdown-content > .option:nth-child(3) {
font-weight:bold;
color:green;
}
"
),
tabItem(tabName = "comp",
fluidRow(
selectInput("mySelectMenu", "",c(1,2,3),selected = NULL, multiple = FALSE
)))
)
server <- function(input, output, session) {
observeEvent(input$mySelectMenu,{
currentIndividual<-as.numeric(input$mySelectMenu)
# toggleFunction(currentIndividual)
},ignoreInit=TRUE)
observeEvent(input$mySelectMenu, {
if (input$mySelectMenu == 1) {
runjs(paste0('$(".selectize-input").css({"color": "red", "font-weight": "bold"})'))
}
if (input$mySelectMenu == 2) {
runjs(paste0('$(".selectize-input").css({"color": "blue", "font-weight": "bold"})'))
}
if (input$mySelectMenu == 3) {
runjs(paste0('$(".selectize-input").css({"color": "green", "font-weight": "bold"})'))
}
})
}
shinyApp(ui, server)
答案 2 :(得分:0)
如果颜色不依赖于它所持有的值,而是依赖于列表中的外观顺序,则可以使用nth-child
:
.selectize-dropdown-content > .option:nth-child(2) {
font-weight:bold;
color:red;
}