我在显示当前选定的项目时遇到问题(始终返回空白) - 很可能我没有正确迭代对象。虽然不确定最好的方法。我想要的只是在多选中显示所有选项以及当前选定的项目(例如来自数据库)。
成分:
orders: any[] = [];
this.orderDetailService.getAllOrders().subscribe((orders) => {
this.orders = orders;
});
private fetchOrderDetailData() {
if (this.orderDetailId !== undefined) {
this.orderDetailService.getOrderDetailById(this.orderDetailId).subscribe((orderDetail) => {
console.log(orderDetail.DetailCategories);
this.orderDetail = orderDetail;
});
} else {
// Handle the case where this.order.OrderDetail?.Id is undefined
console.error('OrderDetail Id is undefined');
}
}
HTML:
<div>
<p-multiSelect [options]="categories" [(ngModel)]="selectedCategories" defaultLabel="Select a Category" optionLabel="Name"
display="chip"></p-multiSelect>
</div>
目前:
console.log(orderDetail.DetailCategories);是显示当前所选项目的项目,并且是一个对象数组。大批 [ {…}, {…} ]。 另外,Category.Name 是我作为 option.Label 放置的内容,但对于上面的数组,它被称为 CategoryName(因此无法引用该名称)
老实说,我试图通过多次重读文本来理解问题的本质...但是,不幸的是,我无法理解其含义 XD。
我将尝试根据 MultiSelect 的使用提出一个想法。
MultiSelect 的基本操作需要以下属性:
在实践中,我使用所有对象作为值(不使用 optionValue)。
我会回到你的例子。
选项<= categories - Where do you fill in this variable? value <= selectedCategories - Where do you output this variable?
Mby这里一定是订单作为选项吗?