javascript - 如何使用用户输入的数据向 ngx-datatable 添加新行?

标签 javascript angular ngx-datatable

如何根据用户输入向 ngx-datatable 添加新行?

我添加一个空行,如下所示:

addRow() {
  this.rows.unshift({unique_id: '<em>empty</em>', name: '<em>empty</em>'});
  this.rows = this.rows.slice();
}

但是我如何控制该行以便用户可以向该行添加数据?

最佳答案

更改你的 component.html 以循环所有标签

<ngx-datatable
  #mydatatable
  class="material"
  [headerHeight]="50"
  [limit]="5"
  [columnMode]="'force'"
  [footerHeight]="50"
  [rowHeight]="'auto'"
  [rows]="rows"
  [scrollbarH]="true">
  <ngx-datatable-column *ngFor="let label of labels" [name]="label.name" [prop]="label.prop">
    <ng-template ngx-datatable-cell-template let-rowIndex="rowIndex" let-value="value" let-row="row">
      <span
        title="double Click here "
        (dblclick)="editCell(label.prop, rowIndex)"
        *ngIf="!editing[rowIndex + '-' + label.prop]"
      >
        {{ value }}
      </span>
      <input
        autofocus
        (blur)="updateValue($event, label.prop, rowIndex)"
        (keyup.enter)="updateValue($event, label.prop, rowIndex)"
        *ngIf="editing[rowIndex+ '-' + label.prop]"
        [type]="label.inputType"
        class="form-control"
        [value]="value"
      />
    </ng-template>
  </ngx-datatable-column>
</ngx-datatable>

然后将这些方法添加到您的 component.ts 中

@Component({
  selector: 'app-mycomponent',
  templateUrl: './mycomponent.component.html',
  styleUrls: ['./mycomponent.component.css']
})
export class MyComponent{
  editing = {};
  rows = [];
  labels = [];
  
  // call to update cell value
  updateValue(event, cell, rowIndex) {
    this.editing[rowIndex + '-' + cell] = false;
    this.rows[rowIndex][cell] = event.target.value;
    this.rows = [...this.rows];
  }

  // call on double click in cell
  editCell(cell, rowIndex) {
    this.editing = {};
    this.editing[rowIndex + '-' + cell] = true;
  } 
}

关于javascript - 如何使用用户输入的数据向 ngx-datatable 添加新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48566169/

相关文章:

javascript - Amplify.service.api.get 返回sampleCloudAPI 不存在

javascript - 需要根据行索引隐藏 ngx-datatable 的行

css - Ngx-datatable 在悬停时显示图标

javascript - 从 jquery ajax 加载导入数据时创建的重复 div 标签

javascript - 谷歌地图信息窗口closeclick事件(函数参数)

javascript - 将 jQuery 值设置为 <c :forEach> 的 "items"属性

javascript - 如何在具有已知类名的特定行第一次出现之后/之前移动行?

javascript - Angular2 或 React 或任何其他

Angular 5 - 将服务限制为模块

angular - 我在 ngx-datatable 的每个标题单元格中都有一个自定义下拉组件