javascript - 如何使用 react.js 从列表拖放到树

标签 javascript reactjs drag-and-drop

我是 React 的新手,我正在尝试实现拖放屏幕。

我在网上找到了一个使用 jQuery 的例子 jsFiddle .我将代码复制到这里作为可运行的演示,因为我不能在没有代码示例的情况下发布指向 jsFiddle 的链接。

谁能给我指出正确的方向,告诉我如何在 ReactJS 中做类似的事情?

var exData = [{
  id: "loc1",
  parent: "#",
  text: "Location 1"
}, {
  id: "loc2",
  parent: "#",
  text: "Location 2"
}, {
  id: "italy-1",
  parent: "loc2",
  text: "Italy",
  icon: "fa fa-flag"
}, {
  id: "poland-1",
  parent: "loc2",
  text: "Poland",
  icon: "fa fa-flag"
}];

function makeTreeItem(el) {
  return $("<a>", {
    id: $(el).attr("id") + "_anchor",
    class: "jstree-anchor",
    href: "#"
  });
}

$(function() {
  $('#tree').jstree({
    core: {
      check_callback: true,
      data: exData
    },
    types: {
      root: {
        icon: "fa fa-globe-o"
      }
    },
    plugins: ["dnd", "types"]
  });
  $('#tagList li').draggable({
    cursor: 'move',
    helper: 'clone',
    start: function(e, ui) {
      var item = $("<div>", {
        id: "jstree-dnd",
        class: "jstree-default"
      });
      $("<i>", {
        class: "jstree-icon jstree-er"
      }).appendTo(item);
      item.append($(this).text());
      var idRoot = $(this).attr("id").slice(0, -2);
      var newId = idRoot + "-" + ($("#tree [id|='" + idRoot + "'][class*='jstree-node']").length + 1);
      return $.vakata.dnd.start(e, {
        jstree: true,
        obj: makeTreeItem(this),
        nodes: [{
          id: newId,
          text: $(this).text(),
          icon: "fa fa-flag-o"
        }]
      }, item);
    }
  });
});
#tagList ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

#tagList ul li {
  background: #fff;
  border: 1px solid #ccc;
  width: auto;
  display: inline-block;
  padding: .125em .2em;
  margin: 3px 2px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet"/>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>

<div class="ui-widget">
  <div class="ui-widget-header">
    Tags
  </div>
  <div id="tagList">
    <ul>
      <li data-tag="1" id="uk-1">United Kingdom</li>
      <li data-tag="2" id="france-1">France</li>
      <li data-tag="3" id="germany-1">Germany</li>
    </ul>
  </div>
</div>
<div class="ui-widget">
  <div class="ui-widget-header">
    Tree
  </div>
  <div id="tree">
  </div>
</div>

提前致谢。

最佳答案

您提供的示例是用 jQuery 编写的。与其尝试让 jQuery UI 的 Draggable 在 React 中工作,不如使用 React DnD。这是一个从根本上做完全相同事情的示例。

http://react-dnd.github.io/react-dnd/examples/nesting/drop-targets

关于javascript - 如何使用 react.js 从列表拖放到树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493872/

相关文章:

jquery - 将节点从 jstree 拖到外部 div 元素上,然后在放下时执行操作

c# - 我如何支持在 Winforms 中拖放文件夹?

javascript - Firebase 不部署。错误的自签名证书

javascript - Jquery 1.11.1 - 下载文件和附加回调

javascript - React,本地缓存变量并在渲染时触发 onclick

css - React Bootstrap,为 NavItems 添加悬停效果

node.js - 添加 webpack 时 react 项目依赖错误

ios - 在 Xcode 中复制文件夹内容

javascript - 动态添加下拉菜单并将数据存储在json中

javascript - 第一次需要 javascript 帮助