Angular 2 – 拖放回调函数
|
Plunker:
https://plnkr.co/edit/uZ8mvu6a1LXAsBBY3Shd?p=preview
在顶部我有’小部件’:’文字’和’文字b’,你可以拖放. 在小部件下面,你已经有了两行内部小部件. >阻拦容器 >块行(蓝色背景) >块列(粉红色背景) >块(红色背景) 基本上就像一个Bootstrap网格. 1)当我将’text a’小部件拖到红色块上时,小部件被添加但我没有得到回调’onDragEnter’函数调用.我只得到console.log onDropSuccess. 当我将小部件拖到容器,行,列和块上时,我希望得到一个回调,以便我可以添加逻辑. 我尝试添加我的功能,但不起作用,无法弄清楚错误在哪里. 我希望有些人可以帮助解决这个问题. //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',event);
}
onDragEnter(widget: any,event: any) {
alert('entered ')
console.log('onDragEnter',event);
console.log('drag enter containers',this.containers)
}
chicken(event) {
console.log('onDragEnter chicken',event);
}
onDragSuccess(widget: any,event: any) {
console.log('onDragSuccess',event);
}
onDragOver(widget: any,event: any) {
console.log('onDragOver',event);
}
onDragEnd(widget: any,event);
}
onDragLeave(widget: any,event: any) {
console.log('onDragLeave',event);
}
onMouseDown(){
this.dragOperation = true;
console.log('mouse down');
}
onMouseUp(event: any){
console.log(event);
this.dragOperation = false;
}
解决方法
你需要做的是在你的html中为你的项添加两件事.
首先是这个 (dragover)="$event.preventDefault()" 它会做什么,这使得它允许你放入你已经在其他地方处理过的那个项目,但是通过这样做,这允许你注册下一个回叫. 接下来你需要添加的是这个 (dragenter)="onDragEnter('',$event)"
这实际上会调用onDragEnter函数,并将事件链接到它. 对于plunker,您可以在第38行将这两个添加到您的plunker并且它可以工作,这样您就不必尝试理解新的架构. 注意:我建议您不要在onDragEnter中使用警报(就像它当前一样),因为您会收到垃圾邮件. 我还建议查看https://www.w3schools.com/html/html5_draganddrop.asp,因为它提供了关于如何使用事件进行拖放的非常简单的解释. 如果您需要更多帮助,我会非常乐意提供帮助,请告诉我您的困惑在哪里. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
