使用js向selectizInput添加新项目

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

我正在尝试添加一个新选项来通过传单中的 easybutton 选择 selectizeInput。

我得到的是一个未定义的选项

这是一个例子:

library(shiny)
library(leaflet)

shinyApp(
ui <- fluidPage(
  selectizeInput("countries", "Country :", choices = c("Worldwide") ),
  leafletOutput("countryMap",height="500px")
),

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

output$countryMap <- renderLeaflet({
  leaflet() %>%
  addEasyButton(easyButton(icon = "fa-check", id = "doneMap", position = "topright", title = "Finish",
                               onClick = JS("function(btn, map){ 
                                              var $select = $('#countries').selectize();
                                              var selectize = $select[0].selectize;
                                              selectize.addOption({ value: 'selectedRegion', text: 'Selected Region' });
                                              selectize.addItem('selectedRegion');
                                            }
                                            "))) %>%
    addProviderTiles(providers$Esri.WorldStreetMap)
   
 })
})
r shiny r-leaflet selectize.js
1个回答
1
投票

唯一的问题ID是

data
中的
addOption(data)
是需要
value
label
键的对象,而不是
value
text

...,
onClick = JS("function(btn, map){ 
                  var $select = $('#countries').selectize();
                  var selectize = $select[0].selectize;
                  selectize.addOption({value: 'selectedRegion', label: 'Selected Region'});
                  selectize.addItem('selectedRegion');
              }")
© www.soinside.com 2019 - 2024. All rights reserved.