我正在研究拖放功能(使用ng2-dragula)。
我需要从列表中拖动一个元素并将其放到另一个块中。但是我卡住的地方是我一次只需要在放置块中放置一个元素,如果我从放置块中删除第一个添加的元素,那么我可以添加另一个元素。有多个放置块。但列表块只是一个。
我正在研究拖放功能(使用ng2-dragula)。
我需要从列表中拖动一个元素并将其放到另一个块中。但是我卡住的地方是我一次只需要在放置块中放置一个元素,如果我从放置块中删除第一个添加的元素,那么我可以添加另一个元素。有多个放置块。但列表块只是一个。
我创建了一个 stackblitz 网址:我认为这就是您要寻找的内容。
你总是可以像这样在 ng2-dragula 中捕捉事件:
import { Subscription } from 'rxjs';
import { DragulaService } from 'ng2-dragula';
export class MyComponent {
// RxJS Subscription is an excellent API for managing many unsubscribe calls.
// See note below about unsubscribing.
subs = new Subscription();
constructor(private dragulaService: DragulaService) {
// These will get events limited to the VAMPIRES group.
this.subs.add(this.dragulaService.drag("VAMPIRES")
.subscribe(({ name, el, source }) => {
// ...
})
);
this.subs.add(this.dragulaService.drop("VAMPIRES")
.subscribe(({ name, el, target, source, sibling }) => {
// ...
})
);
// some events have lots of properties, just pick the ones you need
this.subs.add(this.dragulaService.dropModel("VAMPIRES")
// WHOA
// .subscribe(({ name, el, target, source, sibling, sourceModel, targetModel, item }) => {
.subscribe(({ sourceModel, targetModel, item }) => {
// ...
})
);
// You can also get all events, not limited to a particular group
this.subs.add(this.dragulaService.drop()
.subscribe(({ name, el, target, source, sibling }) => {
// ...
})
);
}
ngOnDestroy() {
// destroy all the subscriptions at once
this.subs.unsubscribe();
}
}
https://stackblitz.com/edit/ng2-dragula-base-jystom?file=src%2Fapp%2Fapp.component.ts
我使用了一个事件来操作这样的数据:
import { Component } from '@angular/core';
import { DragulaService } from 'ng2-dragula';
import { Subscription } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
// these are some basics to get you started -- modify as you see fit.
subs = new Subscription();
vamps = [
{ name: "Bad Vamp" },
{ name: "Petrovitch the Slain" },
{ name: "Bob of the Everglades" },
{ name: "The Optimistic Reaper" }
];
vamps2 = [
{ name: "Dracula" },
];
constructor(private dragulaService: DragulaService) {
// use these if you want
this.dragulaService.createGroup("VAMPIRES", {
// ...
});
this.dragulaService.dropModel("VAMPIRES").subscribe(args => {
console.log(args);
});
this.subs.add(this.dragulaService.drop("VAMPIRES")
.subscribe(({ name, el, target, source, sibling }) => {
this.vamps2=[{name: el.textContent}]
// ...
console.log( "@@@@@@@@@@@@@",name, el.textContent, target, source, sibling);
})
);
}
}