简体   繁体   English

Angular 2 中的属性绑定

[英]Property Binding in Angular 2

I'm attempting to create inline editing functionality on tabular data in an Angular 2 application.我正在尝试在 Angular 2 应用程序中对表格数据创建内联编辑功能。 Each row displays an edit icon which toggles the inline editor.每行显示一个编辑图标,用于切换内联编辑器。 When editing, there should be two buttons, one to cancel and another to save the row.编辑时,应该有两个按钮,一个是取消,另一个是保存行。

Currently I'm having trouble when trying to revert to the previous state when canceling my edit.目前,我在取消编辑时尝试恢复到以前的状态时遇到了问题。 Please see the sample code below for more information.请参阅下面的示例代码以获取更多信息。

Typescript:打字稿:

interface IRow {
  id: number;
  foo: string;
  bar: string;
  baz: string;
  isEditing: boolean;
}

Template:模板:

<div class="table">
  <div class="table-head">
    <div class="table-row">
      <div class="table-cell">Foo</div>
      <div class="table-cell">Bar</div>
      <div class="table-cell">Baz</div>
      <div class="table-cell"><!-- Edit --></div>
    </div>
  </div>
  <div class="table-body">
    <div *ngFor="let row of rows" class="table-row">

      <-- Show this if not editing -->
      <ng-container *ngIf="!row.isEditing; else editing">
        <div class="table-cell">{{ row.foo }}</div>
        <div class="table-cell">{{ row.bar }}</div>
        <div class="table-cell">{{ row.baz }}</div>
        <div class="table-cell">
          <button (click)="edit(row)>
            <i class="icon-pencil"></i>
          </button>
        </div>
      </ng-container>

      <!-- Show this if editing -->
      <ng-template #editing>
        <div class="table-cell"><input type="text" [(value)]="row.foo"></div>
        <div class="table-cell"><input type="text" [(value)]="row.bar"></div>
        <div class="table-cell"><input type="text" [(value)]="row.baz"></div>
        <div class="table-cell">
          <button (click)="cancel(row)>
            <i class="icon-back"></i>
          </button>
          <button (click)="save(row)>
            <i class="icon-save"></i>
          </button>
        </div>
      <ng-template>

    </div>
  </div>
<div>

Component:成分:

// Class variable
public originalRow;

edit(row) {
  // Save a copy of the original
  this.originalRow = { ...row };
  this.row.isEditing = true;
}

cancel(row) {
  // This doesn't work
  // row = this.originalRow; 

  // This works
  row.foo = this.originalRow.foo;
  row.bar = this.originalRow.bar;
  row.baz = this.originalRow.baz;
  this.row.isEditing = false;
}

save(row) {
  // Store new value in state
  this.originalRow = row;

  // Post update
  this.rowSvc.updateRow(row);
}

What's the best strategy for reverting data to its previous state on cancel of the edit?在取消编辑时将数据恢复到先前状态的最佳策略是什么?

This is the expected behavior.这是预期的行为。 When you set the row , you just overwrite the parameter and losing the object reference.设置row ,您只需覆盖参数并丢失对象引用。

First set the index:首先设置索引:

<div *ngFor="let row of rows; let i = index" class="table-row">

Then pas the index to edit function:然后将索引传递给编辑功能:

<-- Show this if not editing -->
<ng-container *ngIf="!row.isEditing; else editing">
  <div class="table-cell">{{ row.foo }}</div>   <div class="table-cell">{{ row.bar }}</div>
  <div class="table-cell">{{ row.baz }}</div>
  <div class="table-cell">
    <button (click)="edit(row, i)> <-- Pass the index -->
      <i class="icon-pencil"></i>
    </button>
  </div>
</ng-container>

Use index in the cancel function to set the row to original state:在取消函数中使用 index 将行设置为原始状态:

cancel(row: IRow, index: number) {
  this.rows[index] = { ...this.originalRow };
  this.row.isEditing = false;
}

Another option would be simply iterating over the attributes of the row :另一种选择是简单地迭代row的属性:

cancel(row: IRow) {
  Object.keys(row).forEach((key: string) => row[key] = this.originalRow[key]);
  this.row.isEditing = false;
}

Note that, in either solution, you'd have problems with editing multiple rows at the same time.请注意,在任一解决方案中,您在同时编辑多行时都会遇到问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM