Skip to content

Commit

Permalink
Fixed #4361
Browse files Browse the repository at this point in the history
  • Loading branch information
Çağatay Çivici committed Nov 6, 2017
1 parent 18bc9b5 commit 9814130
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
30 changes: 25 additions & 5 deletions src/app/components/datascroller/datascroller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NgModule,Component,ElementRef,AfterViewInit,AfterContentInit,OnDestroy,Input,Output,Renderer2,ViewChild,EventEmitter,ContentChild,ContentChildren,QueryList,TemplateRef} from '@angular/core';
import {NgModule,Component,ElementRef,AfterViewInit,AfterContentInit,DoCheck,OnDestroy,Input,Output,Renderer2,ViewChild,EventEmitter,ContentChild,ContentChildren,QueryList,TemplateRef,IterableDiffers} from '@angular/core';
import {CommonModule} from '@angular/common';
import {Header,Footer,PrimeTemplate,SharedModule} from '../common/shared';
import {DomHandler} from '../dom/domhandler';
Expand All @@ -12,7 +12,7 @@ import {DomHandler} from '../dom/domhandler';
</div>
<div #content class="ui-datascroller-content ui-widget-content" [ngStyle]="{'max-height': scrollHeight}">
<ul class="ui-datascroller-list">
<li *ngFor="let item of dataToRender">
<li *ngFor="let item of dataToRender;trackBy: trackBy">
<ng-template [pTemplateWrapper]="itemTemplate" [item]="item"></ng-template>
</li>
</ul>
Expand All @@ -24,7 +24,7 @@ import {DomHandler} from '../dom/domhandler';
`,
providers: [DomHandler]
})
export class DataScroller implements AfterViewInit,OnDestroy {
export class DataScroller implements AfterViewInit,DoCheck,OnDestroy {

@Input() rows: number;

Expand All @@ -44,6 +44,10 @@ export class DataScroller implements AfterViewInit,OnDestroy {

@Input() loader: any;

@Input() trackBy: Function = (index: number, item: any) => item;

@Input() immutable: boolean = true;

@ViewChild('content') contentViewChild: ElementRef;

@ContentChild(Header) header;
Expand All @@ -64,7 +68,11 @@ export class DataScroller implements AfterViewInit,OnDestroy {

contentElement: HTMLDivElement;

constructor(public el: ElementRef, public renderer: Renderer2, public domHandler: DomHandler) {}
differ: any;

constructor(public el: ElementRef, public renderer: Renderer2, public domHandler: DomHandler, public differs: IterableDiffers) {
this.differ = differs.find([]).create(null);
}

ngAfterViewInit() {
if(this.lazy) {
Expand Down Expand Up @@ -101,7 +109,10 @@ export class DataScroller implements AfterViewInit,OnDestroy {

set value(val:any[]) {
this._value = val;
this.handleDataChange();

if(this.immutable) {
this.handleDataChange();
}
}

handleDataChange() {
Expand All @@ -110,6 +121,15 @@ export class DataScroller implements AfterViewInit,OnDestroy {
else
this.load();
}

ngDoCheck() {
if(!this.immutable) {
let changes = this.differ.diff(this.value);
if(changes) {
this.handleDataChange();
}
}
}

load() {
if(this.lazy) {
Expand Down
25 changes: 21 additions & 4 deletions src/app/showcase/components/datascroller/datascrollerdemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</div>

<div class="content-section implementation">
<button pButton label="Check" (click)="addCar()"></button>
<p-dataScroller [value]="cars" [rows]="10" [buffer]="0.4">
<p-header>
Scroll Down to to Load More
Expand Down Expand Up @@ -143,10 +144,14 @@ <h3>Getting Started</h3>
</code>
</pre>

<h3>Managing Data</h3>
<p>DataTable uses setter based checking to realize if the underlying data has changed to update the UI so your data changes such as adding or removing a record
should always create a new array reference instead of manipulating an existing array. For example, use slice instead of splice when removing an item
or use spread operator instead of push method when adding an item.</p>
<h3>Change Detection</h3>
<p>DataScroller either uses setter based checking or ngDoCheck to realize if the underlying data has changed to update the UI. This is configured using the immutable
property, when enabled (default) setter based detection is utilized so your data changes such as adding or removing a record
should always create a new array reference instead of manipulating an existing array as Angular does not trigger setters if the reference does not change.
For example, use slice instead of splice when removing an item or use spread operator instead of push method when adding an item. On the other hand, setting immutable property to false removes
this restriction by using ngDoCheck with IterableDiffers to listen changes without the need to create a new reference of data. Setter based method is faster however
both methods can be used depending on your preference.
</p>

<h3>Facets</h3>
<p>Header and Footer are the two sections aka facets that are capable of displaying custom content.</p>
Expand Down Expand Up @@ -259,6 +264,18 @@ <h3>Properties</h3>
<td>null</td>
<td>Style class of the component.</td>
</tr>
<tr>
<td>trackBy</td>
<td>Function</td>
<td>null</td>
<td>Function to optimize the dom operations by delegating to ngForTrackBy, default algoritm checks for object identity.</td>
</tr>
<tr>
<td>immutable</td>
<td>boolean</td>
<td>true</td>
<td>Defines how the data should be manipulated.</td>
</tr>
</tbody>
</table>
</div>
Expand Down

0 comments on commit 9814130

Please sign in to comment.