1

让dragula drop事件更新firestore中的索引似乎是不可能的,因此可以保存用户选择的位置。即使项目改变了数组中的位置

this.$note = this.afs.collection(`notes`, ref => ref.orderBy('priority'));
this.$notes = this.$note.snapshotChanges().map(actions => {
       return actions.map(a => {
         const data = a.payload.doc.data();
         const id = a.payload.doc.id;
         return { id, data };
       });
     });

this.$notes.subscribe((noteArray) => {
  console.log('run when change');
  this.noteArray = [];
  for (const n of noteArray) {
    // console.log('run');
    this.noteArray.push(n);
  }

  this.dragulaService.drop.subscribe(() => {
    this.noteArray.forEach((note, index) => {
      // console.log("note", note, index);
      this.afs.doc(`notes/${note.id}`).update({ priority: index });
    });
    console.log("noteArray", this.noteArray);

    this.$notes.subscribe((nArray) => {
      console.log('run when change');
      this.noteArray = [];
      for (const n of nArray) {
        // console.log('run');
        this.noteArray.push(n);
      }
      console.log("nArry", this.noteArray);
  });
4

0 回答 0