angular - 如何使用 Angular 表单创建可编辑的 primeNG 表?

标签 angular primeng angular-forms

我正在尝试创建具有 Angular 形式的 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正在更新中。

我删除了 [formgroup]='i'我在控制台中检查了它,我得到了以下错误

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

我用 <table> 尝试过同样的事情它工作正常。但是对于 p-table 我得到了这种行为?我该如何解决这个问题。

StackBlitz

就像下面的图片一样,我正在使用 [formGroupName] 进入检查元素。

enter image description here

最佳答案

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

Performance Tips

When selection is enabled use dataKey to avoid deep checking when comparing objects.

Use rowTrackBy to avoid unnecessary dom operations.

Prefer lazy loading for large datasets.

所以我修改了我的表格

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

在组件文件中我添加了下面的方法

trackByFn(index, row) {
    return index;
}

关于angular - 如何使用 Angular 表单创建可编辑的 primeNG 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55037776/

相关文章:

angular - 如何在 Angular 2 中以 react 形式重置验证器?

angular - 如何在 Jasmine/Angular 中模拟服务的事件发射器

angular - 如何检查使用的 Angular 版本

angular - 空注入(inject)器错误 : No provider for FormBuilder

javascript - 如何在同一控件上为不同事件实现不同的 debounceTime?

angular - 如何设置 PrimeNG 主题?

angular - 在带有模板的 Angular ng-bootstrap 模式中,关闭和关闭按钮不起作用

angular - 移动 Safari 下载问题 : The operation couldn’t be completed.(webkitblobresource 错误 1。)

angular - 在没有 [()] 语法的情况下使用 ngModel 指令的支持属性是什么

angular - 如何在 Angular 的第一次更改时触发表单输入验证