Angular 2 - Refresh ngx-datatable list issue
Asked Answered
L

4

8

I'm working on an Angular 2 project. I use ngx-datatable to display a list of users:

<ngx-datatable class="material"
[rows]="rows"
[columnMode]="'force'"
[headerHeight]="40"
[footerHeight]="40"
[rowHeight]="30"
[externalPaging]="true"
[limit]="limit"
(select)='onSelect($event)'
(activate)='onActivate($event)'
[selected]="selected"
[selectionType]="'checkbox'">
<ngx-datatable-column
    [width]="30"
    [sortable]="false"
    [canAutoResize]="false"
    [draggable]="false"
    [resizeable]="false"
    [headerCheckboxable]="true"
    [checkboxable]="true">
  </ngx-datatable-column>
      <ngx-datatable-column name="Name">
        <ng-template let-value="value" ngx-datatable-cell-template>
            <div class="redNumber">{{value}}</div>
        </ng-template>
  </ngx-datatable-column>
  <ngx-datatable-column name="Title"></ngx-datatable-column>
  <ngx-datatable-column name="company"></ngx-datatable-column>
  <ngx-datatable-column name="Status" [cellClass]="getStatusClass">  
  </ngx-datatable-column>
<ngx-datatable-column name="Last connexion"></ngx-datatable-column>

When I click on the button that trigger the following showAll() method, nothing happens, unless I click a second time, the list is refreshed, or if I click once, and resize my browser's window, the list is refreshed.. How can I refresh the list correctly? what am I doing wrong? In my component class, I have this:

export class M1OamRoomsListComponent{ 
    selected = [];
    public rows = [
      .... // list of users
    ];
public limit: number;

constructor(public translation: TranslationService){
    this.limit = 10;
}

getStatusClass({ row, column, value }): any {
    return {
        'disabled': value === 'Disabled',
        'enabled': value === 'Enabled'
    };
}
onSelect({ selected }) {
    console.log('Select Event', selected, this.selected);

    this.selected.splice(0, this.selected.length);
    this.selected.push(...selected);
}

onActivate(event) {
    //console.log('Activate Event', event);
}
showAll(): void {
    this.limit = this.rows.length;
}
}
Lanceolate answered 6/9, 2017 at 7:20 Comment(0)
T
25

I think to refresh the table you must do:

this.rows = [...this.rows]
Tripterous answered 10/4, 2018 at 20:38 Comment(4)
Thanks! Worked like a charm!Ale
love u broi! u saved me.Breadfruit
It looks they used angular changeDetection:onPush strategy for the grid to optimize performace. #of items are in the paging area were increasing, but rows were not getting updated.Echopraxia
Yeah, as I guessed swimlane.gitbook.io/ngx-datatable/cdEchopraxia
B
3
this.rows.push(result);
this.rows = [...this.rows]
Bouchier answered 17/10, 2018 at 21:40 Comment(0)
F
2

According to swimlane page of ngx-datatable, ngx-datatable follow OnPush change detection strategy, hence it only checks for immutable data types. So if you want ngx-datatable to detect changes. After doing :

  this.selected.push(...selected);

Do:

this.selected = [...this.selected]

Read more about change detection here: Link

Fallal answered 18/10, 2018 at 16:44 Comment(0)
P
1

I stumbled with this post because I was having a problem when any column was being sorted the UI was not updating a inner component until I scroll up and down again.

I managed to do a workaround with what was suggested previously:

UI:

<ngx-datatable (......) (sort)="onSort()">

TS:

  onSort() {
    const data = this.filteredTableValues;
    this.filteredTableValues = [];
    this.changeDetectorRef.detectChanges();
    this.filteredTableValues = [...data];
  }

Hope this helps someone in the same case

Paff answered 1/11, 2019 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.