Virtualizing the DropDownList Data
Environment
| Product | Progress® Kendo UI® for Angular DropDowns |
Description
How can I implement remote data virtualization in a Kendo UI for Angular DropDownList?
Solution
To achieve remote data virtualization with the DropDownList component, utilize the rxjs BehaviorSubject which relies on the arguments provided by the pageChange event. The pageChange event is private and is emitted by the internal kendo-list component used in the DropDownList popup. Additionally, the described approach can also be applied to other DropDowns components like the MultiSelect.
-
Create the
stateChangeBehaviorSubject and pass the currentstateto it.tspublic state: any = { skip: 0, take: 50 }; private stateChange = new BehaviorSubject<any>(this.state); -
Use
switchMapto cancel the initialstateChangeobservable and attach a new one which sends a request to get the remote data for the currentstate. When subscribing, create adataarray which consists of the returned data and empty objects so that it simulates the complete data array.tsconstructor(service: OrdersService) { this.stateChange .pipe( switchMap(state => { if (this.data.length > 0) { this.loading = true; } this.state = state; return service.fetch(state); }) ) .subscribe((response: any) => { if (this.data.length === 0) { // even though we are getting only 50 items from the response we simulate an array that consists of all data items this.data = response.data.concat( new Array(response.total - this.state.take) ); } else { // if there is already loaded data, it is replaced with the latest received data this.data.splice(this.state.skip, this.state.take, ...response.data); } this.loading = false; }); } -
Handle the
pageChangeevent of the internalkendo-listcomponent. Consequently, pass the emitted state to thestateChangeBehaviorSubject.tspublic onOpened() { // optionsList is a reference to the internal kendo-list component used in the DropDownList popup. // Pass the current state to the stateChange BehaviorSubject on each pageChange event. this.dropdownlist.optionsList.pageChange.subscribe(state => this.stateChange.next(state) ); } -
Within the
kendoDropDownListItemTemplate, display a span with thek-skeleton,k-skeleton-text, andk-skeleton-waveclasses for the items which don't exist.html<ng-template kendoDropDownListItemTemplate let-dataItem> <div *ngIf="!dataItem; else elseBlock"> <span class="k-skeleton k-skeleton-text k-skeleton-wave"></span> </div> <ng-template #elseBlock>{{ dataItem.ShipName }}</ng-template> </ng-template>
The following example demonstrates the full implementation of the suggested approach.