Angular 角度2+;-组合框筛选器(在init上显示加载的信息)
我正在从数据库加载可以有父项(父项ID)的记录(组织);什么时候我正在显示和编辑信息(使用与反应式表单相同的表单),我正在使用具有过滤功能的剑道下拉列表;我正在加载它的初始状态(在ngOnInit上)。虽然该值在combo中设置正确,但不会显示文本 component.htmlAngular 角度2+;-组合框筛选器(在init上显示加载的信息),angular,filter,combobox,kendo-ui-angular2,Angular,Filter,Combobox,Kendo Ui Angular2,我正在从数据库加载可以有父项(父项ID)的记录(组织);什么时候我正在显示和编辑信息(使用与反应式表单相同的表单),我正在使用具有过滤功能的剑道下拉列表;我正在加载它的初始状态(在ngOnInit上)。虽然该值在combo中设置正确,但不会显示文本 component.html <kendo-combobox #orgParentId formControlname="parentId"
<kendo-combobox
#orgParentId
formControlname="parentId"
[data]="organizationsLike"
[textField]="'name'"
[valueField]="'id'"
[filterable]="true"
(filterChange)="handleFilter($event)"
[placeholder]="'Search parent...'"
[suggest]="true"
[valuePrimitive]="true"
(selectionChange)="parentIdSelChanged($event)"
>
</kendo-combobox>
我做错了什么?只需在组合框中添加一个
ngModel
<kendo-combobox
#orgParentId
formControlname="parentId"
[data]="organizationsLike"
[textField]="'name'"
[valueField]="'id'"
[filterable]="true"
(filterChange)="handleFilter($event)"
[placeholder]="'Search parent...'"
[suggest]="true"
[valuePrimitive]="true"
(selectionChange)="parentIdSelChanged($event)"
[(ngModel)]="selectedOption">
</kendo-combobox>
更新:根据评论
如果您使用的是被动表单,那么只需将初始值指定为表单控件构造函数的第一个参数即可
示例:
public listItems: string[] = [ {name: "option1", id: 1}, {name: "option2", id: 2}, {name: "option3", id: 3} ];
public sampleFormGroup: FormGroup = new FormGroup({
myComboBox: new FormControl(this.listItems[0])
// ^^^^^^^^^^^^^^^^ INITIAL VALUE
});
无论您指定了什么值,最初都会显示在组合框中
(使用反应形式)
希望这有帮助非常感谢您的回答,但我使用的是反应式表单,不是模板驱动的……非常感谢!!!老实说,我没有像你说的那样做,但是我改变了定义源代码的方式:导出类OrganizationEditComponent实现OnInit,OnDestroy{….organizationsLike:Array=[];…}其余的都是一样的。。。我没有初始值,这个值是在从数据库读取信息时设置的。再次感谢您的努力,祝您寒假愉快!
selectedOption = JSON.parse(JSON.stringify(this.organizationsLike[0]));
public listItems: string[] = [ {name: "option1", id: 1}, {name: "option2", id: 2}, {name: "option3", id: 3} ];
public sampleFormGroup: FormGroup = new FormGroup({
myComboBox: new FormControl(this.listItems[0])
// ^^^^^^^^^^^^^^^^ INITIAL VALUE
});