【问题标题】:Kendo Angular Grid: Reactive forms value is emptyKendo Angular Grid:反应形式值为空
【发布时间】:2020-12-09 15:59:30
【问题描述】:

我目前正在构建一个基于Kendo Angular Grid 组件的可编辑网格。

我还使用 Reactive Forms 向 Grid 提供表单数据,如 this example 所示。实际上,我的代码和示例中的不太一样,因为我不能使用命令列(被设计团队禁止)。

现在,一切正常,除了 form.value 是空的...

这是我的组件的代码:

<!-- my-grid.component.html -->
<!-- Code voluntary simplified to improve readability -->

<kendo-grid
  #grid
  [data]="rows"
  [selectable]="{ mode: 'single', checkboxOnly: false, enabled: true }"
>
  <ng-template kendoGridToolbarTemplate>
    <button (click)="editHandler()">Edit</button>
    <button (click)="saveHandler()">Save</button>
  </ng-template>

  <kendo-grid-checkbox-column [width]="48" [style]="{ 'text-align': 'center' }"></kendo-grid-checkbox-column>
  <kendo-grid-column
    *ngFor="let column of columns"
    [field]="column.field"
    [title]="column.title"
    [format]="column.getFormat()"
    [editor]="column.type"
    [hidden]="column.hidden"
    [width]="150"
  ></kendo-grid-column>
</kendo-grid>
// my-grid.component.ts
// Code voluntary simplified to improve readability

editHandler(): void {
  const form = {};
  this.columns
    .forEach((c) => {
      const fc = new FormControl(this.selectedRow[c.field]);
      Object.defineProperty(form, c.field, { value: fc, writable: true }); // I can't know in advance what would be the columns of my grid...
    });

  this.currentForm = new FormGroup(form); // at this moment, the FormGroup is correctly built.
  this._grid.editRow(this.selectedRowIndex, this.currentForm);
}

saveHandler(): void {
  const formValue = this.currentForm.value; // yields {} instead of { myField1: 1, myField2: 2 }...
  // ...
}

这是我的堆栈:

  • 角度:9.1.11
  • 剑道角度网格:4.7.2

任何帮助将不胜感激。 :)

【问题讨论】:

    标签: angular kendo-ui kendo-grid kendo-angular-ui


    【解决方案1】:

    好吧,出于某种原因,将 enumerable 属性设置为 true 可以解决问题。

    Object.defineProperty(form, c.field, { value: fc, writable: true, enumerable: true });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      相关资源
      最近更新 更多