|
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import { |
| 3 | + type AngularGridInstance, |
| 4 | + Aggregators, |
| 5 | + type Column, |
| 6 | + FieldType, |
| 7 | + Formatters, |
| 8 | + type GridOption, |
| 9 | + type Grouping, |
| 10 | + type Metrics, |
| 11 | + type OnRowCountChangedEventArgs, |
| 12 | + SortComparers, |
| 13 | + SortDirectionNumber |
| 14 | +} from '../modules/angular-slickgrid'; |
| 15 | + |
| 16 | +const FETCH_SIZE = 50; |
| 17 | + |
| 18 | +@Component({ |
| 19 | + templateUrl: './grid-infinite-json.component.html' |
| 20 | +}) |
| 21 | +export class GridInfiniteJsonComponent implements OnInit { |
| 22 | + angularGrid!: AngularGridInstance; |
| 23 | + columnDefinitions!: Column[]; |
| 24 | + dataset: any[] = []; |
| 25 | + gridOptions!: GridOption; |
| 26 | + metrics!: Partial<Metrics>; |
| 27 | + scrollEndCalled = false; |
| 28 | + shouldResetOnSort = false; |
| 29 | + |
| 30 | + ngOnInit(): void { |
| 31 | + this.defineGrid(); |
| 32 | + this.dataset = this.loadData(0, FETCH_SIZE); |
| 33 | + this.metrics = { |
| 34 | + itemCount: FETCH_SIZE, |
| 35 | + totalItemCount: FETCH_SIZE, |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + angularGridReady(angularGrid: AngularGridInstance) { |
| 40 | + this.angularGrid = angularGrid; |
| 41 | + } |
| 42 | + |
| 43 | + defineGrid() { |
| 44 | + this.columnDefinitions = [ |
| 45 | + { id: 'title', name: 'Title', field: 'title', sortable: true, minWidth: 100, filterable: true }, |
| 46 | + { id: 'duration', name: 'Duration (days)', field: 'duration', sortable: true, minWidth: 100, filterable: true, type: FieldType.number }, |
| 47 | + { id: 'percentComplete', name: '% Complete', field: 'percentComplete', sortable: true, minWidth: 100, filterable: true, type: FieldType.number }, |
| 48 | + { id: 'start', name: 'Start', field: 'start', formatter: Formatters.dateIso, exportWithFormatter: true, filterable: true }, |
| 49 | + { id: 'finish', name: 'Finish', field: 'finish', formatter: Formatters.dateIso, exportWithFormatter: true, filterable: true }, |
| 50 | + { id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', sortable: true, minWidth: 100, filterable: true, formatter: Formatters.checkmarkMaterial } |
| 51 | + ]; |
| 52 | + |
| 53 | + this.gridOptions = { |
| 54 | + autoResize: { |
| 55 | + container: '#demo-container', |
| 56 | + rightPadding: 10 |
| 57 | + }, |
| 58 | + enableAutoResize: true, |
| 59 | + enableFiltering: true, |
| 60 | + enableGrouping: true, |
| 61 | + editable: false, |
| 62 | + rowHeight: 33, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + // add onScroll listener which will detect when we reach the scroll end |
| 67 | + // if so, then append items to the dataset |
| 68 | + handleOnScroll(args: any) { |
| 69 | + const viewportElm = args.grid.getViewportNode(); |
| 70 | + if ( |
| 71 | + ['mousewheel', 'scroll'].includes(args.triggeredBy || '') |
| 72 | + && !this.scrollEndCalled |
| 73 | + && viewportElm.scrollTop > 0 |
| 74 | + && Math.ceil(viewportElm.offsetHeight + args.scrollTop) >= args.scrollHeight |
| 75 | + ) { |
| 76 | + console.log('onScroll end reached, add more items'); |
| 77 | + const startIdx = this.angularGrid.dataView?.getItemCount() || 0; |
| 78 | + const newItems = this.loadData(startIdx, FETCH_SIZE); |
| 79 | + this.angularGrid.dataView?.addItems(newItems); |
| 80 | + this.scrollEndCalled = false; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // do we want to reset the dataset when Sorting? |
| 85 | + // if answering Yes then use the code below |
| 86 | + handleOnSort() { |
| 87 | + if (this.shouldResetOnSort) { |
| 88 | + const newData = this.loadData(0, FETCH_SIZE); |
| 89 | + this.angularGrid.slickGrid?.scrollTo(0); // scroll back to top to avoid unwanted onScroll end triggered |
| 90 | + this.angularGrid.dataView?.setItems(newData); |
| 91 | + this.angularGrid.dataView?.reSort(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + groupByDuration() { |
| 96 | + this.angularGrid?.dataView?.setGrouping({ |
| 97 | + getter: 'duration', |
| 98 | + formatter: (g) => `Duration: ${g.value} <span class="text-green">(${g.count} items)</span>`, |
| 99 | + comparer: (a, b) => SortComparers.numeric(a.value, b.value, SortDirectionNumber.asc), |
| 100 | + aggregators: [ |
| 101 | + new Aggregators.Avg('percentComplete'), |
| 102 | + new Aggregators.Sum('cost') |
| 103 | + ], |
| 104 | + aggregateCollapsed: false, |
| 105 | + lazyTotalsCalculation: true |
| 106 | + } as Grouping); |
| 107 | + |
| 108 | + // you need to manually add the sort icon(s) in UI |
| 109 | + this.angularGrid?.slickGrid?.setSortColumns([{ columnId: 'duration', sortAsc: true }]); |
| 110 | + this.angularGrid?.slickGrid?.invalidate(); // invalidate all rows and re-render |
| 111 | + } |
| 112 | + |
| 113 | + loadData(startIdx: number, count: number) { |
| 114 | + const tmpData: any[] = []; |
| 115 | + for (let i = startIdx; i < startIdx + count; i++) { |
| 116 | + tmpData.push(this.newItem(i)); |
| 117 | + } |
| 118 | + |
| 119 | + return tmpData; |
| 120 | + } |
| 121 | + |
| 122 | + newItem(idx: number) { |
| 123 | + const randomYear = 2000 + Math.floor(Math.random() * 10); |
| 124 | + const randomMonth = Math.floor(Math.random() * 11); |
| 125 | + const randomDay = Math.floor((Math.random() * 29)); |
| 126 | + const randomPercent = Math.round(Math.random() * 100); |
| 127 | + |
| 128 | + return { |
| 129 | + id: idx, |
| 130 | + title: 'Task ' + idx, |
| 131 | + duration: Math.round(Math.random() * 100) + '', |
| 132 | + percentComplete: randomPercent, |
| 133 | + start: new Date(randomYear, randomMonth + 1, randomDay), |
| 134 | + finish: new Date(randomYear + 1, randomMonth + 1, randomDay), |
| 135 | + effortDriven: (idx % 5 === 0) |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + onSortReset(shouldReset: boolean) { |
| 140 | + this.shouldResetOnSort = shouldReset; |
| 141 | + } |
| 142 | + |
| 143 | + clearAllFiltersAndSorts() { |
| 144 | + if (this.angularGrid?.gridService) { |
| 145 | + this.angularGrid.gridService.clearAllFiltersAndSorts(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + setFiltersDynamically() { |
| 150 | + // we can Set Filters Dynamically (or different filters) afterward through the FilterService |
| 151 | + this.angularGrid?.filterService.updateFilters([ |
| 152 | + { columnId: 'percentComplete', searchTerms: ['50'], operator: '>=' }, |
| 153 | + ]); |
| 154 | + } |
| 155 | + |
| 156 | + refreshMetrics(args: OnRowCountChangedEventArgs) { |
| 157 | + if (this.angularGrid && args?.current >= 0) { |
| 158 | + this.metrics.itemCount = this.angularGrid.dataView?.getFilteredItemCount() || 0; |
| 159 | + this.metrics.totalItemCount = args.itemCount || 0; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + setSortingDynamically() { |
| 164 | + this.angularGrid?.sortService.updateSorting([ |
| 165 | + { columnId: 'title', direction: 'DESC' }, |
| 166 | + ]); |
| 167 | + } |
| 168 | +} |
0 commit comments