Dav*_*rem 7 drag-and-drop angular-material angular mat-table
Angular 7带来了强大的功能DragDropModule:https : //material.angular.io/cdk/drag-drop/examples
该文档涉及重新排列列表中的项目或在多个列表之间转移项目。但是,它没有谈论表。
我想知道是否有一种舒适的方法使用角材料的拖放系统对mat-table或cdk-table中的行进行重新排序。
(您可以添加cdkDropList使其mat-table使该机制起作用,但没有所有精美的动画和默认的拖动占位符。)
是否存在像易于实现的默认值那样通过拖放对表行进行排序的东西?
Lee*_*unn 12
样式由CSS完成(请查看示例页面上的CSS标签)。我对其进行了调整以使其与垫子桌子一起使用:
.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);
}
Run Code Online (Sandbox Code Playgroud)
我将其放在我的主要styles.scss文件中。
对于想知道如何在垫子桌子上实现拖放的任何人,您需要:
cdkDropList到mat-table(cdkDropListDropped)="onListDrop($event)"到mat-tablecdkDrag到mat-rowonListDrop 看起来像:
onListDrop(event: CdkDragDrop<string[]>) {
// Swap the elements around
moveItemInArray(this.myArray, event.previousIndex, event.currentIndex);
}
Run Code Online (Sandbox Code Playgroud)
moveItemInArray是Angular Material函数。您可以导入它。
小智 9
找到示例https://stackblitz.com/edit/angular-igmugp
看起来缺少的部分是
this.table.renderRows();
Run Code Online (Sandbox Code Playgroud)
小智 9
我在 stackblitz 上找到了一个很好的例子:https ://stackblitz.com/edit/table-drag-n-drop
为了不破坏拖放预览的用户界面,请使用标签
<mat-table>...</mat-table>
<mat-header-cell>...</mat-header-cell>
<mat-cell>...</mat-cell>
<mat-header-row>...</mat-header-row>
<mat-row>...</mat-row>
Run Code Online (Sandbox Code Playgroud)
代替
<table mat-table>...</table mat-table>
<th mat-header-cell>...</th mat-header-cell>
<td mat-cell>...</td mat-cell>
<tr mat-header-row>...</tr mat-header-row>
<tr mat-row>...</tr mat-row>
Run Code Online (Sandbox Code Playgroud)
结合/sf/answers/3754112001/ 中提到的 css,它对我来说是完美的解决方案。
小智 6
我MatTableDataSource用于我的数据源,所以我的解决方案是这样的:
导入DragDropModule中component.module.ts
CdkDragDrop在组件中导入
添加@ViewChild('table') table: MatTable<any>;到component.ts.
在 HTML 中添加:
<table mat-table #table [dataSource]="dataSource" class="mat-elevation-z8"
cdkDropList
[cdkDropListData]="dataSource"
(cdkDropListDropped)="drop($event)">
Run Code Online (Sandbox Code Playgroud)
在*matRowDef你需要添加这个:
<tr mat-row *matRowDef="let row; columns: displayedColumns;"
cdkDrag
[cdkDragData]=row>
</tr>
Run Code Online (Sandbox Code Playgroud)
然后在component.ts我做了 drop 事件:
drop(event: CdkDragDrop<Scene[]>) {
const previousIndex = this.dataSource.data.findIndex(row => row === event.item.data);
moveItemInArray(this.dataSource.data,previousIndex, event.currentIndex);
this.dataSource.data = this.dataSource.data.slice();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10237 次 |
| 最近记录: |