如何实现Angular工程中列表项目在各个列表中的拖动?
安装程序库:
npm install --s @angular/cdk
引入模块:
import { DragDropModule } from '@angular/cdk/drag-drop';
imports: [
DragDropModule,
],
下面的例子是四个区域的拖动(模板):
<div class="container-board">
<div class="section-container">
<h2>Backlog</h2>
<button
class="add-task-button"
mat-stroked-button
color="blue"
(click)="addTask($event)"
>
<i class="fa fa-plus icon-left"></i>Add Task
</button>
<div
cdkDropList
#backlogList="cdkDropList"
[cdkDropListData]="backlog"
[cdkDropListConnectedTo]="[todoList, doneList, inProgressList]"
class="section-list"
(cdkDropListDropped)="drop($event)"
>
<div class="section-box" *ngFor="let item of backlog" cdkDrag>
{{ item }}
</div>
</div>
</div>
<div class="section-container">
<h2>To Do</h2>
<div
cdkDropList
#todoList="cdkDropList"
[cdkDropListData]="todo"
[cdkDropListConnectedTo]="[backlogList, doneList, inProgressList]"
class="section-list"
(cdkDropListDropped)="drop($event)"
>
<div class="section-box" *ngFor="let item of todo" cdkDrag>
{{ item }}
</div>
</div>
</div>
<div class="section-container">
<h2>In Progress</h2>
<div
cdkDropList
#inProgressList="cdkDropList"
[cdkDropListData]="inProgess"
[cdkDropListConnectedTo]="[backlogList, todoList, doneList]"
class="section-list"
(cdkDropListDropped)="drop($event)"
>
<div class="section-box" *ngFor="let item of inProgess" cdkDrag>
{{ item }}
</div>
</div>
</div>
<div class="section-container">
<h2>Done</h2>
<div
cdkDropList
#doneList="cdkDropList"
[cdkDropListData]="done"
[cdkDropListConnectedTo]="[backlogList, todoList, inProgressList]"
class="section-list"
(cdkDropListDropped)="drop($event)"
>
<div class="section-box" *ngFor="let item of done" cdkDrag>
{{ item }}
</div>
</div>
</div>
</div>
数据定义:
backlog = [];
todo = [
'Task 1',
'Task 2',
'Task 3'
];
inProgess = [
'Task 4',
'Task 5',
];
done = [
'Task 6',
'Task 7',
'Task 8',
'Task 9'
];
boards = [
{id: 1, name: "Board 1"},
{id: 2, name: "Board 2"},
{id: 3, name: "Board 3"},
];
样式定义:
.container-board {
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.section-container {
width: 400px;
max-width: 100%;
margin: 0 25px 25px 0;
display: inline-block;
vertical-align: top;
}
.section-list {
border: solid 1px #ccc;
min-height: 60px;
background: white;
border-radius: 4px;
overflow: hidden;
display: block;
}
.section-box {
padding: 20px 10px;
border-bottom: solid 1px #ccc;
color: rgba(0, 0, 0, 0.87);
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
box-sizing: border-box;
cursor: move;
background: white;
font-size: 14px;
}
.cdk-drag-preview {
box-sizing: border-box;
border-radius: 4px;
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
0 8px 10px 1px rgba(0, 0, 0, 0.14),
0 3px 14px 2px rgba(0, 0, 0, 0.12);
}
.cdk-drag-placeholder {
opacity: 0;
}
.cdk-drag-animating {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
.section-box:last-child {
border: none;
}
.section-list.cdk-drop-list-dragging .section-box:not(.cdk-drag-placeholder) {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
- 点赞
- 收藏
- 关注作者
评论(0)