|
| 1 | +import { AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core'; |
| 2 | +import { IComboSearchInputEventArgs, IForOfState, IgxSimpleComboComponent, IgxToastComponent, ISimpleComboSelectionChangingEventArgs, VerticalAlignment } from 'igniteui-angular'; |
| 3 | +import { RemoteNWindService } from '../../../services/remoteNwind.service'; |
| 4 | + |
| 5 | +@Component({ |
| 6 | + providers: [RemoteNWindService], |
| 7 | + selector: 'app-simple-combo-remote', |
| 8 | + templateUrl: './simple-combo-remote.component.html', |
| 9 | + styleUrls: ['./simple-combo-remote.component.scss'] |
| 10 | +}) |
| 11 | +export class SimpleComboRemoteComponent implements OnInit, AfterViewInit { |
| 12 | + @ViewChild('loadingToast') |
| 13 | + public loadingToast: IgxToastComponent; |
| 14 | + |
| 15 | + @ViewChild('remoteSimpleCombo', { read: IgxSimpleComboComponent, static: true }) |
| 16 | + public remoteSimpleCombo: IgxSimpleComboComponent; |
| 17 | + |
| 18 | + public prevRequest: any; |
| 19 | + public rData: any; |
| 20 | + private searchText: string = null; |
| 21 | + private defaultVirtState: IForOfState = { chunkSize: 6, startIndex: 0 }; |
| 22 | + private currentVirtState: IForOfState = { chunkSize: 6, startIndex: 0 }; |
| 23 | + private itemID = 1; |
| 24 | + private itemCount: number = 0; |
| 25 | + private hasSelection: boolean; |
| 26 | + private additionalScroll: number = 0; |
| 27 | + |
| 28 | + constructor( |
| 29 | + private remoteService: RemoteNWindService, |
| 30 | + public cdr: ChangeDetectorRef |
| 31 | + ) { } |
| 32 | + |
| 33 | + public ngOnInit() { |
| 34 | + this.rData = this.remoteService.remoteData; |
| 35 | + } |
| 36 | + |
| 37 | + public ngAfterViewInit() { |
| 38 | + const initSize = { |
| 39 | + startIndex: 0, |
| 40 | + chunkSize: Math.ceil(this.remoteSimpleCombo.itemsMaxHeight / this.remoteSimpleCombo.itemHeight) |
| 41 | + }; |
| 42 | + this.remoteService.getData(initSize, null, (data) => { |
| 43 | + this.remoteSimpleCombo.totalItemCount = data['@odata.count']; |
| 44 | + this.itemCount = this.remoteSimpleCombo.totalItemCount; |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + public dataLoading() { |
| 49 | + if (this.prevRequest) { |
| 50 | + this.prevRequest.unsubscribe(); |
| 51 | + } |
| 52 | + this.loadingToast.positionSettings.verticalDirection = VerticalAlignment.Middle; |
| 53 | + this.loadingToast.autoHide = false; |
| 54 | + this.loadingToast.open('Loading Remote Data...'); |
| 55 | + this.cdr.detectChanges(); |
| 56 | + |
| 57 | + this.prevRequest = this.remoteService.getData( |
| 58 | + this.remoteSimpleCombo.virtualizationState, |
| 59 | + this.searchText, |
| 60 | + (data) => { |
| 61 | + this.remoteSimpleCombo.totalItemCount = data['@odata.count']; |
| 62 | + this.loadingToast.close(); |
| 63 | + this.cdr.detectChanges(); |
| 64 | + } |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public onOpened() { |
| 69 | + const scroll: number = this.remoteSimpleCombo.virtualScrollContainer.getScrollForIndex(this.itemID - 1); |
| 70 | + this.remoteSimpleCombo.virtualScrollContainer.scrollPosition = scroll + this.additionalScroll; |
| 71 | + this.cdr.detectChanges(); |
| 72 | + } |
| 73 | + |
| 74 | + public onClosing() { |
| 75 | + this.searchText = ''; |
| 76 | + } |
| 77 | + |
| 78 | + public onClosed() { |
| 79 | + this.currentVirtState.startIndex = (this.itemID || 1) - 1; |
| 80 | + this.remoteService.getData( |
| 81 | + this.currentVirtState, |
| 82 | + this.searchText, |
| 83 | + (data) => { |
| 84 | + this.remoteSimpleCombo.totalItemCount = data['@odata.count']; |
| 85 | + this.cdr.detectChanges(); |
| 86 | + } |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + public handleSelectionChanging(evt: ISimpleComboSelectionChangingEventArgs) { |
| 91 | + this.hasSelection = evt.newSelection !== undefined; |
| 92 | + |
| 93 | + if (!this.hasSelection) { |
| 94 | + this.itemID = 1; |
| 95 | + this.currentVirtState = this.defaultVirtState; |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + this.currentVirtState.chunkSize = Math.ceil(this.remoteSimpleCombo.itemsMaxHeight / this.remoteSimpleCombo.itemHeight); |
| 100 | + |
| 101 | + this.itemCount === evt.newSelection ? |
| 102 | + this.additionalScroll = this.remoteSimpleCombo.itemHeight : |
| 103 | + this.additionalScroll = 0; |
| 104 | + |
| 105 | + if (this.itemCount - evt.newSelection >= this.currentVirtState.chunkSize - 1) { |
| 106 | + this.itemID = this.currentVirtState.startIndex = evt.newSelection; |
| 107 | + } else { |
| 108 | + this.itemID = this.currentVirtState.startIndex = this.itemCount - (this.currentVirtState.chunkSize - 1); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public handleSearchInputUpdate(searchData: IComboSearchInputEventArgs) { |
| 113 | + this.currentVirtState.startIndex = 0; |
| 114 | + this.currentVirtState.chunkSize = Math.ceil(this.remoteSimpleCombo.itemsMaxHeight / this.remoteSimpleCombo.itemHeight); |
| 115 | + this.searchText = searchData?.searchText || ''; |
| 116 | + this.remoteService.getData( |
| 117 | + this.searchText ? this.currentVirtState : this.defaultVirtState, |
| 118 | + this.searchText, |
| 119 | + (data) => { |
| 120 | + this.remoteSimpleCombo.totalItemCount = data['@odata.count']; |
| 121 | + } |
| 122 | + ); |
| 123 | + } |
| 124 | +} |
0 commit comments