簡體   English   中英

Kendo Grid for Angular 2反應式FormArray

[英]Kendo Grid for Angular 2 Reactive FormArray

我在Kendo網格示例中沒有找到一個很好,簡單且透明的表單示例,其中Kendo網格為formArray,數組的每一行為form組,每個單元格為formcontrol。

在另一個問題中, 在KendoUI Grid for Angular 2/4中進行批處理編輯有一個答案,但這並不是那么透明。

我不能使這些標簽起作用。

  <form [formGroup]="formGroup"><kendo-grid
  #grid
  [data]="gridData" [formArray]="formArray" formArrayName="arrayGrid" 
  //[formGroup]="gridRow"// how to say each row is in this form group
  [height]="410"
  >
    <ng-template kendoGridToolbarTemplate>
      <button *ngIf="!isEditMode" (click)="editHandler()" class="k-button k-primary">Edit</button>
      <button *ngIf="isEditMode" (click)="saveHandler()" [disabled]="!canSave()" class="k-button">Update</button>
      <button *ngIf="isEditMode" (click)="cancelHandler()" class="k-button">Cancel</button>
    </ng-template>
    <kendo-grid-column field="ProductName" formControlName="ProductName"  title="Name" width="200">
    </kendo-grid-column>
    <kendo-grid-column field="UnitPrice" formControlName="UnitPrice" title="Price" format="{0:c}" width="80" editor="numeric">
    </kendo-grid-column>
    <kendo-grid-column field="UnitsInStock" formControlName="UnitsInStock" title="In stock" width="80" editor="numeric">
    </kendo-grid-column>
</kendo-grid></form>

有人進行過這種實現嗎?

我已經找到了解決方案。 有點小巧,但效果很好。 您必須使用網格的每個單元格模板中的ng-container每個FormGroup -grid數據項作為FormGroup包含的FormArray進行處理。 就我而言,我從外部服務請求數據,但是如果您在本地擁有它,則幾乎是相同的。 FormArray也可以位於更大的FormGroup ,但為簡單起見,我將其作為屬性。

parent.component.html

<kendo-grid #grid [data]="gridData">
<kendo-grid-column field="firstField" title="ID" width="150">
  <ng-template kendoGridHeaderTemplate>
    <span>First Field</span>
  </ng-template>
  <ng-template kendoGridCellTemplate let-dataItem>
    <ng-container [formGroup]="dataItem">
      <app-my-component formControlName="firstField"></app-my-component>
    </ng-container>
  </ng-template>
</kendo-grid-column>
<kendo-grid-column field="secondField" width="145">
  <ng-template kendoGridHeaderTemplate>
    <span>Second Field</span>
  </ng-template>
  <ng-template kendoGridCellTemplate let-dataItem>
    <ng-container [formGroup]="dataItem">
      <kendo-dropdownlist formControlName="secondField" [valueField]="'id'" [textField]="'text'"></kendo-dropdownlist>
    </ng-container>
  </ng-template>
</kendo-grid-column>

parent.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { GridComponent, GridDataResult } from '@progress/kendo-angular-grid';
import { FormGroup, FormArray, FormBuilder } from '@angular/forms';

 export class ParentComponent implements OnInit {

 @ViewChild(GridComponent) private grid: GridComponent;
  public formArray = this.formBuilder.array([]);
  public gridData: GridDataResult;

  constructor(
    private formBuilder: FormBuilder,
    private service: MyService) {

    super();
  }

  ngOnInit() {
    this.requestData();
  }

  public requestData() {
    const response = this.service.getData().subscribe(data => {
      const that = this;
      response.forEach(function (data, i) {
        that.formArray.insert(i, that.createDataFormGroup(data));
      });

      this.gridData = {
        data: this.formArray.controls,
        total: this.formArray.controls.length
      };
    });
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM