R Shiny: Use navbarPage with bsModal by shinyBS

坚强是说给别人听的谎言 提交于 2019-12-13 01:33:57

问题


I'm trying to add a tabPanel in navbarPage so that when you click on it opens a modal window instead of a new tab. The snippet below is not valid because tabPanel does not have an id parameter.

library(shiny)
library(shinyBS)
shinyUI(fluidPage(
    navbarPage("Sample App", id = "main_menu", 
        tabPanel("Open Modal", id = "moda")),
    bsModal("modal1", "Example", "moda", p("This is a modal"))
)

If I edit the generated HTML code from browser, I can make this possible by changing the line

<a href="#tab-9741-5" data-toggle="tab" data-value="Open Modal">Open Modal</a>

with

<a href="#" data-toggle="modal" data-target="#modal1">Open Modal</a>

on the <li> element.

Any idea how to do this or at least how can I override the generated html from shiny?


回答1:


One solution is to use Javascript to rewrite the attribute for the tab title. The JS code below finds the tab title link, and rewrites its attributes.

library(shiny)

jsStr <- '$(document).ready(function(){
  $("a[data-value=\'OpenModal\']").attr({
    "href":"#", 
    "data-toggle":"modal",
    "data-target":"#modal1"
  });
})
' 

ui <- shinyUI(fluidPage(
  tags$head(tags$script(HTML(jsStr))),
  navbarPage("title",
    tabPanel("OpenModal")
  ),
  bsModal("modal1", "Example", "moda", p("This is a modal"))
))


来源:https://stackoverflow.com/questions/36530613/r-shiny-use-navbarpage-with-bsmodal-by-shinybs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!