【发布时间】:2019-07-04 12:53:54
【问题描述】:
下面的应用程序包含一个 actionButton Add box,单击时会创建一个框。每个框都有一个 AdminLTE data-widget = "remove" 属性,因此每个框都有一个删除按钮,该按钮位于框标题的框工具 div 中。
目前,单击框的移除按钮只会隐藏框,而不是将其从 DOM 中移除。我试过下面的jQuery函数:
$('[data-widget = \"remove\"]').click(function() {
function removeBox (obj) {
$(obj).closest('.newbox').remove();
})
..从我放置在删除按钮标签中的 onclick 处理程序调用(参见 server.R),但这似乎不起作用。
这是可重现的代码:
ui.R:
require(shinydashboard)
require(shinyjs)
# HEADER
header <- dashboardHeader()
# SIDEBAR
sidebar <- dashboardSidebar(
tags$head(
tags$script("
$('[data-widget = \"remove\"]').click(function() {
function removeBox (obj) {
$(obj).closest('.newbox').remove();
})
")
))
# BODY
body <- dashboardBody(shinyjs::useShinyjs(),
box(title = "Months", status = "primary", width = 12, solidHeader = T, background = "navy",
tags$div(id = 'add'),
tags$div(class = "pull-right",
actionButton("addbox", "Add box")
)
)
)
ui = dashboardPage(header, sidebar, body, shinyFeedback::useShinyFeedback())
服务器.R
server = function(input, output, session) {
observeEvent(input$addbox, {
id = paste0('box', input$addbox)
insertUI(
selector = '#add',
ui = tags$div(class = "newbox",
id = id,
box(width = 12,
solidHeader = T,
title = div(h4(class = "box-title", paste("Month", input$addbox),
div(class = "box-tools",
tags$button(class = "btn btn-box-tool",
onclick = "removeBox(this)",
`data-widget` = "remove",
shiny::icon("remove")
)
)
),
selectizeInput(id, "Month name:", choices = month.name)
)
) #end tags$div
)
)
}) #end observeEvent
} #end server
我哪里错了?我想我可能使用了不正确的选择器 ('[data-widget = \"remove\"]'),但我尝试了 .btn.btn-box-tool 无济于事。任何帮助将不胜感激。
【问题讨论】:
标签: jquery r shiny dom-events shinyjs