最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ngx-Datatable - rowIndex value incorrect when sorting column - Stack Overflow

programmeradmin0浏览0评论

I'm using ngx-datatable which works great but just facing an issue on the following behaviour:

  • I have a toggle switch which changes the column property when toggled:

  • To change the property value, I'm using below code in template and ponent:
<ngx-datatable-column name="Activation Status" prop="activation_status">
  <ng-template ngx-datatable-cell-template let-value="value" let-row="row" let-rowIndex="rowIndex">
    <mat-slide-toggle *ngIf="value === 'ACTIVATED'" color="accent" checked="true" disabled="true">
      {{value}}
    </mat-slide-toggle>
    <mat-slide-toggle *ngIf="value === 'PENDING'" color="accent" checked="false" (change)="onToggle(rowIndex)">
      {{value}}
    </mat-slide-toggle>
  </ng-template>
</ngx-datatable-column>

    onToggle(rowIndex) {
    setTimeout(() => {
      this.rows[rowIndex].activation_status = 'ACTIVATED';
      this.rows = [...this.rows];
    }, 100);
    console.log(rowIndex);
  }

The property is updated fine as long as the column is not sorted.

If I sort the column, then the rowIndex is kept as per original value and the property is not updated.

Any solution for this ?

Thanks

I'm using ngx-datatable which works great but just facing an issue on the following behaviour:

  • I have a toggle switch which changes the column property when toggled:

  • To change the property value, I'm using below code in template and ponent:
<ngx-datatable-column name="Activation Status" prop="activation_status">
  <ng-template ngx-datatable-cell-template let-value="value" let-row="row" let-rowIndex="rowIndex">
    <mat-slide-toggle *ngIf="value === 'ACTIVATED'" color="accent" checked="true" disabled="true">
      {{value}}
    </mat-slide-toggle>
    <mat-slide-toggle *ngIf="value === 'PENDING'" color="accent" checked="false" (change)="onToggle(rowIndex)">
      {{value}}
    </mat-slide-toggle>
  </ng-template>
</ngx-datatable-column>

    onToggle(rowIndex) {
    setTimeout(() => {
      this.rows[rowIndex].activation_status = 'ACTIVATED';
      this.rows = [...this.rows];
    }, 100);
    console.log(rowIndex);
  }

The property is updated fine as long as the column is not sorted.

If I sort the column, then the rowIndex is kept as per original value and the property is not updated.

Any solution for this ?

Thanks

Share Improve this question edited Apr 28, 2018 at 12:09 Diego Venâncio 6,0372 gold badges53 silver badges73 bronze badges asked Apr 28, 2018 at 11:47 Luca BrasiLuca Brasi 7312 gold badges13 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Ok I think that's a noob mistake in my TS code.

I was modifying the rows object directly, which from my understanding is an immutable.

So instead if I do the following by modifying the let-row object then it works:

<ng-template ngx-datatable-cell-template let-value="value" let-row="row" let-rowIndex="rowIndex">
  <mat-slide-toggle *ngIf="value=='ACTIVATED'" color="accent" checked="true" disabled="true">
    {{value}}
  </mat-slide-toggle>
  <mat-slide-toggle *ngIf="value=='PENDING'" color="accent" checked="false" (change)="onToggle(row)">
    {{value}}
  </mat-slide-toggle>
</ng-template>

onToggle(row: any) {
   setTimeout(() => {
   row.activation_status = 'ACTIVATED';
 }, 200);
   console.log(row);
}
发布评论

评论列表(0)

  1. 暂无评论