gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:35:09 26 4
gpt4 key购买 nike

如何根据用户输入向 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com