首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在shinyTree包中使用拖放时复制指定项而不是移动?

如何在shinyTree包中使用拖放时复制指定项而不是移动?
EN

Stack Overflow用户
提问于 2022-06-07 10:05:21
回答 1查看 47关注 0票数 0

我正在试用shinyTree包,看看它是否适合我的层次结构树需要,每个post How to build a drag and drop hierarchical tree with user inputs using shinyTree, jsTreeR, or similar package?

在下面的可复制代码中,目的是让用户将项目从“菜单”部分复制到“拖动此处”部分。相反,下面的代码移动拖动的项目,从“菜单”部分删除它们。我如何改变这种情况,使拖动的元素被复制而不是从“菜单”中丢失?(类似于可排序包中的“克隆”,请参见post How to replenish the bucket list when running the sortable package?)。

另外(也许这最好留给后续的帖子?),我想“修复”菜单部分,这样用户就不能对它做任何更改:不能重新排序,不能删除,不能添加。用户应该只能复制这些项目。

底部的图像说明了这个问题。

可复制代码:

代码语言:javascript
复制
library(shiny)
library(shinyTree)

ui <- fluidPage(
  pageWithSidebar(
    headerPanel("shinyTree!"),
    sidebarPanel(helpText(HTML("Created using <a href = \"http://github.com/trestletech/shinyTree\">shinyTree</a>."))),
    mainPanel(shinyTree("tree", 
                        stripes = TRUE, 
                        multiple = TRUE, 
                        animation = FALSE, 
                        dragAndDrop = TRUE, 
                        contextmenu = TRUE
                        )
              )
  )
)

server <- function(input, output, session) {
  output$tree <- renderTree({
    list(
      'Menu' = list(A = "", B = "", C = "", D = ""),
      'Drag here:' = list("")
    )
  })
}

shinyApp(ui, server)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-07 11:03:18

这是使用jsTreeR的方法。我还阻止了一些移动,只有当目标是“拖动这里”节点时,才允许移动。

代码语言:javascript
复制
library(jsTreeR)

nodes <- list(
  list(
    text = "Menu",
    state = list(opened = TRUE),
    children = list(
      list(
        text = "A",
        type = "moveable",
        state = list(disabled = TRUE)
      ),
      list(
        text = "B",
        type = "moveable",
        state = list(disabled = TRUE)
      ),
      list(
        text = "C",
        type = "moveable",
        state = list(disabled = TRUE)
      ),
      list(
        text = "D",
        type = "moveable",
        state = list(disabled = TRUE)
      )
    )
  ),
  list(
    text = "Drag here:",
    type = "target",
    state = list(opened = TRUE)
  )
)

checkCallback <- JS(
  "function(operation, node, parent, position, more) {",
  "  if(operation === 'copy_node') {",
  "    if(parent.id === '#' || parent.type !== 'target') {",
  "      return false;", # prevent moving an item above or below the root
  "    }",               # and moving inside an item except a 'target' item
  "  }",
  "  return true;",      # allow everything else
  "}"
)

dnd <- list(
  always_copy = TRUE,
  is_draggable = JS(
    "function(node) {",
    "  return node[0].type === 'moveable';",
    "}"
  )
)

jstree(
  nodes, dragAndDrop = TRUE, dnd = dnd, checkCallback = checkCallback,
  types = list(moveable = list(), target = list())
)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72529351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档