Skip to content

Commit

Permalink
tree virtual scroll added
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmetcetin01140 committed Feb 26, 2024
1 parent fc8747b commit be077eb
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/app/showcase/doc/tree/treedoc.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import { MultipleDoc } from './multipledoc';
import { SingleDoc } from './singledoc';
import { StyleDoc } from './styledoc';
import { TemplateDoc } from './templatedoc';
import { VirtualScrollDoc } from './virtualscrolldoc';
import { LazyVirtualScrollDoc } from './virtualscrolllazydoc';

@NgModule({
imports: [CommonModule, AppCodeModule, AppDocModule, TreeModule, ButtonModule, InputSwitchModule, FormsModule, ToastModule, ContextMenuModule],
exports: [AppDocModule],
declarations: [ImportDoc, BasicDoc, ControlledDoc, SingleDoc, MultipleDoc, CheckboxDoc, EventDoc, LazyDoc, TemplateDoc, DragDropDoc, ContextMenuDoc, FilterDoc, StyleDoc, AccessibilityDoc]
declarations: [ImportDoc, BasicDoc, ControlledDoc, SingleDoc, MultipleDoc, CheckboxDoc, EventDoc, LazyDoc, VirtualScrollDoc, LazyVirtualScrollDoc, TemplateDoc, DragDropDoc, ContextMenuDoc, FilterDoc, StyleDoc, AccessibilityDoc]
})
export class TreeDocModule {}
80 changes: 80 additions & 0 deletions src/app/showcase/doc/tree/virtualscrolldoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/api';
import { Code } from '../../domain/code';
import { NodeService } from '../../service/nodeservice';

@Component({
selector: 'virtual-scroll-doc',
template: `
<app-docsectiontext>
<p>VirtualScroller is a performance-approach to handle huge data efficiently. Setting <i>virtualScroll</i> property as true and providing a <i>virtualScrollItemSize</i> in pixels would be enough to enable this functionality.</p>
</app-docsectiontext>
<div class="card flex justify-content-center">
<p-tree class="w-full md:w-30rem" scrollHeight="250px" [virtualScroll]="true" [virtualScrollItemSize]="46" [value]="files"></p-tree>
</div>
<app-code [code]="code" selector="tree-virtual-scroll-demo"></app-code>
`
})
export class VirtualScrollDoc implements OnInit {
loading: boolean = false;

files!: TreeNode[];

constructor(private nodeService: NodeService, private cd: ChangeDetectorRef) {}

ngOnInit() {
this.nodeService.getFiles().then((data) => {
this.files = this.duplicateData(data, 50);
this.cd.markForCheck();
});
}

duplicateData(data: TreeNode[], count: number): TreeNode[] {
let duplicatedData: TreeNode[] = [];
for (let i = 0; i < count; i++) {
duplicatedData = [...duplicatedData, ...data.map((item) => ({ ...item }))];
}
return duplicatedData;
}

code: Code = {
basic: `<p-tree class="w-full md:w-30rem" scrollHeight="250px" [virtualScroll]="true" [virtualScrollItemSize]="46" [value]="files"></p-tree>`,

html: `
<div class="card flex justify-content-center">
<p-tree class="w-full md:w-30rem" scrollHeight="250px" [virtualScroll]="true" [virtualScrollItemSize]="46" [value]="files"></p-tree>
</div>`,

typescript: `
import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/api';
@Component({
selector: 'tree-template-demo',
templateUrl: './tree-template-demo.html'
})
export class TreeTemplateDemo implements OnInit {
loading: boolean = false;
files!: TreeNode[];
constructor(private nodeService: NodeService, private cd: ChangeDetectorRef) {}
ngOnInit() {
this.nodeService.getFiles().then((data) => {
this.files = this.duplicateData(data, 50);
this.cd.markForCheck();
});
}
duplicateData(data: TreeNode[], count: number): TreeNode[] {
let duplicatedData: TreeNode[] = [];
for (let i = 0; i < count; i++) {
duplicatedData = [...duplicatedData, ...data.map((item) => ({ ...item }))];
}
return duplicatedData;
}
}`
};
}
114 changes: 114 additions & 0 deletions src/app/showcase/doc/tree/virtualscrolllazydoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/api';
import { Code } from '../../domain/code';
import { NodeService } from '../../service/nodeservice';

