本文介绍了使用角材料的拖放重新排列垫表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

  1. cdkDropList 添加到 mat-table
  2. (cdkDropListDropped)="onListDrop($event)" 添加到 mat-table
  3. cdkDrag 添加到 mat-row
  1. Add cdkDropList to mat-table
  2. Add (cdkDropListDropped)="onListDrop($event)" to mat-table
  3. Add cdkDrag to mat-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.

这篇关于使用角材料的拖放重新排列垫表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 19:28