简体   繁体   English

如何以反应形式修补多选下拉列表(primeng 组件)的值?

[英]How to patch the value of multiselect dropdown (primeng component) in a reactive form?

I am using primeng components in the template.我在模板中使用 primeng 组件。 I have the option to edit the form, which displays the existing field values.我可以选择编辑显示现有字段值的表单。 I am not able to patch the value of multiselect dropdown.我无法修补多选下拉列表的值。

excludeDays = [      
    {label: 'Mon', value: 1},
    {label: 'Tue', value: 2},
    {label: 'Wed', value: 3},
    {label: 'Thu', value: 4},
    {label: 'Fri', value: 5},
    {label: 'Sat', value: 6},
    {label: 'Sun', value: 7},
  ]

I have my backend data coming in as a string and then I am converting it into array of number like我的后端数据以字符串形式输入,然后将其转换为数字数组,例如

[1,2] or[2,3,4]

Now after I press the edit button I want the values of the dropdown to be selected as above excludeDays values are there.现在,在我按下编辑按钮后,我希望选择下拉列表的值,因为上面的 excludeDays 值在那里。

<div class="col-12" style="width: auto;" >
      <p-multiSelect [options]="excludeDays" optionLabel="label" defaultLabel="Select day" formControlName="excludeDays"></p-multiSelect> 
</div>

I am using我在用

this.form.patchValue({
... : ....
excludeDays: converted array of (excludeDays) to number array
})

This is the console output of the excludeDays array after converting it into number array这是将 excludeDays 数组转换为数字数组后的控制台 output 排除数组

After getting data from the backend, create an array of objects using the original array, then set those values to the patchValue method.从后端获取数据后,使用原始数组创建一个对象数组,然后将这些值设置为 patchValue 方法。

const excludeDays = [1,2].map(id=>this.excludeDays.find(day=>day.value === id));

this.form.patchValue({
excludeDays: excludeDays
})

Prime NG multiselect ( p-multiselect ) expects the same data type for ngModel property as that of options property. Prime NG multiselect ( p-multiselect ) 期望ngModel属性的数据类型与options属性的数据类型相同。

That means, if your options are an array of string (string[]) then you can have ngModel to be an array of string as well.这意味着,如果您的选项是一个字符串数组 (string[]),那么您也可以让 ngModel 成为一个字符串数组 But if your options are an array of objects , then options will have to be array of object (object must be of same type).但是如果你的选项是一个对象数组,那么选项必须是object的数组(对象必须是相同类型的)。

So for example, you have this array to bind to p-multiselect因此,例如,您将此数组绑定到 p-multiselect

excludeDays = [      
{label: 'Mon', value: 1},
{label: 'Tue', value: 2},
{label: 'Wed', value: 3},
{label: 'Thu', value: 4},
{label: 'Fri', value: 5},
{label: 'Sat', value: 6},
{label: 'Sun', value: 7}
]

and want to pre-select three values out of it, then you'll have to provide an array of same type for example并想从中预先选择三个值,那么您必须提供一个相同类型的数组

selectedDays = [
{label: 'Mon', value: 1},
{label: 'Tue', value: 2}
];

and your template would look like this你的模板看起来像这样

<p-multiSelect  [options]="excludeDays" [(ngModel)]="selectedDays"  defaultLabel="Select a day" optionLabel="label"></p-multiSelect>

Having explained that, you'll need to have the exact same array of objects in the patchValue method instead of having array of numbers.解释完之后,您需要在patchValue方法中使用完全相同的对象数组,而不是数字数组。

this.form.patchValue({
... : ....
excludeDays: excludeDays //instead of array of numbers you should have same array of objects.
})
let datafromBackend = [1, 2];

let UpdateSelection = [];
this.excludeDays.forEach((item) => {
  if (datafromBackend.findIndex((x) => x === item.value) > -1) {
    UpdateSelection.push(item);
  }
});

this.profileForm.get('excludeDays').setValue(UpdateSelection);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM