Skip to content
Advertisement

How to use Angular CDK DragDrop with predefined slots?

StackBlitz: https://stackblitz.com/edit/league-predictions

I have a project where I want to predict football league standings. I have two lists, one where the prediction goes, and one with all the teams.

At the beginning the prediction list is empty, so you can start dragging teams in. But because it is empty, the first team is automatically ranked first. Of course you can sort them later, but what I want is predefined slots based on the number of team. This way you can drag the teams directly in the right place.

I can’t really find a solution on the internet on how to achieve this.

This is my current situation, so you can see what I am talking about: League Predictions

And this is what I want to achieve.enter image description here

Does someone know how to predefine slots for Angular CDK DragDrop

This is my current code.

JavaScript

Don’t mind the long list of team, this will all be data from database

JavaScript

Advertisement

Answer

made a stackblitz outlining what to do, first, create dummy entries in your predictions array:

JavaScript

this will fill out your slots. disable dragging on these items:

JavaScript

next you need to add a parameter to your drop function to know which container is being dropped on:

JavaScript

and template updates appropriately:

JavaScript

in your drop function, you use this parameter in the case of a list transfer, to either add or remove a placeholder:

JavaScript

maybe some tweaks / improvements could be made to the algorithm to determine which blank to remove in the case of dropping a team on a team or where to insert the blank on switch back to teams, but the simple version here worked well enough when i played with it.

working blitz: https://stackblitz.com/edit/league-predictions-ouu8nr?file=src%2Fapp%2Fprediction%2Fprediction.component.ts

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement