我的要求是在网格的两列的每一行中都具有下拉菜单,如下所示:
以下是我遇到的问题:https://stackblitz.com/edit/angular9-primeng9
用户可以为每一行选择值并保存。我能够使用service从数据源中获取数据,并在observable的订阅中初始化下拉选项。但是,我无法设置页面加载时每行的选定项以显示预先存在的行(我认为设置[(ngModel)]的问题)。
此外,我需要添加一个加号按钮,该按钮将向该表添加新行,并且保存操作时结果应保存到数据库中。解决该问题的任何指导/线索都会有很大帮助。
以下是我正在使用的HTML代码:
<p-table [columns]="cols" [value]="rows">
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of columns">
{{col.header}}
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
<tr>
<td>
<p-dropdown [options]="DropdownSource" [(ngModel)]="rowData.AAttribute" placeholder="Select"
[showClear]="true"></p-dropdown>
</td>
<td>
<p-dropdown [options]="DropdownSource" [(ngModel)]="rowData.BAttribute" placeholder="Select"
[showClear]="true"></p-dropdown>
</td>
</tr>
</ng-template>
</p-table>
和组件(订阅onchanges):
this.cols = [
{ "field": "field_0", "header": "A Attribute" },
{ "field": "field_1", "header": "B Attribute" }
];
this.rows = [{ "AAttribute": "Data3", "BAttribute": "DataC" },
{ "AAttribute": "Data5", "BAttribute": "DataE" }];
this.DropdownSource= [
{ "AAttribute": "Data1", "BAttribute": "DataA" },
{ "AAttribute": "Data2", "BAttribute": "DataB" },
{ "AAttribute": "Data3", "BAttribute": "DataC" },
{ "AAttribute": "Data4", "BAttribute": "DataD" },
{ "AAttribute": "Data5", "BAttribute": "DataE" },
{ "AAttribute": "Data6", "BAttribute": "DataF" }]
答案 0 :(得分:0)
ngmodel需要数组中的对象。
请参阅下面的stackblitz https://stackblitz.com/edit/angular9-primeng9-dgxsjj
this.mappingRows = [{ "targetCol": "DataF", "SourceCol": this.sourceAttributes.find(p=>p.label === "Data3") },
{ "targetCol": this.targetAttributes[3], "SourceCol": "Data6" },
{ "targetCol": "DataC", "SourceCol": "Data1" }];