提问人:Village.Idyot 提问时间:1/19/2023 更新时间:1/19/2023 访问量:49
如何在jsTreeR的菜单列表项上添加悬停文本弹出窗口?
How to add hover-over text pop ups over menu list items in jsTreeR?
问:
我一直是 Shiny 内部 jsTreeR 包的活跃用户。运行下面的骨架代码时,顶部会显示一个可拖动选项菜单,用户将这些项目向下拖动到标有“>> 拖到此处<<”的目标列表,以生成自定义的顺序标记列表。将鼠标悬停在这些菜单项上时,是否可以构建带有解释性文本的悬停弹出窗口,如下图所示?这怎么能做到呢?
一旦元素被拖动,就不需要相同的悬停弹出窗口。仅适用于位于顶部“菜单”部分中的元素。
法典:
library(jsTreeR)
library(shiny)
nodes <- list(
list(
text = "Menu",
state = list(opened = TRUE),
children = list(
list(text = "Dog",type = "moveable",state = list(disabled = TRUE)),
list(text = "Cat",type = "moveable",state = list(disabled = TRUE))
)
),
list(text = ">> Drag here <<",type = "target",state = list(opened = TRUE))
)
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,
types = list(moveable = list(), target = list())
)
script <- '
$(document).ready(function(){
var LETTERS = ["A", "B", "C"];
var Visited = {};
$("#mytree").on("copy_node.jstree", function(e, data){
var oldid = data.original.id;
var visited = Object.keys(Visited);
if(visited.indexOf(oldid) === -1){
Visited[oldid] = 0;
}else{
Visited[oldid]++;
}
var letter = LETTERS[Visited[oldid]];
var node = data.node;
var id = node.id;
var index = $("#"+id).index() + 1;
var text = index + ". " + node.text + " " + letter;
Shiny.setInputValue("choice", text);
var instance = data.new_instance;
instance.rename_node(node, text);
});
});
'
ui <- fluidPage(
tags$head(tags$script(HTML(script))),
fluidRow(jstreeOutput("mytree"))
)
server <- function(input, output, session){
output[["mytree"]] <- renderJstree(mytree)
}
shinyApp(ui, server)
答:
1赞
David Jorquera
1/19/2023
#1
I. 最简单、最直接的方法:
- 向节点添加属性。
title
children = list(
list(text = "Dog",type = "moveable",state = list(disabled = TRUE), a_attr = list(title = "A domesticated carnivorous mammal etc...")),
list(text = "Cat",type = "moveable",state = list(disabled = TRUE), a_attr = list(title = "A small domesticated carnivorous mammal etc..."))
)
II. 习惯的、有缺陷的方法:
- 添加到您感兴趣的孩子:
id
children = list(
list(text = "Dog",type = "moveable",state = list(disabled = TRUE), a_attr = list(id = "child_dog")),
list(text = "Cat",type = "moveable",state = list(disabled = TRUE), a_attr = list(id = "child_cat"))
)
- 用于定义 :
library(shinyBS)
tooltips
ui <- fluidPage(
tags$head(tags$script(HTML(script))),
bsTooltip("child_dog", "A domesticated carnivorous mammal etc...",
"right", options = list(container = "body"), trigger = "hover"),
bsTooltip("child_cat", "A small domesticated carnivorous mammal etc...",
"right", options = list(container = "body"), trigger = "hover"),
fluidRow(jstreeOutput("mytree"))
)
- 然而;折叠然后重新打开父文件夹会使工具提示无响应。我似乎找不到原因;也许这与 JavaScript 更改节点的选择器或 ID 有关。
评论
0赞
Village.Idyot
1/19/2023
很好。您最简单、直接的方法非常适合我的目的。您的第二种方法看起来很棒,无论如何,我可能会使“拖出”菜单不可折叠,这应该可以解决问题。
评论