angular - Prime ng 表在第一次更改时未更新
问题描述
我有一个主要的商店表,我可以在其中删除商店并将其添加到列表中。
行为:当添加商店时,ChildComponent
会发出一个事件ParentComponent
,然后将商店添加到列表中并更新输入的可观察对象,ChildComponent
以便商店不再出现在表中。
问题:上述行为正常工作,除非表格被过滤,然后在添加商店时表格没有更新,即使我可以看到表格数组已在组件中正确更新。但是,当添加另一个商店(在同一个过滤表中)时,它工作正常,然后两个商店都从表中删除。
该表是纯组件(子)的一部分:
export class ChildComponent implements OnInit {
@Input() shops$: BehaviorSubject<Shop[]>
@Output() addSelectedShops = new EventEmitter<Shop[]>()
shops: Shop[]
selectedShops: Shop[] = []
constructor() {}
ngOnInit(): void {
this.shops$.subscribe((data) => {
this.shops = data
})
}
// this is called when adding a new shop
onAddSelectedShops(): void {
this.addSelectedShops.emit(this.selectedShops)
this.selectedShops = []
}
}
带有 update 方法的父组件:
export class ParentComponent implements OnInit {
shops$: BehaviorSubject<Shop[]>
localShops: Shop[]
list: Shop[]
constructor() {}
ngOnInit(): void {
// localShops is initiated here
}
// this is called via the event emitter
addShopToList(shop: Shop): void {
this.list.push(shop)
const shopIndex = this.localShops.findIndex(...)
if (shopIndex > -1) {
this.localShops.splice(shopIndex, 1)
}
this.shops$.next(this.localShops)
}
}
这是 childComponent 中的表:
<p-table #dt1 [value]="shops" [(selection)]="selectedShops" [globalFilterFields]="['resellerName']" dataKey="uniqueResellerName">
<ng-template pTemplate="caption">
<span>
<i class="candidate-shop-list__search-bar-icon pi pi-search"></i>
<input pInputText type="text" (input)="dt1.filterGlobal($any($event.target).value, 'contains')" placeholder="Search" />
</span>
</ng-template>
<ng-template pTemplate="header">
..... HEADERS ....
</ng-template>
<ng-template pTemplate="body" let-rowIndex="rowIndex" let-shop>
..... DATA ....
</ng-template>
</p-table>
请让我知道是否有任何想法为什么这不适用于第一次添加。谢谢你。
解决方案
我遵循了这个问题的答案,它对我有用,但我仍然不完全理解为什么它在第一次添加时不起作用,然后在之前的下一个添加中起作用。
推荐阅读
- javafx - javafx用颜色序列化包装器
- powershell - 使用 Powershell 脚本检查运行服务的计算机列表
- python - Pythonicaly 根据每行和所有列的条件从 numpy 矩阵中获取行的子集
- c++ - 如何用转义字符替换字符串中的所有特殊字符?
- arrays - IF条件检查第一个字母是否是某个字母并且Python中是否存在第二个字母
- python - 如果多个列与字典中的值匹配,则用值填充数据框列
- php - 如何执行 10 in 10 交互 PHP 的动作
- amazon-web-services - AWS NACL - 仅在传入和传出规则中允许 0.0.0.0/0 时才有效
- discord.py - Discord.py 如何根据用户反应执行操作
- macos - 如何为 Mojave 和 Catalina 上的 MacOS 帮助生成工作索引