Angular 2 - 拖放回调函数

4

Plunker: https://plnkr.co/edit/uZ8mvu6a1LXAsBBY3Shd?p=preview

在顶部,我有两个“小部件”:‘Text a’和‘Text b’,您可以拖放它们。

小部件下面有两行已经包含小部件的行。

  • 块容器
    • 块行(蓝色背景)
      • 块列(粉色背景)
        • 块(红色背景)

基本上就像Bootstrap网格一样。

1)当我将“text a”小部件拖动到红色块上时,该小部件被添加,但我没有回调“onDragEnter”函数。我只得到了onDropSuccess的console.log。

我希望在拖动小部件到容器、行、列和块上时能够得到回调,以便我可以添加逻辑。

我尝试添加我的函数,但不起作用,也无法找出错误所在。

希望有人能帮忙解决这个问题。

  //Drag functions

        drop(item){
            alert('dropped')
            console.log('dropping event', item)
                var target = item.mouseEvent.target,
                  index;

                if(target.classList.contains('row')) {
                    index = target.getAttribute('data-index');
                }

                if(target.classList.contains('item') && target.parentNode.classList.contains('row')) {
                    index = target.parentNode.getAttribute('data-index');
                }

                if(index) {
                    console.log(this.containers);
                    console.log(this.containers[index]);
                    this.containers[index].widgets.push( item.dragData);
                } else {
                    this.containers.push([ item.dragData]);
                }
         }
         onDropSuccess(widget: any, event: any, objecType: string) {
            console.log('dropped on ', objecType)
            if( objecType == 'row'){
                console.log('dropped on', objecType)
            }
            else if(objecType == 'block'){
                console.log('dropped on ', objecType)
            }
            this.dragOperation = false;
            console.log('onDropSuccess x', widget, event);

            console.log('containers', this.containers)
         }

         onDragStart(widget: any, event: any) {
            console.log('onDragStart', widget, event);
         }

         onDragEnter(widget: any, event: any) {
            alert('entered ')
            console.log('onDragEnter', widget, event);
            console.log('drag enter containers', this.containers)
         }

           chicken(event) {
            console.log('onDragEnter chicken', event);

         }

         onDragSuccess(widget: any, event: any) {
            console.log('onDragSuccess', widget, event);
         }

         onDragOver(widget: any, event: any) {
            console.log('onDragOver', widget, event);
         }

         onDragEnd(widget: any, event: any) {
            console.log('onDragOver', widget, event);
         }
         onDragLeave(widget: any, event: any) {
            console.log('onDragLeave', widget, event);
         }

         onMouseDown(){
            this.dragOperation = true;
            console.log('mouse down');
         }

         onMouseUp(event: any){
            console.log(event);
            this.dragOperation = false;
         }

1
你考虑过使用 RxJS 的强大功能来完成这个任务吗? - danimal
请解释得更详细一些,你的想法是什么,以及如何实现。因为我是一个新手。 - Chris Tarasovs
这是一个非常好的例子:https://egghead.io/lessons/javascript-simple-drag-and-drop-with-observables - danimal
@Danimal,我开始设置悬赏了,因为我无法理解它 ::( - Chris Tarasovs
我对这个库不是很了解,但我建议使用ng2-dragula。这个库有很好的回调和事件。https://github.com/valor-software/ng2-dragula。 - CharanRoot
3个回答

1
你需要做的是在你的HTML中添加两个内容。
第一个是这个:
(dragover)="$event.preventDefault()"

它的作用是让你能够进入已在其他地方处理过的项目,但通过在此处执行此操作,可以使你注册下一个回调。
接下来需要添加的是:
(dragenter)="onDragEnter('',$event)"

这将实际调用onDragEnter函数,并将事件链接到它。当然,您可以更改参数以匹配您的需求。

至于plunker,您可以将以下两个内容添加到第38行,这样您就不必尝试理解新的架构。

注意:我建议您不要在onDragEnter中使用警报(如目前所示),因为您将收到大量警报。

我还建议查看https://www.w3schools.com/html/html5_draganddrop.asp,因为它提供了非常易于使用的关于如何使用拖放事件的说明。

如果需要更多帮助,我很乐意提供帮助,只需让我知道您的困惑所在。


我已经按照你的建议做了,但不明白为什么我会收到两个警报。dragEnter只在页面上调用一次,但我看到当我拖动时它会再次调用。这是我的plunker链接:https://plnkr.co/edit/BeqfGyPQe4GKVatZNWYG?p=preview - Chris Tarasovs
看起来这与将元素添加到蓝色容器有关,因此您需要在原先的红色框(Lorem Ipsum)上进行拖放进入操作,然后它会创建新的红色框,并且还会在该框上触发ondragenter事件。 - Joo Beck

0

看起来你是 Angular 2 的新手。我建议使用标准库来进行拖放操作。我知道的两个好选择是 ng2-dragulang2-dnd。我发现 ng2-dnd 要好得多,已经在我的一个项目中从 ng-dragula 转移到了它。


嗨,Prabhat,我正在使用ng2-dnd :) - Chris Tarasovs
不,Prabhat,我正在尝试弄清楚这个库,这就是问题所在 :) - Chris Tarasovs

0

你可以把你的事件名称从onDragEnter改为dragEnter,我只是在新的Plunkr中fork了你的例子。[Plunkr链接][1]

 [1]: https://plnkr.co/edit/88aBdI1WOeJ7MvEvnAKm?p=preview

我已经测试了你的 Plunker,但 dragEnter 不起作用。如果我将元素拖到粉色区域,它不会运行 onDragEnter 函数,也就无法调用 Alert。 - Chris Tarasovs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接