angular - 如何根据屏幕另一个区域的某些条件以角度从循环中渲染单个项目?
问题描述
任何人都知道如何呈现这种场景:
我知道可拖动库提供了禁用拖动的选项。但是,当项目被禁用拖动但另一个项目可以在拖动时更改不可拖动项目的位置时,这并不是更好的解决方案。
那么有没有办法在不同的地方渲染项目某种黑客或东西或功能,通过这种方式可以完成这种场景?
请指导我在正确的道路上以角度呈现这种场景
解决方案
将支持数组拆分为两个可观察对象
由于我们关注一个源但需要在两个不同的位置输出项目的子集,我们将采用一种方法来保留一个内部 BehaviorSubject,我们可以从中派生多个 observables,以创建我们过滤的子集。
依赖关系
- Angular 材质组件源
ng add @angular/material
在您的角度项目根目录中添加角度材料运行。当提示添加动画时,选择是
修改你的模块:默认是 app.module
在您可以使用 CDK 拖放指令之前,您必须将DragDropModule添加到您的导入中。
@NgModule({
declarations: [
...
],
imports: [
...
DragDropModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
创建组件
控制器
@Component({
selector: 'drag-drop-demo',
templateUrl: './drag-drop-demo.component.html',
styleUrls: ['./drag-drop-demo.component.scss']
})
export class DragDropDemoComponent implements OnInit, OnChanges, OnDestroy {
// utilize behaviorSubject so we always get the latest input in our observables regardless of lifecycle timing.
private internal!: BehaviorSubject<number[]>;
@Input() listItems!: number[];
conditionAppliesList!: number[];
conditionDoesNotApplyList!: number[];
subscriptions: Subscription;
constructor() {
this.internal = new BehaviorSubject<number[]>([]);
this.subscriptions = new Subscription();
}
ngOnInit(): void {
// setup two distinct observables that apply some filter condition to our list.
this.subscriptions.add(
this.internal.asObservable()
.pipe(map(items => items.filter(number => number % 2 == 0))).subscribe(numbers => {
this.conditionAppliesList = numbers;
})
);
this.subscriptions.add(
this.internal.asObservable()
.pipe(map(items => items.filter(number => number % 2 != 0))).subscribe(numbers => {
this.conditionDoesNotApplyList = numbers;
})
);
}
ngOnChanges(changes: SimpleChanges): void {
// when listItems are detected in our input, emit the values in the internal BehaviorSubject
if ('listItems' in changes) {
// emit next value
this.internal.next(this.listItems);
}
}
ngOnDestroy() {
// cleanup the subscriptions.
this.subscriptions.unsubscribe();
}
// cdkHandler.
drop(event: CdkDragDrop<string[]>) {
moveItemInArray(this.conditionAppliesList, event.previousIndex, event.currentIndex);
}
}
模板
<h2>Drag and drop</h2>
<div class="list-outlet">
<h3>Draggable</h3>
<div class="list-container" cdkDropList (cdkDropListDropped)="drop($event)">
<div class="list-item" *ngFor="let num of conditionAppliesList" cdkDrag>
{{num}}
</div>
</div>
<h3>Non-Draggable</h3>
<div class="list-container">
<div class="list-item" *ngFor="let num of conditionDoesNotApplyList">
{{num}}
</div>
</div>
</div>
推荐阅读
- sql - 如何将 timescaleDB 查询限制为使用的存储桶大小?
- javascript - 如何从 docx 生成 pdf - NodeJS + TypeScript
- apache-spark - 使用 ALTER TABLE 将新列添加到 Databricks 上的 Array(Struct) 列中
- angular - 角度日期函数 - 我想将日期增加 7 并在 html 中显示
- amazon-web-services - 用于高度可扩展、安全且可用的 API 的反向代理服务器
- json - 浮点数格式的 JSON 列表
- java - 生成 JAXB 类 - XSD 文件中重复的 simpleType 元素的问题
- ssl - OpenSSL:openssl s_client 验证返回:1 但验证返回码:0(正常)
- keycloak - Keycloak丢失了管理员密码
- python - 链接无效的 Python-Telegram-Bot