简体   繁体   English

尝试清除Angular 8 CDK拖放列表

[英]Trying to clear Angular 8 CDK Drag and Drop List

I'm making a simple drag and drop list with Angular and can't figure out how to reset the tables to their starting arrangements. 我正在使用Angular创建一个简单的拖放列表,但无法弄清楚如何将表重置为其开始安排。 I was trying to use transferArrayItem but I don't know how to reference the table to get access to the items in the list. 我试图使用transferArrayItem,但是我不知道如何引用该表来访问列表中的项目。

<div class="container">
<h2>Groceries</h2>

<div cdkDropList #todoList="cdkDropList" [cdkDropListData]="todo"
  [cdkDropListConnectedTo]="doneList" class="list" (cdkDropListDropped)="drop($event)">
<div class="list-item" *ngFor="let item of todo" cdkDrag>{{item}}</div>
</div>
</div>

<div class="container">
<h2>Cart</h2>

<div cdkDropList #doneList="cdkDropList" [cdkDropListData]="done"
 [cdkDropListConnectedTo]="todoList" class="list" (cdkDropListDropped)="drop($event)">
<div class="list-item" *ngFor="let item of done" cdkDrag>{{item}}</div>
</div>
</div>

<div id="buttons">
<button id="clearCart" (click)="clearCart()">Clear Cart</button>
<button id="checkout" (click)="checkout()">Checkout</button>
</div>

This is my "groceries.component.html" file where the lists are located and defined. 这是我的“ groceries.component.html”文件,在其中找到并定义了列表。 And below is my "groceries.component.ts" file where the functions are ran. 下面是运行这些函数的“ groceries.component.ts”文件。

import { Component } from '@angular/core';
import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';

@Component({
selector: 'app-groceries',
templateUrl: './groceries.component.html',
styleUrls: ['./groceries.component.css'],
})
export class GroceriesComponent {
todo = [
'Apple',
'Banana',
'Avocado',
'Cheez-Its',
'Doritos',
'Gushers',
'Orange',
'Big ol steaks'
];

done = [
];

drop(event: CdkDragDrop<string[]>) {
if (event.previousContainer === event.container) {
  moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
} else {
  transferArrayItem(event.previousContainer.data,
      event.container.data,
      event.previousIndex,
      event.currentIndex);
}
}

clearCart(){


window.alert("Cart has been cleared!");
}

exit(){

}

checkout(){
window.alert("Don't worry about paying, we'll ship them right now!");
}
}

I was going to use the exit() method to reset the elements back to original positions but still run into the problem of not knowing how to access the information of the items in the "done" array. 我本打算使用exit()方法将元素重置回原始位置,但仍然遇到不知道如何访问“完成”数组中各项信息的问题。

Also it's worth noting that I'm exploring into Angular for the first time, the "todo" and "done" lists were autogenerated and I plan on changing the names when I get the functionality working. 同样值得注意的是,我是第一次探索Angular,“ todo”和“ done”列表是自动生成的,我计划在功能正常工作时更改名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM