【问题标题】:Fire JavaScript code once element is added添加元素后触发 JavaScript 代码
【发布时间】:2019-09-11 13:09:20
【问题描述】:

假设我想在shiny 中获取以下jstree(带有按钮的部分只是为了说明shinytree 从一开始就不存在):

$(function() {
  $('#create').on('click', function() {
    $('#mytree').jstree({
      'core' : {
        'data' : {
          "url" : "//www.jstree.com/fiddle/?lazy",
          "data" : function (node) {
            return { "id" : node.id };
          }
        }
      },
      contextmenu : {
        items : {
          'item1' : {
            'label' : 'item1',
            'action' : function () { /* action */ }
          }
        }
      },
      plugins : ["contextmenu"]
    });
  })
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.8/themes/default/style.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.8/jstree.min.js"></script>

<div id="mytree"></div>
<button id = "create">
   Create Tree
</button>

问题是我无法通过shinyTree 提供contextmenu。所以我需要回退到 JavaScript 来自己添加这个功能。在控制台上,我会这样做:

$('#mytree').jstree(true).settings.contextmenu = {
   items: {
      item1 : {
         'label' : 'item1',
         'action': function() { /* action */ }
      }
   }
}

但是我应该在何时何地在我的 ShinyApp 中调用它?我使用另一个处理程序的方法不起作用,因为我猜该处理程序在树呈现之前触发(然后按第二次按钮就可以了,至少显示 JS 代码按预期工作)。玩优先级也无济于事。

我可以使用一个我会附加到$(document) 的 javascript 事件处理程序来监听树的创建吗?

ShinyApp

library(shiny)
library(shinyTree)
library(shinyjs)
js_code <- "$('#mytree').jstree(true).settings.contextmenu = {
   items: {
      item1 : {
         'label' : 'item1',
         'action': function() { /* action */ }
      }
   }
};"
ui <- fluidPage(useShinyjs(),
                actionButton("create", "Create Tree"), 
                shinyTree("mytree", contextmenu = TRUE))

server <- function(input, output, session) {
   ## does not work as intended
   observeEvent(input$create, runjs(js_code), ignoreInit = TRUE, priority = -1)


   output$mytree <- renderTree({
     req(input$create)
     list("Root Node" = list("Child Node 1" = list(
                                                "Child Node 3" = "", 
                                                "Child Node 4" = ""),
                             "Child Node 2" = ""))
   }) 
}

shinyApp(ui, server)

【问题讨论】:

    标签: javascript r shiny jstree shinytree


    【解决方案1】:

    我找到了解决方案。基本上可以使用ready.jstree 或loaded.jstree 事件:

    library(shiny)
    library(shinyTree)
    library(shinyjs)
    js_code <- "$('.shiny-tree').on('ready.jstree', function() {
       $(this).jstree(true).settings.contextmenu = {
          items: {
             item1 : {
                'label' : 'item1',
                'action': function() { /* action */ }
             }
          }
       };
    })"
    ui <- fluidPage(useShinyjs(),
                    actionButton("create", "Create Tree"), 
                    shinyTree("mytree", contextmenu = TRUE))
    
    server <- function(input, output, session) {
       session$onFlushed(function() runjs(js_code))
    
       output$mytree <- renderTree({
         req(input$create)
         list("Root Node" = list("Child Node 1" = list(
                                                    "Child Node 3" = "", 
                                                    "Child Node 4" = ""),
                                 "Child Node 2" = ""))
       }) 
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多