html - 使用 CDKdraganddrop 将容器的一个元素拖动到另一个元素时,是否可以保持原始容器的样式
问题描述
我希望能够使用 CDK 拖放将来自不同容器的多个元素添加到一个容器中。是否可以从本质上对不同的项目进行颜色编码。现在,当我拖动一个项目时,它会从它放入的容器中继承样式,我希望它保持其在原始容器中的颜色。
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
.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);
}
解决方案
我解释得像一本合上的书。这个想法是将类应用于我们想要拖动的“div”。为了得到它,我们可以为我们的对象添加一个新属性。想象一下你有一些喜欢
people:any[] = ['Alice','George','Peter','John'];
movies:any[] = ['Start Wars','The thing','It'];
我们可以做一些像
this.people=this.people.map(x=>({name:x,type:1}))
this.movies=this.movies.map(x=>({name:x,type:2}))
如果数组是对象数组,只需使用 forEach 添加新变量
this.people.forEach(x=>{x.type=1})
现在,我们可以使用两个类
.peopleClass
{
background:yellow!important;
}
.movieClass
{
background:red!important;
color:white!important;
}
并使用“类型”将此类添加到 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>
推荐阅读
- c++ - 快速将 2 个双精度数组交错为具有 2 个浮点数和 1 个 int(循环不变)成员的结构数组,使用 SIMD 双精度->浮点数转换?
- ubuntu - composer的安装方法有什么区别?
- reactjs - 如何在生产中部署 js?
- javascript - 使用 jQuery 将数据排序添加到现有数据表
- python - 正确转换图像以在 blob_log 中使用
- vim - 如何让 Vim 突出显示匹配的 <>?
- javascript - 创建在任务列表中应用过滤器的方法
- python - Python 中的 input() 函数可以动态检测输入的数据类型吗?
- office365 - 将文件从 SharePoint Online 发布到 Yammer
- python - 对字典进行排序但出现“列表索引必须是整数,而不是元组”错误