【问题标题】:R Shiny - Removing UI using jQuery and onclick event handlerR Shiny - 使用 jQuery 和 onclick 事件处理程序删除 UI
【发布时间】: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


    【解决方案1】:

    您使用按钮的onclick 属性添加onclick 事件处理程序。所以你不需要添加另一个。只需使用此脚本:

      tags$head(
        tags$script("
    function removeBox(obj) {
      $(obj).closest('.newbox').remove();
    }")
        )
      )
    

    就是这样。无需设置data-widget 属性。

    你想用的脚本很奇怪:

    $('[data-widget = \"remove\"]').click(function() {
      function removeBox (obj) { ......
    

    结构是

    $('[data-widget = \"remove\"]').click(function() {
     ***action to perform when the user clicks***
    })
    

    在您的脚本中,您将一个函数定义为“要执行的操作”,它不执行任何操作。

    【讨论】:

    • 成功了,非常感谢您的帮助。我是 jQuery 新手,按照 W3 教程尝试过 $('[data-widget=\"remove\"]').click(function() { $(this).closest('.newbox').remove(); }); 之类的东西,但不知道为什么它不起作用。
    • @user51462 似乎 shinydashboard 已经将 onclick-event 处理程序附加到具有data-widget="remove" 属性的元素。也许此事件处理程序会取消您定义的事件处理程序。否则你的“东西”应该可以工作。
    • 干杯。我没有意识到即使在移除框后,框中输入的值仍然保留在输入列表中。这是有问题的,因为我想在稍后阶段遍历这些框,并且只想包含在 UI 中仍然打开的框的输入。我可以在 removeBox 函数中添加 .children('input').val('null') 之类的内容,以从内存中擦除封闭框的输入值吗?
    • 或者,将在被关闭的框中输入的 ID 减 1 是否正确? function removeLearner(obj) { var closed = $(obj).closest('.lrnrbox'); closed.remove(); closed.nextAll('lrnrbox').getElementById('input').{some function to subtract 1 from the id of the selectizeInputs in boxes succeeding the one being closed} }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    相关资源
    最近更新 更多