在运行下面的可再现代码时,用户将元素从层次结构树顶部的“菜单”节点中列出的固定选项拖到下面的“拖动此处”节点。我试图找出用户如何删除“拖动这里”节点中的元素,如下面的文章所示。目前,我将jstree选项设置为contextMenu = TRUE,但如下图所示,我不希望用户修改“菜单”节点中的元素;此外,在“拖动此处”节点中,用户应该只能添加、删除和重新排序元素(以及打开子元素--稍后再打开)。
如何将其更改为允许用户删除“菜单”中的元素?我的首选项只是将元素从网格中拖出,鼠标右键单击显示"Delete“,但目前没有显示Edit/Create/Edit (可能在后面添加”adding“和其他TBD内容),或者是一个拖入的垃圾桶。
可复制代码:
library(jsTreeR)
library(shiny)
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';",
"}"
)
)
ui <- fluidPage(jstreeOutput("jstree"))
server <- function(input, output){
output[["jstree"]] <- renderJstree({
jstree(
nodes,
dragAndDrop = TRUE,
dnd = dnd,
checkCallback = checkCallback,
contextMenu = TRUE, # << is there a better way so user can only delete items in the ´Drag here´ list?
types = list(moveable = list(), target = list())
)
})
}
shinyApp(ui, server)说明:

发布于 2022-06-07 19:05:12
您必须使用自定义上下文菜单。您没有查看这些示例,因为给出了这样一个示例;-)
我还在checkCallback中添加了一个条件,以防止“拖到这里”中的一些副本。
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) { console.log(node);",
" if(operation === 'copy_node') {",
" if(parent.id === '#' || node.parent !== 'j1_1' || 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';",
"}"
)
)
customMenu <- JS(
"function customMenu(node) {",
" var tree = $('#mytree').jstree(true);", # 'mytree' is the Shiny id or the elementId
" var items = {",
" 'delete' : {",
" 'label' : 'Delete',",
" 'action' : function (obj) { tree.delete_node(node); },",
" 'icon' : 'glyphicon glyphicon-trash'",
" }",
" }",
" return items;",
"}")
jstree(
nodes, dragAndDrop = TRUE, dnd = dnd, checkCallback = checkCallback,
types = list(moveable = list(), target = list()),
contextMenu = list(items = customMenu),
elementId = "mytree" # don't use elementId in Shiny! use the Shiny id
)https://stackoverflow.com/questions/72536127
复制相似问题