-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathdrop-down-virtual.component.ts
78 lines (70 loc) · 2.96 KB
/
drop-down-virtual.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Component, OnInit, ViewChild, AfterViewInit, ChangeDetectorRef } from '@angular/core';
import { AsyncPipe } from '@angular/common';
import { Observable } from 'rxjs';
import { RemoteService } from 'src/app/shared/remote.service';
import { IForOfState, IgxButtonDirective, IgxDropDownComponent, IgxDropDownItemComponent, IgxDropDownItemNavigationDirective, IgxForOfDirective, IgxToastComponent, IgxToggleActionDirective, VerticalAlignment } from 'igniteui-angular';
interface DataItem {
name: string;
id: number;
}
@Component({
selector: 'app-drop-down-virtual',
templateUrl: './drop-down-virtual.component.html',
styleUrls: ['./drop-down-virtual.component.scss'],
providers: [RemoteService],
imports: [IgxButtonDirective, IgxToggleActionDirective, IgxDropDownItemNavigationDirective, IgxDropDownComponent, IgxForOfDirective, IgxDropDownItemComponent, IgxToastComponent, AsyncPipe]
})
export class DropDownVirtualComponent implements OnInit, AfterViewInit {
@ViewChild('loadingToast', { read: IgxToastComponent, static: true })
public loadingToast: IgxToastComponent;
@ViewChild('asyncFor', { read: IgxForOfDirective, static: true })
public remoteVirtDir: IgxForOfDirective<any>;
@ViewChild('dropdown', { read: IgxDropDownComponent, static: true })
public remoteDropDown: IgxDropDownComponent;
public itemsAsync: Observable<any[]>;
public localItems: DataItem[];
public totalItemCount = 0;
public prevRequest: any;
public startIndex = 0;
public itemHeight = 40;
public itemsMaxHeight = 320;
constructor(protected remoteService: RemoteService, protected cdr: ChangeDetectorRef) {
this.remoteService.urlBuilder = (state) => {
const chunkSize = state.chunkSize || Math.floor(this.itemsMaxHeight / this.itemHeight) + 1;
return `${this.remoteService.url}?$count=true&$skip=${state.startIndex}&$top=${chunkSize}`;
};
// eslint-disable-next-line prefer-spread
this.localItems = Array.apply(null, { length: 2000 }).map((e, i) => ({
name: `Item ${i + 1}`,
id: i
}));
}
public getIndex(index: number) {
return this.startIndex + index;
}
public ngOnInit() {
this.itemsAsync = this.remoteService.remoteData;
}
public ngAfterViewInit() {
this.remoteService.getData(this.remoteVirtDir.state, (data) => {
this.remoteVirtDir.totalItemCount = data['@odata.count'];
this.remoteVirtDir.igxForItemSize = this.itemHeight;
});
}
public dataLoading(evt: IForOfState) {
if (this.prevRequest) {
this.prevRequest.unsubscribe();
}
this.loadingToast.positionSettings.verticalDirection = VerticalAlignment.Middle;
this.loadingToast.autoHide = false;
this.loadingToast.open('Loading Remote Data...');
this.cdr.detectChanges();
this.prevRequest = this.remoteService.getData(
evt,
(data) => {
this.remoteVirtDir.totalItemCount = data['@odata.count'];
this.loadingToast.close();
this.cdr.detectChanges();
});
}
}