angular - How to use cdkDrag together with mat-grid
问题描述
I would like to use Angular 8 Material Design cdkDrag together with mat-grid-list and I have made a small test project to try it out.
My code is fairly simple. app.component.html:
<style>
.drag-handle {
position: absolute;
top: 10px;
left: 10px;
color: #ccc;
cursor: move;
width: 24px;
height: 24px;
}
</style>
<div cdkScrollable cdkDropList (cdkDropListDropped)="drop($event)">
<mat-grid-list cols="4" rowHeight="3:4" style="width: 25%;">{{number}}
<div cdkDrag *ngFor="let number of numbers">
<mat-grid-tile style="border: 1px solid green;">
{{number}}
<div class="drag-handle" cdkDragHandle>
<svg width="24px" fill="currentColor" viewBox="0 0 24 24">
<path
d="M10 9h4V6h3l-5-5-5 5h3v3zm-1 1H6V7l-5 5 5 5v-3h3v-4zm14 2l-5-5v3h-3v4h3v3l5-5zm-9 3h-4v3H7l5 5 5-5h-3v-3z">
</path>
<path d="M0 0h24v24H0z" fill="none"></path>
</svg>
</div>
</mat-grid-tile>
</div>
</mat-grid-list>
</div>
app.component.ts:
import { Component } from '@angular/core';
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'drag-drop';
numbers: number[] = [];
constructor() {
for (let i = 0; i < 100; i++) {
this.numbers.push(i);
}
}
drop(event: CdkDragDrop<number[]>) {
moveItemInArray(this.numbers, event.previousIndex, event.currentIndex);
}
}
It works, but not the way I would like. I can drag each tile, but I can drop it anywhere:
What I would like is to keep the tiles nice and ordered in a grid like:
How can I keep the tiles in a grid after I drag and drop them?
解决方案
推荐阅读
- python - 如何使用 python 处理 1000 个请求的并行请求?
- c# - 统一解析xlsx文件
- z3 - 为什么引入这个存在量词会导致不终止?
- android - Android Studio 如何使 WebView 支持阿拉伯语,即 RTL 文本方向?
- amazon-web-services - AWS 100 TB 静态数据转换 S3
- java - 在带有 CDT 的 eclipse 插件中使用自己的 CodeFormatter
- database - 无法让我的 docker-compose 文件与根的基岩一起使用
- javascript - 获取任意给定数量的包含字符串的数组的交集元素的函数
- android - 将 mix-blend-mode 设置为“正常”以外的任何内容都会在 Android 9 上的 Chrome 中隐藏元素
- java - Crashlytic 不显示确切的代码行和错误