如何使用JSTREE添加次级顺序编号到将元素拖动到层次结构的元素中?
我正在尝试将次级顺序编号添加到拖动到层次结构树节点的元素中,该元素使用包Jstreer,如底部所示。以下是用于插图的可再现代码。此顺序编号(通过字母而非数字)表示特定元素被拖动到目标节点的次数。
关于如何做的任何想法?
请注意,在相关帖子中,在r Shiny中如何将其附加到每个列表元素使用可排序的JS?在列表中显示的顺序次数?,解决了相同的问题,但使用html/css与可排序的软件包一起使用html/css反而。
可重复的代码:
library(jsTreeR)
library(shiny)
nodes <- list(
list(
text = "Menu",
state = list(opened = TRUE),
children = list(
list(
text = "Mickey",
type = "moveable",
state = list(disabled = TRUE)
),
list(
text = "Mouse",
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 === '#' || 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
" }",
" if(operation === 'delete_node') {",
" Shiny.setInputValue('deletion', position + 1);",
" }",
" return true;", # allow everything else
"}"
)
dnd <- list(
always_copy = TRUE,
# inside_pos = "last",
is_draggable = JS(
"function(node) {",
" return node[0].type === 'moveable';",
"}"
)
)
mytree <- jstree(
nodes, dragAndDrop = TRUE, dnd = dnd, checkCallback = checkCallback,
types = list(moveable = list(), target = list())
)
script <- '
$(document).ready(function(){
$("#mytree").on("copy_node.jstree", function(e, data){
var instance = data.new_instance;
var node = data.node;
var id = node.id;
var index = $("#"+id).index() + 1;
var text = index + ". " + node.text;
Shiny.setInputValue("choice", text);
instance.rename_node(node, text);
});
});
'
library(shiny)
ui <- fluidPage(
tags$head(tags$script(HTML(script))),
fluidRow(
column(width = 4,jstreeOutput("mytree")),
column(width = 8,verbatimTextOutput("choices"))
)
)
server <- function(input, output, session){
output[["mytree"]] <- renderJstree(mytree)
Choices <- reactiveVal(data.frame(choice = character(0)))
observeEvent(input[["choice"]],{Choices(rbind(Choices(),data.frame(choice = input[["choice"]])))})
observeEvent(input[["deletion"]], {Choices(Choices()[-input[["deletion"]], , drop = FALSE])})
output[["choices"]] <- renderPrint({Choices()})
}
shinyApp(ui, server)
插图:
I am trying to add secondary sequential numbering to elements dragged in to a hierarchy tree node, using the package jsTreeR, as shown in the illustration at the bottom. Below is the reproducible code used for the illustration. This sequential numbering (via letters not numbers) represents the number of times a particular element was dragged into the target node.
Any ideas for how to do this?
Note that in a related post, In R Shiny how to append to each list element the sequential number of times it appears in the list using sortable js?, the same issue was addressed but using HTML/CSS with the sortable package instead.
Reproducible code:
library(jsTreeR)
library(shiny)
nodes <- list(
list(
text = "Menu",
state = list(opened = TRUE),
children = list(
list(
text = "Mickey",
type = "moveable",
state = list(disabled = TRUE)
),
list(
text = "Mouse",
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 === '#' || 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
" }",
" if(operation === 'delete_node') {",
" Shiny.setInputValue('deletion', position + 1);",
" }",
" return true;", # allow everything else
"}"
)
dnd <- list(
always_copy = TRUE,
# inside_pos = "last",
is_draggable = JS(
"function(node) {",
" return node[0].type === 'moveable';",
"}"
)
)
mytree <- jstree(
nodes, dragAndDrop = TRUE, dnd = dnd, checkCallback = checkCallback,
types = list(moveable = list(), target = list())
)
script <- '
$(document).ready(function(){
$("#mytree").on("copy_node.jstree", function(e, data){
var instance = data.new_instance;
var node = data.node;
var id = node.id;
var index = $("#"+id).index() + 1;
var text = index + ". " + node.text;
Shiny.setInputValue("choice", text);
instance.rename_node(node, text);
});
});
'
library(shiny)
ui <- fluidPage(
tags$head(tags$script(HTML(script))),
fluidRow(
column(width = 4,jstreeOutput("mytree")),
column(width = 8,verbatimTextOutput("choices"))
)
)
server <- function(input, output, session){
output[["mytree"]] <- renderJstree(mytree)
Choices <- reactiveVal(data.frame(choice = character(0)))
observeEvent(input[["choice"]],{Choices(rbind(Choices(),data.frame(choice = input[["choice"]])))})
observeEvent(input[["deletion"]], {Choices(Choices()[-input[["deletion"]], , drop = FALSE])})
output[["choices"]] <- renderPrint({Choices()})
}
shinyApp(ui, server)
Illustration:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)