kendo-ui - Kendo UI TreeView : how to avoid re-dropping the node to its parent?

标签 kendo-ui kendo-treeview

以下代码片段禁止在特定条件下删除节点。禁用节点被丢弃到它自己的父节点的条件是什么? (即节点没有移动)。默认情况下,TreeView 允许这种行为。

<div id="treeview"></div>
<script>
$("#treeview").kendoTreeView({
   dragAndDrop: true,
   dataSource: [
       { text: "foo", items: [
          { text: "bar" },
          { text: "baz" }
        ] }
   ]
});

var treeview = $("#treeview").data("kendoTreeView");

treeview.bind("drag", function(e) {

   if (condition1 == false) {
     // deny the drop
     e.setStatusClass("k-denied");
   }

});
</script>

最佳答案

如果您只需要防止将其拖到其自己的父级上,则可以实现条件为:

// Get reference to target item
var tgt = this.dataItem(e.dropTarget);
// Get reference to parent item of the one being dragged
var parent = this.dataItem(this.parent(e.sourceNode));
// If they have the same uid then they are the same and condition should be false
    var condition1 = (parent.uid === tgt.uid);
    if (condition1 == true) {
      // deny the drop
      e.setStatusClass("k-denied");
    }

在这里查看它的实际效果:

$(document).ready(function() {
  $("#treeview").kendoTreeView({
    dragAndDrop: true,
    dataSource: [
      { text: "foo", items: [
        { text: "bar" },
        { text: "baz" }
      ] }
    ]
  });

  var treeview = $("#treeview").data("kendoTreeView");

  treeview.bind("drag", function(e) {
    var tgt = this.dataItem(e.dropTarget);
    var parent = this.dataItem(this.parent(e.sourceNode));
    console.log(parent.uid, tgt.uid, parent.uid === tgt.uid);
    var condition1 = (parent.uid !== tgt.uid);
    if (condition1 == false) {
      // deny the drop
      e.setStatusClass("k-denied");
    }

  });
});
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1411/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1411/styles/kendo.default.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1411/js/kendo.all.min.js"></script>

<div id="treeview"></div>

关于kendo-ui - Kendo UI TreeView : how to avoid re-dropping the node to its parent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28790781/

相关文章:

javascript - 在不调用 read() 的情况下设置剑道数据源

kendo-ui - 编辑 Kendo UI 网格后如何刷新网格?

javascript - 在每个父项 A-Z 中对 Kendo UI Jquery Treeview 子项进行排序

javascript - Kendo Treeview 使用 json 填充

kendo-ui - kendo Treeview 数据源绑定(bind)

javascript - 如何在 knockout 剑道多重选择中设置占位符

angularjs - 需要两个属性之一的 Angular 指令

jquery - Kendo Treeview checkChildren 属性在未选中子项时不要取消选中父级

asp.net-mvc - MVC Grid 的 Kendo UI 如何隐藏 ID 列