angular - Kendo Grid for Angular 2 Reactive FormArray

标签 angular kendo-ui kendo-grid kendo-ui-angular2

我在剑道网格示例中没有找到一个好的、简单且透明的表单示例,其中剑道网格作为 formArray,数组的每一行作为表单组,每个单元格作为表单控件

在另一个问题中Batch editing in 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>

有人做过这样的实现吗?

最佳答案

我已经找到了解决方案。这有点hacky,但是效果很好。您必须将每个 kendo-grid 数据项作为包含在 FormArray 中的 FormGroup 进行处理,并在每个单元格模板中使用 ng-container网格。就我而言,我从外部服务请求数据,但如果您在本地拥有数据,则几乎是相同的。 此 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>

父组件.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
      };
    });
  }

关于angular - Kendo Grid for Angular 2 Reactive FormArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51331725/

相关文章:

javascript - 使用网格 ID 隐藏剑道网格工具栏

javascript - 网格中的 Kendo DropDownList 在选择后才绑定(bind)

java - 我已允许 @CrossOrigin(origins ="*") 注释,但它仍然不起作用。谁能告诉我这里出了什么问题吗?

kendo-ui - 使用 requirejs 注入(inject) kendo ui

popup - 为什么当我点击更新时 Popup Kendo 没有关闭?

jsp - 属性数据的未知属性类型(对象)

javascript - 将Kendo Grid列显示为DateTime,但过滤时忽略Time

angular - 如何为事件发射器设置初始值

css - 如何防止 Angular 组件样式覆盖转移到其他组件?

Angular(ng 测试)调试 - VS 代码不会在断点处停止