简体   繁体   English

使用 CDKdraganddrop 将容器的一个元素拖动到另一个元素时,是否可以保留原始容器的样式

[英]Is it possible to keep the styling of the original container when dragging one element of a container to another using CDKdraganddrop

I want to be able to add multiple elements from different containers into one container using CDK drag and drop.我希望能够使用 CDK 拖放将来自不同容器的多个元素添加到一个容器中。 Is it possible to essentially colour code the different items.是否可以对不同的项目进行颜色编码。 Right now when I drag an item it inherits the styling from the container its dropped into and i want it to keep its colour that was in the original container.现在,当我拖动一个项目时,它继承了它放入的容器的样式,我希望它保持原始容器中的颜色。

HTML HTML

  <h2>Studio 6</h2>
        <div cdkDropList #donestudio6="cdkDropList" [cdkDropListData]="studio6"
          [cdkDropListConnectedTo]="[moviesList, toglist, ppolist]" class="movie-list" 
(cdkDropListDropped)="onDrop($event)">
          <div class="movie-block" *ngFor="let item of studio6" cdkDrag>{{item}}</div>
        </div>

   <div id="first" class="bottompanel">
        <h2>People</h2>
        <div cdkDropList #moviesList="cdkDropList" [cdkDropListData]="MoviesList"
          [cdkDropListConnectedTo]="[doneMovieList, donestudio2, donestudio3, donestudio4, 
donestudio5, donestudio6, donestudio7]" class="movie-list" (cdkDropListDropped)="onDrop($event)">
          <div class="peopleblock" *ngFor="let moviesList of MoviesList" cdkDrag>{{moviesList}}</div>
        </div>

CSS CSS

  .movie-block {
    padding: 10px 5px;
    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: 12px;
  }

  .peopleblock {
    padding: 10px 5px;
    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: 12px;
    background-color: rgb(53, 201, 206);
  }

I'm explain like a closed book.我像一本封闭的书一样解释。 The idea is that the class is applied to the "div" we want drag.这个想法是将类应用于我们想要拖动的“div”。 To get it, we can add a new property to our objects.为了得到它,我们可以为我们的对象添加一个新属性。 Imagine you has some like想象一下你有一些喜欢

  people:any[] = ['Alice','George','Peter','John'];
  movies:any[] = ['Start Wars','The thing','It'];

We can do some like我们可以做一些像

this.people=this.people.map(x=>({name:x,type:1}))
this.movies=this.movies.map(x=>({name:x,type:2}))

If the array is an array of object simply add the new variable using a forEach如果数组是一个对象数组,只需使用 forEach 添加新变量

this.people.forEach(x=>{x.type=1})

Now, we can use two class现在,我们可以使用两个类

.peopleClass
{
   background:yellow!important;
}
.movieClass
{
  background:red!important;
  color:white!important;
}

And use the "type" to add this class to the div并使用“类型”将此类添加到 div

<div class="example-container">
  <h2>Studio</h2>
  <div
    cdkDropList
    #studioList="cdkDropList"
    [cdkDropListData]="studio"
    class="example-list"
    (cdkDropListDropped)="drop($event)">
    <!--see how we use ngClass to add a class to the div-->
    <div class="example-box" 
         [ngClass]="{'peopleClass':item.type==1,'movieClass':item.type==2}"  
         *ngFor="let item of studio" cdkDrag>
      <span >{{item.name}}
        </span>
      </div>
  </div>
</div>

You can see an example in stackblitz你可以在stackblitz 中看到一个例子

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

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