Angular 启动p树组件的停止节点丢弃事件

Angular 启动p树组件的停止节点丢弃事件,angular,tree,primeng,Angular,Tree,Primeng,我正在使用素数p-树组件,我想禁止将某些节点类型放入另一个节点类型中 例如,我有一个folder类型的节点和另一个file类型的节点,我只希望file类型的节点移动到folder类型的节点中。我想禁止在另一个文件夹节点内移动文件夹节点。(b)其他规则 <p-tree [value]="filesTree7" draggableNodes="true" droppableNodes="true" dragdropScope="files" selectionMode="single" [(s

我正在使用素数p-树组件,我想禁止将某些节点类型放入另一个节点类型中

例如,我有一个folder类型的节点和另一个file类型的节点,我只希望file类型的节点移动到folder类型的节点中。我想禁止在另一个文件夹节点内移动文件夹节点。(b)其他规则

<p-tree [value]="filesTree7" draggableNodes="true" droppableNodes="true" dragdropScope="files" selectionMode="single" [(selection)]="selectedFile2" [contextMenu]="cm" (contextmenu)="onContextMenu($event)"
          (onNodeDrop)="onNodeDrop($event)"></p-tree> 
但它不起作用

当我在这里查看启动代码时:似乎onNodeDrop事件太晚了


您知道如何实现我需要的功能吗?

如果您希望单个节点不可拖放或不可拖动,则必须在数组中设置参数draggable/dropable:

{
    "label": "Note e Commenti",
    "icon": "fa-file-word-o",
    "draggable":false,
    "droppable":false,
    "data": {
        "id": 1,
        "nome": "Note e Commenti",
        "testo": "Note e Commenti",
        "idCategoria": 2,
        "idTipologia": 1
    }
},
在实现之前,解决方法可能是覆盖
组件的
allowDrop
,并编写自己的drop逻辑实现。 但是,您可能(实际上应该)希望使用原始方法中包含的drop逻辑,只需添加位

现在,
原型设计
可能如下所示:

  • 在使用了
    pTree
    的组件中,通过
    ViewChild

    @ViewChild(Tree) tree: Tree;
    
  • 然后在
    构造函数
    ngOnInit
    或任何适合您的情况下,
    原型
    中的
    allowDrop

    Tree.prototype.allowDrop = (dragNode: any, dropNode: any, dragNodeScope: any): boolean => {
      return this._overrideAllowDrop(dragNode, dropNode, dragNodeScope)
    }
    
\u overrideallowdop
用于简洁。要取消删除,只需
返回false

此外,您还可以利用树实例的
dragDropService
来显示错误消息,以防删除失败

例如:

this.tree.dragDropService.dragStop$.subscribe((data) => {
  if(this.failed){ // set failed to true in your custom drop logic
    alert('Error!')
  }
})
但是这种解决方案的缺点是,删除逻辑应该在UI端,如果您的逻辑需要后端干扰,那么这种解决方案可能没有多大帮助


虽然我的案例涉及后端,但逻辑很简单,因此我将其移动到UI端,从而
原型设计
达到了目的。

错误的标记…阅读所有标记的描述并更正标记。我已经更正了一个,谢谢。我现在面临相同的问题。你解决了这个问题吗?不幸的是,我不能做我想做的事情因此我在树上寻找另一个有更多选项的组件,我发现dragula希望它能有所帮助
this.tree.dragDropService.dragStop$.subscribe((data) => {
  if(this.failed){ // set failed to true in your custom drop logic
    alert('Error!')
  }
})