typescript 不从原始容器中取出的成Angular 拖放

mwg9r5ms  于 2022-12-27  发布在  TypeScript
关注(0)|答案(1)|浏览(129)

我正在把一个元素从一个容器复制到另一个容器中。当复制它的时候,临时的从原始的容器中被移除,并且被复制。有没有办法不临时的从原始的容器中移除,并且仍然被复制?
我试着做了一些研究,发现它可以用cdkDragStarted和CdkDragStopped完成,但我无法正确地实现它!下面是我想要实现的演示代码,以进一步在我的主项目中使用它

//HtMl

<div class="container">
  <h2>Movies</h2>
  <div 
  cdkDropList 
  #moviesList="cdkDropList" 
  [cdkDropListData]="MoviesList"
  [cdkDropListConnectedTo]="[doneMovieList]" 
  class="movie-list" 
  (cdkDropListDropped)="onDrop($event)">
  <div class="movie-block" *ngFor="let moviesList of MoviesList" cdkDrag>{{moviesList}}</div>
</div>
</div>
<div class="container">
  <h2>Movies Watched</h2>
  <div 
  cdkDropList 
  #doneMovieList="cdkDropList" 
  [cdkDropListData]="MoviesWatched"
  [cdkDropListConnectedTo]="[moviesList]" 
  class="movie-list" 
  (cdkDropListDropped)="onDrop($event)">
  <div class="movie-block" *ngFor="let moviesWatched of MoviesWatched" cdkDrag>{{moviesWatched}}</div>
</div>
</div>

//CSS

.wrapper {
    margin: 25px auto;
    max-width: 600px;
    text-align: center;
    padding: 0 20px;
  }
  .container {
    width: 45%;
    margin: 0 25px 25px 0;
    display: inline-block;
    vertical-align: top;
  }
  .movie-list {
    width: 80%;
    border: solid 1px #ccc;
    min-height: 60px;
    display: inline-block;
    background: white;
    border-radius: 4px;
    overflow: hidden;
    margin-top: 25px;
  }
  .movie-block {
    padding: 20px 10px;
    border-bottom: solid 1px #ccc;
    color: rgba(0, 0, 0, 0.87);
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    cursor: move;
    background: white;
    font-size: 14px;
  }
  .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);
  }
  .movie-block:last-child {
    border: none;
  }
  .movie-list.cdk-drop-list-dragging .movie-block:not(.cdk-drag-placeholder) {
    transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
  }

.wrapper {
    margin: 25px auto;
    max-width: 600px;
    text-align: center;
    padding: 0 20px;
  }
  .container {
    width: 45%;
    margin: 0 25px 25px 0;
    display: inline-block;
    vertical-align: top;
  }
  .movie-list {
    width: 80%;
    border: solid 1px #ccc;
    min-height: 60px;
    display: inline-block;
    background: white;
    border-radius: 4px;
    overflow: hidden;
    margin-top: 25px;
  }
  .movie-block {
    padding: 20px 10px;
    border-bottom: solid 1px #ccc;
    color: rgba(0, 0, 0, 0.87);
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    cursor: move;
    background: white;
    font-size: 14px;
  }
  .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);
  }
  .movie-block:last-child {
    border: none;
  }
  .movie-list.cdk-drop-list-dragging .movie-block:not(.cdk-drag-placeholder) {
    transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
  }

//TS

import { Component } from '@angular/core';
import { CdkDragDrop, CdkDragStart, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  // Transfer Items Between Lists
  MoviesList:any = [
    'The Far Side of the World',
    'Morituri',
    'Napoleon Dynamite',
    'Pulp Fiction',
    'Blade Runner',
    'Cool Hand Luke',
    'Heat',
    'Juice'
  ];
  MoviesWatched:any = [
  ];
  onDrop(event: CdkDragDrop<any[]>) {
    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);
    }
  }  



}
yacmzcpb

yacmzcpb1#

很简单。用copyArrayItem()方法代替transferArrayItem()方法。它也能工作。

使用此选项:

...
copyArrayItem(
  event.previousContainer.data,
  event.container.data,
  event.previousIndex,
  event.currentIndex
);
...

相关问题