【问题标题】:How to create the editable primeNG table with Angular forms?如何使用 Angular 表单创建可编辑的 primeNG 表?
【发布时间】:2019-03-07 06:57:32
【问题描述】:

我正在尝试创建具有角度形式的 PrimeNg 可编辑表格。

app.component.ts(这是最小的可重现代码)

export class AppComponent implements OnInit {
    epForm: FormGroup;
    range: FormArray;

    constructor(private fb: FormBuilder,){
    }
    ngOnInit() {
        this.epForm = new FormGroup({});
        this.epForm.addControl('range', new FormArray([]));
        this.range = <FormArray>this.epForm.get('range');
        this.range.push(
        this.fb.group({
            type: ['X1 Gene']
        })
        );
    }
}

html文件是

<form [formGroup]="epForm" novalidate>
    <p-table [value]="epForm.controls.range.value" [rows]="10" [responsive]="true">
        <ng-template pTemplate="header">
            <tr> Range </tr>
        </ng-template>
        <ng-template pTemplate="body" let-i="rowIndex">            
            <tr [formGroupName]='i'>
                <td >
                    <input type="text" pInputText formControlName="type" />
                 </td>
             </tr>
        </ng-template>
      </p-table>
</form>

我尝试使用上面的代码显示内容,但我无法编辑输入标签。我打开检查元素并检查它,只有tbody 是keyon 更新。

我删除了[formgroup]='i',并在控制台中检查了它,但出现以下错误

 Cannot find control with path: 'range -> type'

我用&lt;table&gt; 尝试过同样的事情,它工作正常。但是使用 p-table 我得到了这种行为?我该如何解决这个问题。

StackBlitz

像下面的图片一样,我正在检查元素中,[formGroupName]

【问题讨论】:

  • [formGroupName]='i'替换为formGroupName='i'
  • 我尝试了但没有工作,我收到了Cannot find control with path: 'range -&gt; i'
  • 你有没有找到一种方法来使用primeng editable with formarray?我面临同样的问题,一些问题很多
  • @Liem 我使用 trackby 函数解决了这个问题,你可以在答案部分看到我的答案。

标签: angular primeng angular-forms


【解决方案1】:

我得到了我自己问题的答案,来自 primeNg documentation

性能提示

启用选择时,使用 dataKey 避免在比较对象时进行深度检查。

使用 rowTrackBy 避免不必要的 dom 操作。

对于大型数据集更喜欢延迟加载。

所以我修改了我的表像

<p-table [value]="epForm.controls.range.value" [rows]="10" [responsive]="true"
[rowTrackBy]="trackByFn">

在组件文件中我添加以下方法

trackByFn(index, row) {
    return index;
}

【讨论】:

    【解决方案2】:
    Use Like this as mentioned in Doc 
    

    https://www.primefaces.org/primeng/#/table/edit

    Html 
        <p-table [value]="cars">
            <ng-template pTemplate="header">
                <tr>
                    <th>Vin</th>
                    <th>Year</th>
                    <th>Brand</th>
                    <th>Color</th>
                </tr>
            </ng-template>
            <ng-template pTemplate="body" let-rowData>
                <tr>
                    <td pEditableColumn>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="rowData.vin">
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{rowData.vin}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td pEditableColumn>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="rowData.year" required>
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{rowData.year}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td pEditableColumn>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <p-dropdown [options]="brands" [(ngModel)]="rowData.brand" [style]="{'width':'100%'}"></p-dropdown>
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{rowData.brand}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td pEditableColumn>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="rowData.color">
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{rowData.color}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                </tr>
            </ng-template>
        </p-table>
    
        <h3>Row Editing</h3>
        <p-table [value]="cars2" dataKey="vin">
            <ng-template pTemplate="header">
                <tr>
                    <th>Vin</th>
                    <th>Year</th>
                    <th>Brand</th>
                    <th>Color</th>
                    <th style="width:8em"></th>
                </tr>
            </ng-template>
            <ng-template pTemplate="body" let-rowData let-editing="editing" let-ri="rowIndex">
                <tr [pEditableRow]="rowData">
                    <td>
                        {{rowData.vin}}
                    </td>
                    <td>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="rowData.year" required>
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{rowData.year}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <p-dropdown [options]="brands" [(ngModel)]="rowData.brand" [style]="{'width':'100%'}"></p-dropdown>
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{rowData.brand}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="rowData.color">
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{rowData.color}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td style="text-align:center">
                        <button *ngIf="!editing" pButton type="button" pInitEditableRow icon="pi pi-pencil" class="ui-button-info" (click)="onRowEditInit(rowData)"></button>
                        <button *ngIf="editing" pButton type="button" pSaveEditableRow icon="pi pi-check" class="ui-button-success" style="margin-right: .5em" (click)="onRowEditSave(rowData)"></button>
                        <button *ngIf="editing" pButton type="button" pCancelEditableRow icon="pi pi-times" class="ui-button-danger" (click)="onRowEditCancel(rowData, ri)"></button>
                    </td>
                </tr>
            </ng-template>
        </p-table>
    
    
    Component be like .
    
    enter code here
    
        export class TableEditDemo implements OnInit {
    
            cars1: Car[];
    
            cars2: Car[];
    
            brands: SelectItem[];
    
            clonedCars: { [s: string]: Car; } = {};
    
            constructor(private carService: CarService) { }
    
            ngOnInit() {
                this.carService.getCarsSmall().then(cars => this.cars1 = cars);
                this.carService.getCarsSmall().then(cars => this.cars2 = cars);
    
                this.brands = [
                    {label: 'Audi', value: 'Audi'},
                    {label: 'BMW', value: 'BMW'},
                    {label: 'Fiat', value: 'Fiat'},
                    {label: 'Ford', value: 'Ford'},
                    {label: 'Honda', value: 'Honda'},
                    {label: 'Jaguar', value: 'Jaguar'},
                    {label: 'Mercedes', value: 'Mercedes'},
                    {label: 'Renault', value: 'Renault'},
                    {label: 'VW', value: 'VW'},
                    {label: 'Volvo', value: 'Volvo'`enter code here`}
                ];
            }
    
            onRowEditInit(car: Car) {
                this.clonedCars[car.vin] = {...car};
            }
    
            onRowEditSave(car: Car) {
                if (car.year > 0) {
                    delete this.clonedCars[car.vin];
                    this.messageService.add({severity:'success', summary: 'Success', detail:'Car is updated'});
                }
                else {
                    this.messageService.add({severity:'error', summary: 'Error', detail:'Year is required'});
                }
            }
    
            onRowEditCancel(car: Car, index: number) {
                this.cars2[index] = this.clonedCars[car.vin];
                delete this.clonedCars[car.vin];
            }
    
        }
    

    【讨论】:

    • 感谢您的回答,但这与我处理多个输入的角度形式不同。
    • 您是否从 PrimeNG 导入了 TableModule?我已经导入了所需的模块,并且应用程序在这里运行-stackblitz.com/edit/… 我还删除了 [formGroupName]='i'
    猜你喜欢
    • 2013-10-11
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2018-01-18
    相关资源
    最近更新 更多