问题描述
Angular 7 带来了强大的 DragDropModule
:https://material.angular.io/cdk/drag-drop/examples
Angular 7 brought the powerful DragDropModule
with it: https://material.angular.io/cdk/drag-drop/examples
该文档涉及在列表中重新排列项目或在多个列表之间传输项目.但是,它没有讨论表格.
The documentation deals with rearranging items within lists or transferring items between several lists. However, it doesn't talk about tables.
我想知道是否有一种舒适的方式可以使用 angular material 的拖放系统来重新排序 mat-table 或 cdk-table 中的行.
I was wondering whether there is a comfortable way of using angular material's drag-and-drop system for reordering rows in mat-table or cdk-table.
(您可以将 cdkDropList
添加到 mat-table
中,这使得该机制可以工作,但没有所有花哨的动画和默认的拖动占位符.)
(You can add cdkDropList
to mat-table
which makes the mechanism work but without all the fancy animations and default drag placeholders.)
是否存在通过拖放对表格行进行排序的易于实现的默认设置?
Does something like an easy-to-implement default for sorting table rows via drag-and-drop exist?
推荐答案
样式由 CSS 完成(查看示例页面上的 CSS 选项卡).我调整了它以与 mat-table 一起使用:
The styling is done by CSS (look at the CSS tab on the example page). I tweaked it to work with mat-table:
.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);
}
.cdk-drop-list-dragging .mat-row:not(.cdk-drag-placeholder) {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
我把它放在我的主 style.scss 文件中.
I placed this in my main styles.scss file.
对于想知道如何在 mat-table 上实现拖放功能的任何人,您需要:
For anyone wondering how to implement drag and drop on a mat-table, you need to:
- 将
cdkDropList
添加到mat-table
- 将
(cdkDropListDropped)="onListDrop($event)"
添加到mat-table
- 将
cdkDrag
添加到mat-row
- Add
cdkDropList
tomat-table
- Add
(cdkDropListDropped)="onListDrop($event)"
tomat-table
- Add
cdkDrag
tomat-row
onListDrop
看起来像:
onListDrop(event: CdkDragDrop<string[]>) {
// Swap the elements around
moveItemInArray(this.myArray, event.previousIndex, event.currentIndex);
}
moveItemInArray
是一个 Angular Material 函数.您可以导入它.
moveItemInArray
is an Angular Material function. You can import it.
这篇关于使用角材料的拖放重新排序垫表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!