|
| 1 | +import { Component, TemplateRef, ViewChild } from '@angular/core'; |
| 2 | + |
| 3 | +import { IGridStateOptions, IgxGridStateDirective, IgxGridComponent, NoopSortingStrategy, IgxColumnComponent } from 'igniteui-angular'; |
| 4 | +import { Observable } from 'rxjs'; |
| 5 | +import { take } from 'rxjs/operators'; |
| 6 | +import { FinancialDataService } from '../../services/financial.service'; |
| 7 | + |
| 8 | +@Component({ |
| 9 | + providers: [FinancialDataService], |
| 10 | + selector: 'app-grid-state-persistance-sample', |
| 11 | + styleUrls: ['./grid-state-persistance-sample.component.scss'], |
| 12 | + templateUrl: 'grid-state-persistance-sample.component.html' |
| 13 | +}) |
| 14 | + |
| 15 | +export class GridStatePersistenceSampleComponent { |
| 16 | + @ViewChild('grid', { static: true }) |
| 17 | + public grid: IgxGridComponent; |
| 18 | + @ViewChild(IgxGridStateDirective, { static: true }) |
| 19 | + public state!: IgxGridStateDirective; |
| 20 | + |
| 21 | + @ViewChild('price', { static: true }) public priceTemplate: TemplateRef<any>; |
| 22 | + @ViewChild('change', { static: true }) public changeTemplate: TemplateRef<any>; |
| 23 | + @ViewChild('buy', { static: true }) public buyTemplate: TemplateRef<any>; |
| 24 | + @ViewChild('sell', { static: true }) public sellTemplate: TemplateRef<any>; |
| 25 | + @ViewChild('spread', { static: true }) public spreadTemplate: TemplateRef<any>; |
| 26 | + @ViewChild('volume', { static: true }) public volumeTemplate: TemplateRef<any>; |
| 27 | + @ViewChild('highD', { static: true }) public highDTemplate: TemplateRef<any>; |
| 28 | + @ViewChild('lowD', { static: true }) public lowDTemplate: TemplateRef<any>; |
| 29 | + @ViewChild('highY', { static: true }) public highYTemplate: TemplateRef<any>; |
| 30 | + @ViewChild('lowY', { static: true }) public lowYTemplate: TemplateRef<any>; |
| 31 | + @ViewChild('startY', { static: true }) public startYTemplate: TemplateRef<any>; |
| 32 | + @ViewChild('changeOnYear', { static: true }) public changeOnYearTemplate: TemplateRef<any>; |
| 33 | + @ViewChild('changePercentage', { static: true }) public changePercentageTemplate: TemplateRef<any>; |
| 34 | + |
| 35 | + public data: Observable<any[]>; |
| 36 | + public customStrategy = NoopSortingStrategy.instance(); |
| 37 | + public options: IGridStateOptions = { |
| 38 | + rowSelection: true, |
| 39 | + filtering: true, |
| 40 | + sorting: true, |
| 41 | + columnSelection: true |
| 42 | + }; |
| 43 | + |
| 44 | + constructor(private localService: FinancialDataService) { |
| 45 | + this.localService.getData(100000); |
| 46 | + this.data = this.localService.records; |
| 47 | + } |
| 48 | + |
| 49 | + public formatNumber(value: number) { |
| 50 | + return value.toFixed(2); |
| 51 | + } |
| 52 | + |
| 53 | + public formatCurrency(value: number) { |
| 54 | + return '$' + value.toFixed(2); |
| 55 | + } |
| 56 | + |
| 57 | + public saveState() { |
| 58 | + const state = this.state.getState() as string; |
| 59 | + window.sessionStorage.setItem('grid-state', state); |
| 60 | + } |
| 61 | + |
| 62 | + public restoreState() { |
| 63 | + const state = window.sessionStorage.getItem('grid-state'); |
| 64 | + this.state.stateParsed.pipe(take(1)).subscribe(parsedState => { |
| 65 | + parsedState.sorting.forEach(x => x.strategy = this.customStrategy); |
| 66 | + }); |
| 67 | + this.state.setState(state); |
| 68 | + } |
| 69 | + |
| 70 | + public clearStorage() { |
| 71 | + window.sessionStorage.removeItem('grid-state'); |
| 72 | + } |
| 73 | + |
| 74 | + public onColumnInit(column: IgxColumnComponent) { |
| 75 | + if (column.field === 'Price') { |
| 76 | + column.bodyTemplate = this.priceTemplate; |
| 77 | + } |
| 78 | + else if (column.field === 'Buy') { |
| 79 | + column.bodyTemplate = this.buyTemplate; |
| 80 | + } |
| 81 | + else if (column.field === 'Sell') { |
| 82 | + column.bodyTemplate = this.sellTemplate; |
| 83 | + } |
| 84 | + else if (column.field === 'Spread') { |
| 85 | + column.bodyTemplate = this.spreadTemplate; |
| 86 | + } |
| 87 | + else if (column.field === 'Volume') { |
| 88 | + column.bodyTemplate = this.volumeTemplate; |
| 89 | + } |
| 90 | + else if (column.field === 'High(D)') { |
| 91 | + column.bodyTemplate = this.highDTemplate; |
| 92 | + } |
| 93 | + else if (column.field === 'Low(D)') { |
| 94 | + column.bodyTemplate = this.lowDTemplate; |
| 95 | + } |
| 96 | + else if (column.field === 'High(Y)') { |
| 97 | + column.bodyTemplate = this.highYTemplate; |
| 98 | + } |
| 99 | + else if (column.field === 'Low(Y)') { |
| 100 | + column.bodyTemplate = this.lowYTemplate; |
| 101 | + } |
| 102 | + else if (column.field === 'Start(Y)') { |
| 103 | + column.bodyTemplate = this.startYTemplate; |
| 104 | + } |
| 105 | + else if (column.field === 'Change On Year(%)') { |
| 106 | + column.bodyTemplate = this.changeOnYearTemplate; |
| 107 | + } |
| 108 | + else if (column.field === 'Change(%)') { |
| 109 | + column.bodyTemplate = this.changeTemplate; |
| 110 | + } |
| 111 | + else if (column.field === 'Change') { |
| 112 | + column.bodyTemplate = this.changePercentageTemplate; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments