如何在对象数组中设置默认选择值?

huangapple go评论71阅读模式
英文:

How do I set the default select value on an array of objects?

问题

我有一个与对象集合绑定的选择框。我想要添加一个默认值 "请选择",但似乎无法默认选择该项:

<select [(ngModel)]="ingredient.definition" class="form-select" name="ingredient{{ i }}">
    <option value="" disabled selected>请选择</option>
    <option *ngFor="let definition of definitions" [ngValue]="definition" [disabled]="disableOption(definition)">{{ definition.name }}</option>
</select>

<button type="button" class="btn btn-primary d-block" [disabled]="disableAdd()" (click)="addIngredient()">添加配料</button>

这是我的definition接口:

export interface Definition {
  id: number;
  name: string;
  measurement: string;
  unit: string;
  measurementToUnit: number;
  unitPrice: number;
}

和我的ingredient接口:

import { Definition } from './definition';

export interface Ingredient {
  quantity: number;
  definition: Definition;
}

选择框是动态添加的,因此在添加选择框后,我执行以下操作创建一个空对象:

addIngredient(): void {
    let ingredient: Ingredient = {
      quantity: 0,
      definition: {} as Definition
    }
    this.model.ingredients.push(ingredient);
}

由于我的属性上的definition对象为空,我希望选择框会自动突出显示空的默认选项,但实际情况并非如此。

我还尝试创建一个名为 "请选择" 的虚拟 "definition" 对象并将其添加到 "definitions" 集合中,但似乎也不起作用。

有人知道使其正常工作的正确方法吗?

英文:

I have a select that binds to a collection of objects. I wanted to add a default value "Please select" but I can't seem to get it to select the item by default:

&lt;select [(ngModel)]=&quot;ingredient.definition&quot; class=&quot;form-select&quot; name=&quot;ingredient{{ i }}&quot;&gt;
  	&lt;option value=&quot;&quot; disabled selected&gt;Please select&lt;/option&gt;
   	&lt;option *ngFor=&quot;let definition of definitions&quot; [ngValue]=&quot;definition&quot; [disabled]=&quot;disableOption(definition)&quot;&gt;{{ definition.name }}&lt;/option&gt;
&lt;/select&gt;

&lt;button type=&quot;button&quot; class=&quot;btn btn-primary d-block&quot; [disabled]=&quot;disableAdd()&quot; (click)=&quot;addIngredient()&quot;&gt;Add ingredient&lt;/button&gt;

Here is my definition interface:

export interface Definition {
  id: number;
  name: string;
  measurement: string;
  unit: string;
  measurementToUnit: number;
  unitPrice: number;
}

and my ingredient interface:

import { Definition } from &#39;./definition&#39;;

export interface Ingredient {
  quantity: number;
  definition: Definition;
}

The select is dynamically added, so upon adding the select I execute the following to create an empty object:

addIngredient(): void {
    let ingredient: Ingredient = {
      quantity: 0,
      definition: {} as Definition
    }
    this.model.ingredients.push(ingredient);
}

Since the definition object on my property is empty, I was hoping the select would automatically highlight the empty default option, however this isn't the case:

如何在对象数组中设置默认选择值?

I've also tried creating a dummy definition object with the name "Please select" and adding that to the definitions collection, however that doesn't seem to work either.

Does anyone know the correct way to get this working?

Thanks

答案1

得分: 1

我遇到过几次类似的情况,这个问题是由于选项动态填充时的索引引起的。

尝试使用formControl和patchValue,而不是ngModel和ngValue。

TS:

public defaultSelectName: string = 'someOption';

public optionsForm: FormGroup = new FormGroup({
    options: new FormControl(null, {validators: Validators.nullValidator})
});

public setDefaultOption(): void {
    const matchedIndex= this.definitions.findIndex(definition => definition.name === defaultSelectName);
    if (matchedIndex) {
        this.optionsForm.get('options')!.patchValue(this.definitions[matchedIndex].name)
    }
}

通过这样做,您可以明确地设置默认选择的值以及其索引。

英文:

I came across such scenarios a couple of times as this issue arises due to indexing of options dynamically populating.

Instead of ngModel and ngValue, try using formControl and patchvalue.

TS:

public defaultSelectName: string = &#39;someOption&#39;;

public optionsForm: FormGroup = new FormGroup({
    options: new FormControl(null, {validators: Validators.nullValidator})
});

public setDefaultOption(): void {
    const matchedIndex= this.definitions.findIndex(definition =&gt; definition.name === defaultSelectName);
    if (matchedIndex) {
        this.optionsForm.get(&#39;options&#39;)!.patchValue(this. definitions[matchedIndex].name)
    }
}

by doing so, you're explicitly setting the default selected value along with its index.

答案2

得分: 0

根据@Eliseo的建议,我成功地使其工作:

&lt;option [value]=&quot;{}&quot; disabled&gt;请选择&lt;/option&gt;

非常感谢

英文:

so following on from @Eliseo's suggestion, I managed to get it to work with:

&lt;option [value]=&quot;{}&quot; disabled&gt;Please select&lt;/option&gt;

Thanks so much

huangapple
  • 本文由 发表于 2023年2月8日 18:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定