好的,在尝试了两种解决方案后,我已经弄清楚了。第一个涉及将占位符框添加到两个列表中,只有在它们有内容时才可见。它们的内容将是推入该列表的框的内容。同时,原盒被赋予了display: none
风格。这主要实现了我想要的行为,但由于可拖动的内部模型和 DOM 之间的不匹配,存在一些视觉问题。
最终最终奏效的是放弃首先拥有两个列表的概念。然后排序自然而然地解决了。但是,样式必须稍有不同,因为每个可拖动对象都必须是列表的直接后代。
附上代码和一个有效的 Stackblitz 示例:
app.component.ts
import { Component, OnInit} from '@angular/core';
import {CdkDragDrop, CdkDropList, moveItemInArray } from '@angular/cdk/drag-drop';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
public lists: {list1: string[], list2: string[]};
public fullList: string[];
public numList1: number;
constructor() {}
ngOnInit() {
this.lists = {
list1: ['one', 'two'],
list2: ['three', 'four']
};
this.fullList = this.lists.list1.concat(this.lists.list2);
this.numList1 = this.lists.list1.length;
}
public drop(event: CdkDragDrop<string[]>) {
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
}
}
app.component.html
<div class="list-container">
<div cdkDropList
[cdkDropListAutoScrollDisabled]="true"
[cdkDropListData]="fullList"
cdkDropListLockAxis="y"
(cdkDropListDropped)="drop($event)">
<ng-container *ngFor="let item of fullList; let index = index;">
<h2 *ngIf="index === 0">List 1</h2>
<h2 *ngIf="index === numList1">List 2</h2>
<div cdkDrag class="drop-box">{{item}}</div>
</ng-container>
</div>
</div>
Stackblitz:https ://stackblitz.com/edit/angular-ivy-s7zfye