【问题标题】:p-dropdown in p-table dynamic rows PrimeNG - version 9 - Stackblitz addedp-table 动态行 PrimeNG 中的 p-dropdown - 版本 9 - Stackblitz 添加
【发布时间】:2020-05-20 17:58:25
【问题描述】:

我的要求是在两列网格的每一行中都有下拉菜单,如下所示:

这是我的问题的堆栈闪电:https://stackblitz.com/edit/angular9-primeng9

用户可以为每一行选择值并保存。我能够使用服务从数据源中获取数据,并在订阅 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" }]

【问题讨论】:

    标签: primeng angular9 primeng-turbotable primeng-dropdowns p-table


    【解决方案1】:

    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" }];
    

    【讨论】:

      猜你喜欢
      • 2020-02-17
      • 2019-05-02
      • 2018-12-07
      • 2018-09-15
      • 2018-10-21
      • 2019-04-09
      • 2017-01-16
      • 2019-11-11
      • 1970-01-01
      相关资源
      最近更新 更多