I Marçal Antwort unglaublich hilfreich, und haben sogar erweitert auf sie ein wenig, ein Update zu meinem DB schreiben mit der Sequenz Wert jedes zu aktualisieren Artikel (Kontakte in einer Organisation, in meinem Fall) in der Liste:
HTML (Container ist die Organisation des Kontakts.ein Kontakt, in meinem Fall, kann nicht zwischen Organisationen) bewegt werden:
<div class="container" [dragula]="'org-' + org.id" [dragulaModel]="org.contacts">
<nested-contact *ngFor="let contact of org.contacts" [user]="contact" class="contact" [attr.data-id]="contact.id"></nested-contact>
</div>
JS (in meinem Kontakt Service, eine PUT-Funktion zur Aktualisierung der gespeicherten Sequenzwerte, die mit jedem meiner Kontakten, so dass ihre Aufträge bestehen):
contactsIdPut (id, body) {
let url = '/api/v3/contacts/' + id + '?access_token=' + localStorage.getItem('access_token');
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.put(url, body, options)
.map((response: Response) => {
return response.json();
});
}
JS (in meiner Organisation view-Komponente, die Aktionen auf Drag & Drop gezogen werden zu umreißen, respectively):
export class nestedOrganizationComponent {
orgs = [];
thisOrg: any;
thisContact: any;
constructor (private _contactService: ContactService, private dragulaService: DragulaService) {
this._contactService = _contactService;
let dragIndex: any;
let dropIndex: any;
let elementNode: any;
dragulaService.drag.subscribe((value) => {
let id = Number(value[1].dataset.id);
let orgId: Number = value[0].split('-')[1];
elementNode = value[2].querySelectorAll('.contact:not(.ignore)').item(dragIndex);
if (!!id) {
// this renderedOrgs is just an array to hold the org options that render in this particular view
this._organizationService.renderedOrgs.push(this.org);
this.thisOrg = this._organizationService.renderedOrgs.filter(org => { return org.id == orgId; })[0];
this.thisContact = this.thisOrg.contacts.filter(contact => { return contact.id == id; })[0];
let arr = this.thisOrg.contacts.map(x => { return x.id; });
dragIndex = arr.indexOf(id);
}
});
dragulaService.drop.subscribe((value: any[]) => {
if (elementNode) {
let id = Number(elementNode.dataset.id);
if (!!id) {
let arr = this.thisOrg.contacts.map(x => { return x.id; });
dropIndex = arr.indexOf(id);
}
}
if (value[2] === value[3]) { // target container === source container
if (dragIndex >= 0 && dropIndex >= 0 && dragIndex !== dropIndex) {
this.thisOrg.contacts.forEach((contact, index) => {
contact.sequence = index;
this.updateSequence(contact.id, index);
});
}
}
});
}
updateSequence (id: Number, index: Number) {
let contactBody = {
avatar: {
sequence: index,
}
};
return this._contactService.contactsIdPut(id, contactBody)
.subscribe(
(data: any) => {
// nothing is needed, the same view can apply because the contact has already been moved.
},
(error: any) => {
console.error(error);
}
);
}
}
Hoffentlich gibt dies ein bisschen mehr Klarheit in Bezug auf die Angelegenheit an jemand anderen an einem Ort, der ähnlich ist, wo ich mich heute befand.
das gleiche Problem zu haben und kann nicht scheinen, eine Antwort zu finden. Ich verliere tatsächlich das DOM-Element, das ich komplett ziehe, sobald ich es fallen lasse. Das Entfernen des '[DragulaModel] = 'myList'' scheint dieses seltsame Verhalten zu beheben (immer noch Drag & Drop möglich - kann aber den Aufruf von dragulaService.dropModel nicht mehr auslösen). –
@DanJ, sehe ich nur ein Setup-Szenario, nichts über onDrop oder das Modell von onDrop zurückgegeben? –
Entschuldigung, muss ein Kopierproblem sein - im Kreis herumlaufen und versuchen, das Problem herauszufinden. –