多选显示项目和选定项目(p-多选)

问题描述 投票:0回答:1

我在显示当前选定的项目时遇到问题(始终返回空白) - 很可能我没有正确迭代对象。虽然不确定最好的方法。我想要的只是在多选中显示所有选项以及当前选定的项目(例如来自数据库)。

成分:

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(因此无法引用该名称)

angular typescript primeng
1个回答
0
投票

老实说,我试图通过多次重读文本来理解问题的本质...但是,不幸的是,我无法理解其含义 XD。

我将尝试根据 MultiSelect 的使用提出一个想法。

MultiSelect 的基本操作需要以下属性:

  • 输入()选项:任意[]
  • 值 - ngModel 或 formControl
  • 如果您使用对象作为选项:
    • optionLabel - 对象中标签键的键
    • optionValue - 对象中值键的键

在实践中,我使用所有对象作为值(不使用 optionValue)。


我会回到你的例子。

选项<= categories - Where do you fill in this variable? value <= selectedCategories - Where do you output this variable?

Mby这里一定是订单作为选项吗?

© www.soinside.com 2019 - 2024. All rights reserved.