StackBlitz:https ://stackblitz.com/edit/league-predictions
我有一个项目,我想预测足球联赛的排名。我有两个列表,一个包含预测,一个包含所有团队。
一开始预测列表是空的,所以你可以开始拖队。但是因为它是空的,所以第一队自动排名第一。当然,您可以稍后对其进行排序,但我想要的是基于团队数量的预定义插槽。这样,您可以将团队直接拖到正确的位置。
我真的无法在互联网上找到如何实现这一目标的解决方案。
这是我目前的情况,所以你可以看到我在说什么: 联赛预测
有人知道如何为 Angular CDK DragDrop 预定义插槽吗
这是我当前的代码。
<div class="container">
<div class="example-container">
<h5>Predictions</h5>
<div
cdkDropList
#predictionsList="cdkDropList"
[cdkDropListData]="predictions"
[cdkDropListConnectedTo]="[teamList]"
class="example-list"
(cdkDropListDropped)="drop($event)"
>
<div class="example-box" *ngFor="let prediction of predictions; let i = index" cdkDrag>
<app-team [team]="prediction" [index]="i"></app-team>
</div>
</div>
</div>
<div class="example-container">
<h5>Teams</h5>
<div
cdkDropList
#teamList="cdkDropList"
[cdkDropListData]="teams"
[cdkDropListConnectedTo]="[predictionsList]"
class="example-list"
(cdkDropListDropped)="drop($event)"
>
<div class="example-box" *ngFor="let team of teams;" cdkDrag>
<app-team [team]="team"></app-team>
</div>
</div>
</div>
</div>
不要介意一长串的团队,这都是来自数据库的数据
import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
predictions = [
];
teams = [
{
name: 'ADO Den Haag',
logo: 'Ado-Den-Haag-Logo.png'
},
{
name: 'Ajax',
logo: 'AFC-Ajax-Logo.png'
},
{
name: 'AZ',
logo: 'AZ-Alkmaar-Logo.png'
},
{
name: 'FC Emmen',
logo: 'FC-Emmen-Logo.png'
},
{
name: 'FC Groningen',
logo: 'FC-Groningen-Logo.png'
},
{
name: 'FC Twente',
logo: 'fc-twente-logo.png'
},
{
name: 'FC Utrecht',
logo: 'FC-Utrecht-Logo.png'
},
{
name: 'Feyenoord',
logo: 'Feyenoord-Rotterdam-Logo.png'
},
{
name: 'Fortuna Sittard',
logo: 'Fortuna-Sittard-Logo.png'
},
{
name: 'Heracles',
logo: 'Heracles-Almelo-Logo.png'
},
{
name: 'PEC Zwolle',
logo: 'PEC-Zwolle-Logo.png'
},
{
name: 'PSV',
logo: 'PSV-Eindhoven-Logo.png'
},
{
name: 'RKC Waalwijk',
logo: 'rkc-waalwijk.png'
},
{
name: 'SC Heerenveen',
logo: 'SC-Heerenveen-Logo.png'
},
{
name: 'Sparta Rotterdam',
logo: 'Sparta_Rotterdam_logo.png'
},
{
name: 'Vitesse',
logo: 'Vitesse-Arnhem-Logo.png'
},
{
name: 'VVV Venlo',
logo: 'VVV-Venlo-Logo.png'
},
{
name: 'Willem II',
logo: 'Willem-II-Logo.png'
},
];
drop(event: CdkDragDrop<string[]>) {
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
} else {
transferArrayItem(event.previousContainer.data,
event.container.data,
event.previousIndex,
event.currentIndex);
}
}
}