@Component({
selector: 'lazy-virtual-scroll-doc',
template: `
<app-docsectiontext>
<p>VirtualScroller is a performance-approach to handle huge data efficiently. Setting <i>virtualScroll</i> property as true and providing a <i>virtualScrollItemSize</i> in pixels would be enough to enable this functionality.</p>
</app-docsectiontext>
<div class="card flex justify-content-center">
<p-tree class="w-full md:w-30rem" scrollHeight="250px" [virtualScroll]="true" [lazy]="true" [virtualScrollItemSize]="46" [value]="files" (onNodeExpand)="nodeExpand($event)" [loading]="loading"></p-tree>
</div>
<app-code [code]="code" selector="tree-virtual-scroll-lazy-demo"></app-code>
`
})
export class LazyVirtualScrollDoc implements OnInit {
loading: boolean = false;

files!: TreeNode[];

virtualFiles!: TreeNode[];

constructor(private nodeService: NodeService, private cd: ChangeDetectorRef) {}

ngOnInit() {
this.loading = true;
setTimeout(() => {
this.nodeService.getLazyFiles().then((files) => (this.files = this.duplicateData(files, 50)));
this.loading = false;
this.cd.markForCheck();
}, 1000);
}

duplicateData(data: TreeNode[], count: number): TreeNode[] {
let duplicatedData: TreeNode[] = [];
for (let i = 0; i < count; i++) {
duplicatedData = [...duplicatedData, ...data.map((item) => ({ ...item }))];
}
return duplicatedData;
}

nodeExpand(event: any) {
if (event.node) {
this.loading = true;
setTimeout(() => {
this.nodeService.getLazyFiles().then((nodes) => {
event.node.children = nodes;
this.files = [...this.files, event.node.children];
});
this.loading = false;
this.cd.markForCheck();
}, 200);
}
}

code: Code = {
basic: `<p-tree class="w-full md:w-30rem" scrollHeight="250px" [virtualScroll]="true" [lazy]="true" [virtualScrollItemSize]="46" [value]="files" (onNodeExpand)="nodeExpand($event)" [loading]="loading"></p-tree>`,

html: `<div class="card flex justify-content-center">
<p-tree class="w-full md:w-30rem" scrollHeight="250px" [virtualScroll]="true" [lazy]="true" [virtualScrollItemSize]="46" [value]="files" (onNodeExpand)="nodeExpand($event)" [loading]="loading"></p-tree>
</div>`,

typescript: `
import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/api';
@Component({
selector: 'tree-template-demo',
templateUrl: './tree-template-demo.html'
})
export class TreeTemplateDemo implements OnInit {
loading: boolean = false;
files!: TreeNode[];
virtualFiles!: TreeNode[];
constructor(private nodeService: NodeService, private cd: ChangeDetectorRef) {}
ngOnInit() {
this.loading = true;
setTimeout(() => {
this.nodeService.getLazyFiles().then((files) => (this.files = this.duplicateData(files, 50)));
this.loading = false;
this.cd.markForCheck();
}, 1000);
}
duplicateData(data: TreeNode[], count: number): TreeNode[] {
let duplicatedData: TreeNode[] = [];
for (let i = 0; i < count; i++) {
duplicatedData = [...duplicatedData, ...data.map((item) => ({ ...item }))];
}
return duplicatedData;
}
nodeExpand(event: any) {
if (event.node) {
this.loading = true;
setTimeout(() => {
this.nodeService.getLazyFiles().then((nodes) => {
event.node.children = nodes;
this.files = [...this.files, event.node.children];
});
this.loading = false;
this.cd.markForCheck();
}, 200);
}
}
}`
};
}
18 changes: 18 additions & 0 deletions src/app/showcase/pages/tree/treedemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { ContextMenuDoc } from '../../doc/tree/contextmenudoc';
import { FilterDoc } from '../../doc/tree/filterdoc';
import { StyleDoc } from '../../doc/tree/styledoc';
import { AccessibilityDoc } from '../../doc/tree/accessibilitydoc';
import { VirtualScrollDoc } from '../../doc/tree/virtualscrolldoc';
import { LazyVirtualScrollDoc } from '../../doc/tree/virtualscrolllazydoc';

@Component({
templateUrl: './treedemo.html'
Expand Down Expand Up @@ -65,6 +67,22 @@ export class TreeDemo {
label: 'Lazy',
component: LazyDoc
},
{
id: 'virtualscroll',
label: 'Virtual Scroll',
children:[
{
id: 'preload',
label: 'Preload',
component: VirtualScrollDoc
},
{
id: 'lazyvirtualscroll',
label: 'Lazy',
component: LazyVirtualScrollDoc
},
]
},
{
id: 'template',
label: 'Template',
Expand Down

0 comments on commit be077eb

Please sign in to comment.