Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent duplicates on multidrag #2387

Open
MarianVlad74 opened this issue Jul 3, 2024 · 3 comments
Open

How to prevent duplicates on multidrag #2387

MarianVlad74 opened this issue Jul 3, 2024 · 3 comments

Comments

@MarianVlad74
Copy link

There is a possibility to prevent duplicates on multidrag?

@MarianVlad74
Copy link
Author

I solved by adding elements in an array in put group option and i check for duplictes on onAdd function.

It would be nice if the MultiDrag Plugin had an option like multiDragSkipDuplicates: true.

@BirgitPohl
Copy link

BirgitPohl commented Aug 20, 2024

Would you show how you solved it?
I have a similar issue in regards to unwanted duplicates with simple dragging.

  • other items are dragged with the items I am dragging
  • if not, the dragged item becomes duplicated on the target column.
async initMovability() {
    await this.updateComplete;
      const boardColumns = this.shadowRoot!.querySelectorAll('.board-column .board-items');
      boardColumns.forEach(column => {
        Sortable.create(column as HTMLElement, {
          group: 'board',
          animation: 300,
          onEnd: (evt) => {
            const itemEl = evt.item;
            const newState = itemEl.closest('.board-column')?.id as COLUMN_STATE;
            if (!itemEl.dataset.id) {
              throw new Error('"data-set-id" not found, but it\'s required for sorting.')
            }
            if (newState) {
              this._boardCardContext?.updateCardState(itemEl.dataset.id!, newState);
              this.requestUpdate();
            }
          }
        });
      });
  }

So, you say, you added an optional parameter to group and you check on onAdd if there are duplicates and you skip them.
My solution was to remove the duplicate from the newList in the onRemove().

async initMovability() {
    await this.updateComplete;
    
    const boardColumns = this.shadowRoot!.querySelectorAll('.board-column .board-items');
    
    boardColumns.forEach(column => {
      Sortable.create(column as HTMLElement, {
        group: {
          name: "board",
          pull: true,
          put: true
        },
        animation: 300,
        
        onAdd: (evt) => {
          const newList = evt.to as HTMLElement;
          const itemEl = evt.item;
          
          if (!itemEl.dataset.id) {
            throw new Error('Data-set-id not found, but it\'s required for sorting.');
          }
  
          const newState = newList.closest('.board-column')?.id as COLUMN_STATE;
          if (newState) {
            this._boardCardContext?.updateCardState(itemEl.dataset.id!, newState);
          }
        },
  
        onRemove: (evt) => {
          // Somehow this ensures that duplicates are removed after moving
          const newList = evt.to as HTMLElement;
          newList.removeChild(evt.item);
        },
      });
    });
  }

@MarianVlad74
Copy link
Author

So, you say, you added an optional parameter to group and you check on onAdd if there are duplicates and you skip them.
My solution was to remove the duplicate from the newList in the onRemove().

On the group/put option, i save elemens in to an array

group: {
    put: (to, from, dragEl, event) => {
         // save to arrayName
    },
},

and on onAdd function ...

onAdd: function (e) {
     e.items.forEach(e => {
           if (arrayName.includes(...)) {
               e.remove();
           }
     }
 },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants