我正在判断不同的选项,以允许用户构建一系列数学运算的层次 struct ,并正在try 不同的包,例如jsTreeRshinyTreeshinyDNDsortable和最后手段(放弃用户友好的视觉方法)rhandsontable.如相关帖子How to build a drag and drop hierarchical tree with user inputs using shinyTree, jsTreeR, or similar package?所述

使用下面的可复制代码,我试图为从"菜单" node 拖动(或复制)到"拖动到此处" node 的元素添加顺序编号.有什么办法吗?这篇文章最下面的图片更好地解释了这一点.

在使用sortableHTMLwidgets包来运行类似的东西(拖放)时,我运行了以下代码来成功地对拖动的元素进行编号,但不确定如何在jsTreeR中实现它:

tags$head(
    tags$style(HTML('
      #dragTo {list-style-type: none;  counter-reset: css-counter 0;}
      #dragTo > div {counter-increment: css-counter 1;}
      #dragTo > div:before {content: counter(css-counter) ". ";}
      ')
    )
  ),

其中,"dragTo"id是在一系列div()个HTML函数中设置的:

div(
      div(
        class = ...,
        div(class = "panel-heading", "Drag to here"),
        div(
          class = "...",
          id = "dragTo"
        )
      )
    )

Reproducible code for this question:

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 = "Rat", type = "moveable", state = list(disabled = TRUE)),
      list(text = "Bat", 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);", 
  "  var items = {",
  "    'delete' : {",
  "      'label'  : 'Delete',",
  "      'action' : function (obj) { tree.delete_node(node); },",
  "      'icon'   : 'glyphicon glyphicon-trash'",
  "     }",
  "  }",
  "  return items;",
  "}")

  
ui <- fluidPage(jstreeOutput("mytree"))  

server <- function(input, output){
  output[["mytree"]] <- renderJstree({
    jstree(
      nodes, 
      dragAndDrop = TRUE, 
      dnd = dnd, 
      checkCallback = checkCallback,
      types = list(moveable = list(), 
                   target = list()),
      contextMenu = list(items = customMenu),
    )
  })

}  

shinyApp(ui, server)

插图:

enter image description here

推荐答案

在UI中添加此脚本:

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 text = node.text;
    var index = $("#"+id).index() + 1;
    instance.rename_node(node, index + ". " + text);
  })
});
'

i、 e.在UI的开头显示tags$head(tags$script(HTML(script))).这是可行的,但你必须仔细瞄准最后一个位置;正如你所看到的,我在这里犯了一个错误:

enter image description here

Css相关问答推荐

Mat-Form-字段占用更多区域,导致其他控件远离它

如何仅使用CSS Select 器根据列表中第n个项的子项属性 Select 该项

如何为图像堆叠效果下方的图层实现与磨砂玻璃类似的视觉效果

为什么 width: 100% 在 Flex 父级中缩小这个 div ?

如何使用来自单独模块的代码将自定义标头插入到使用 DT Shiny 呈现的表中?

Angular (13) material - 覆盖 CSS 文件不起作用

从 Angular SCSS 生成 CSS?

你能用 JavaScript 控制 GIF 动画吗?

  • 元素上的子弹来自哪里?
  • 隐藏元素,但显示 CSS 生成的内容

    HTML/CSS 中的单选/复选框对齐

    使用 Bootstrap 设置身体的最大宽度

    Angular 动画的目的是什么?

    如何使用 Bootstrap2 使 div 居中?

    CSS 悬停与 JavaScript 鼠标悬停

    通过 Bootstrap4 对列进行排序

    bootstrap.css 和 bootstrap-theme.css 有什么区别?

    CSS停止图像下的文本换行

    为什么容器 div 坚持比 IMG 或 SVG 内容略大?

    如何将文本与图标字体垂直对齐?