angular - 想要拖动和替换元素而不是附加每个元素
问题描述
我想拖放元素的副本,但是每当我们拖动新元素时,它都应该替换旧元素。
drop(event: CdkDragDrop<string[]>) {
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
} else {
let item:any = event.previousContainer.data[event.previousIndex];
let copy:any = JSON.parse(JSON.stringify(item));
let element:any = {};
for(let attr in copy){
if(attr == 'title') {
element[attr] = copy[attr] += ' copy';
} else{
element[attr] = copy[attr];
}
}
this.destination.splice(event.previousIndex,0,element);
}
}
解决方案
在你的 drop 函数中,只需替换
//this.destination.splice(event.previousIndex,0,element); //<--replace this
//by
this.destination=[element];
注意:如果您使用空的 cdkDragPlaceholder,您会感觉更好
在你的 originList
<div class="field-box" *ngFor="let item of origin" cdkDrag>{{item.title}}
<!--add this line -->
<div *cdkDragPlaceholder ></div>
</div>
推荐阅读
- laravel - Laravel 自定义密码邮箱
- sql - 查询内的多个替换
- python - 使用 Flask 从提交的 HTML 表单值重定向链接
- android - appcompat-v7:28.0.0-rc02 不适用于 design:28.0.0-rc01(应该是 rc02,但我们不能)
- android - 从 PHP 服务器获取 JSON 数据
- ignite - 阿帕奇点燃 | TCP 发现 SPI
- python - 使用 PYTHON 和 JSON 传递 API 页面
- ios - UITextView 中的 UITextField 函数不可用
- python - 从 Selenium 导入元素时,会写入 CSV 上的多行而不是一行
- kotlin - 如何在 Kotlin 中分配当前类实例