0

I'm doing a project in Angular 2 and I'm getting an issue that makes my pages lose a lot of performance. So, I have a table and everytime I scroll (up or down) it keeps calling one of my functions. This is the one:

.component.ts

entriesToShow(): Array<any>{
    let entries = [];
    let i = 0;
    if(Number(this.startingEntry)+Number(this.numOfEntries) <this.totalEntries)
        this.lastShowingEntry = Number(this.startingEntry)+Number(this.numOfEntries);
    else
        this.lastShowingEntry = this.totalEntries;

    if(this.dataTable != null && this.dataTable.aaData != null){
        for(i = this.startingEntry; i< this.lastShowingEntry; i++){
            entries.push(this.dataTable.aaData[i]);
        }
        this.lastShowingEntry = i;
        return entries;
    }else
        return null;
}

And in my html I got something like this:

<div class="col-md-12" *ngIf="isDataTableAvailable">
     <table id="table-EDIImports" class="table table-bordered display" cellspacing="0">
         <thead>
              <tr>
                   <th><strong>{{ 'POINT_OF_SALE' | translate }}</strong></th>

                    (more fields)

                    <th *ngIf="mode=='EDIT'"></th>
             </tr>
             <tr *ngFor="let obj of entriesToShow()" [ngSwitch]="obj.Status">
                    <th>{{ obj.PointOfSell }}</th>
                    <th *ngIf="obj.LineID == editRowId && selectedField == 'NIF' && mode=='EDIT'">
                        <input type="text" [(ngModel)]="valueToSave" value="{{ obj.NIF }}">
                     </th>
                        (more fields)
              </tr>
           </thead>
           <tbody></tbody>
       </table>
 </div>

Any advice to make my page stop calling entriesToShow() everytime I scroll up/down?

Thank for the help.

EDIT: Removed some extra code

1

1 Answer 1

2

Don't call the entriesToShow() function from your template! It cause's the function to be called every change detection! Instead you should store the data in a variable in your component and the ngFor should iterate over him. .component.ts

        entries:Array<any>;

        ngOnInit(){
           this.entries=this.entriesToShow();
        }
        entriesToShow(): Array<any>{
            let entries = [];
            let i = 0;
            if(Number(this.startingEntry)+Number(this.numOfEntries) <this.totalEntries)
                this.lastShowingEntry = Number(this.startingEntry)+Number(this.numOfEntries);
            else
                this.lastShowingEntry = this.totalEntries;

            if(this.dataTable != null && this.dataTable.aaData != null){
                for(i = this.startingEntry; i< this.lastShowingEntry; i++){
                    entries.push(this.dataTable.aaData[i]);
                }
                this.lastShowingEntry = i;
                return entries;
            }else
                return null;
        }

.html

<div class="col-md-12" *ngIf="isDataTableAvailable">
     <table id="table-EDIImports" class="table table-bordered display" cellspacing="0">
         <thead>
              <tr>
                   <th><strong>{{ 'POINT_OF_SALE' | translate }}</strong></th>

                    (more fields)

                    <th *ngIf="mode=='EDIT'"></th>
             </tr>
             <tr *ngFor="let obj of entries" [ngSwitch]="obj.Status">
                    <th>{{ obj.PointOfSell }}</th>
                    <th *ngIf="obj.LineID == editRowId && selectedField == 'NIF' && mode=='EDIT'">
                        <input type="text" [(ngModel)]="valueToSave" value="{{ obj.NIF }}">
                     </th>
                        (more fields)
              </tr>
           </thead>
           <tbody></tbody>
       </table>
 </div>
Sign up to request clarification or add additional context in comments.

13 Comments

Well... that didn't work.. I can't get the data rendered that way... check the img: imgur.com/H6qkCIK
Why can't you get the data? What did you try to show me in the image?
Well... the page will access a database and then load the data, when the html is renderer I don't have any entries yet, I guess thats making the table empty
Did you debuged the code to make sure entriesToShow() doesn't return null or empty array?
Well, the pagination and number of entries are there, if you see I have 4334 entries, so it isn't null... The method to get the entries work fine, my problem is just everytime I scroll it run the method over and over
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.