如何在 Angular 2 中使用 ngModel 在多选中设置默认选定值
How to set default selected values in multiselect with ngModel in Angular 2
如何在多选中设置默认选择值。我从数据库中得到 current_options
和 all_options
,我想更新 current_options
并再次向数据库发送新值。
更新数据库有效,但是当我刷新页面时 none 个选项被选中。
current_options = [{id:1, name:'name1'}]; #from database
all_options = [{id:1, name:'name1'},{id:2, name:'name2'}]; #from database
我的模板:
<select multiple name="type" [(ngModel)]="current_options">
<option *ngFor="let option of all_options" [ngValue] = "option">
{{option.name}}
</option>
</select>`
您应该使用所选项目的数组
<select [(ngModel)]="selectedElement" multiple>
<option *ngFor="let type of types" [ngValue]="type"> {{type.Name}}</option>
</select>
我选择的项目如下
selectedElement:any= [
{id:1,Name:'abc'},
{id:2,Name:'abdfsdgsc'}];
current_options = [all_options[0]]
初始化输入的默认值。
Current_options 需要使用包含 all_options.
中存在的相同对象的数组进行初始化
我从另一个答案中分叉了 Plunker 来说明它。
请记住:
{id:1, name:'name1'} !== {id:1, name:'name1'}
编辑:
假设 current_options 已经包含您从服务器接收到的一些值:
current_options = current_options.map((current_option) => {
return all_options.find((all_option) => current_option.id === all_option.id);
})
或者可能性能更高:
for (let i in all_options) {
for (let j in current_options) {
if (all_options[i].id === current_options[j].id ) {
current_options[j] = all_options[i];
}
}
}
编辑:
根据 angular documentation,您可以使用 compare with 函数指定如何假设两个对象相等。
<select multiple [compareWith]="compareFn" ...>
</select>
compareFn(c1: Category, c2: Category): boolean {
return c1 && c2 ? c1.id === c2.id : c1 === c2;
}
如果您将值作为 id 数组传递给 ngModel
let idArrary = ["1"];
<select multiple name="type" [(ngModel)]="idArrary">
<option *ngFor="let option of all_options" [ngValue] = "option">
{{option.name}}
</option>
</select>
`
如果您想使用 ngModel 创建多个 select 和选项并设置默认值和双向绑定其工作代码
<div *ngFor="let options of optionsArray; let in = index">
<br>
<select [(ngModel)]="res[in]" >
<option [ngValue]="option" *ngFor="let option of options.options; let i =index">
{{option}}
</option>
</select>
{{res[in]}}
</div>
{{res}}
export class ExComponent implements OnInit {
public res=[];
public optionsArray = [
{id: 1, text: 'Sentence 1', options:['kapil','vinay']},
{id: 2, text: 'Sentence 2', options:['mukesh','anil']},
{id: 3, text: 'Sentence 3', options:['viky','kd']},
{id: 4, text: 'Sentence 4', options:['alok','gorva']},
]
ngOnInit()
{
this.optionsArray.forEach(data=>{
this.res.push(data.options[0]);
})
}
如何在多选中设置默认选择值。我从数据库中得到 current_options
和 all_options
,我想更新 current_options
并再次向数据库发送新值。
更新数据库有效,但是当我刷新页面时 none 个选项被选中。
current_options = [{id:1, name:'name1'}]; #from database
all_options = [{id:1, name:'name1'},{id:2, name:'name2'}]; #from database
我的模板:
<select multiple name="type" [(ngModel)]="current_options">
<option *ngFor="let option of all_options" [ngValue] = "option">
{{option.name}}
</option>
</select>`
您应该使用所选项目的数组
<select [(ngModel)]="selectedElement" multiple>
<option *ngFor="let type of types" [ngValue]="type"> {{type.Name}}</option>
</select>
我选择的项目如下
selectedElement:any= [
{id:1,Name:'abc'},
{id:2,Name:'abdfsdgsc'}];
current_options = [all_options[0]]
初始化输入的默认值。
Current_options 需要使用包含 all_options.
中存在的相同对象的数组进行初始化我从另一个答案中分叉了 Plunker 来说明它。
请记住:
{id:1, name:'name1'} !== {id:1, name:'name1'}
编辑:
假设 current_options 已经包含您从服务器接收到的一些值:
current_options = current_options.map((current_option) => {
return all_options.find((all_option) => current_option.id === all_option.id);
})
或者可能性能更高:
for (let i in all_options) {
for (let j in current_options) {
if (all_options[i].id === current_options[j].id ) {
current_options[j] = all_options[i];
}
}
}
编辑: 根据 angular documentation,您可以使用 compare with 函数指定如何假设两个对象相等。
<select multiple [compareWith]="compareFn" ...>
</select>
compareFn(c1: Category, c2: Category): boolean {
return c1 && c2 ? c1.id === c2.id : c1 === c2;
}
如果您将值作为 id 数组传递给 ngModel
let idArrary = ["1"];
<select multiple name="type" [(ngModel)]="idArrary">
<option *ngFor="let option of all_options" [ngValue] = "option">
{{option.name}}
</option>
</select>
`
如果您想使用 ngModel 创建多个 select 和选项并设置默认值和双向绑定其工作代码
<div *ngFor="let options of optionsArray; let in = index">
<br>
<select [(ngModel)]="res[in]" >
<option [ngValue]="option" *ngFor="let option of options.options; let i =index">
{{option}}
</option>
</select>
{{res[in]}}
</div>
{{res}}
export class ExComponent implements OnInit {
public res=[];
public optionsArray = [
{id: 1, text: 'Sentence 1', options:['kapil','vinay']},
{id: 2, text: 'Sentence 2', options:['mukesh','anil']},
{id: 3, text: 'Sentence 3', options:['viky','kd']},
{id: 4, text: 'Sentence 4', options:['alok','gorva']},
]
ngOnInit()
{
this.optionsArray.forEach(data=>{
this.res.push(data.options[0]);
})
}