Skip to content

Commit 5314977

Browse files
authored
Merge pull request #3214 from IgniteUI/snovoselski/add-sample-for-stateParsed-example-for-grid-vnext
feat(grid): add sample with state persistance
2 parents 829857f + ebe97f5 commit 5314977

7 files changed

Lines changed: 303 additions & 2 deletions

File tree

live-editing/configs/GridConfigGenerator.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,17 @@ export class GridConfigGenerator implements IConfigGenerator {
14211421
})
14221422
}));
14231423

1424+
configs.push(new Config({
1425+
component: 'GridStatePersistenceSampleComponent',
1426+
additionalFiles: ['/src/app/directives/prevent-scroll.directive.ts', '/src/app/data/financialData.ts', '/src/app/services/financial.service.ts'],
1427+
appModuleConfig: new AppModuleConfig({
1428+
imports: ['GridStatePersistenceSampleComponent', 'IgxAvatarModule', 'IgxGridModule', 'IgxSwitchModule',
1429+
'IgxExcelExporterService', 'IgxCsvExporterService', 'IgxPreventDocumentScrollModule', 'FinancialDataService'],
1430+
ngDeclarations: ['GridStatePersistenceSampleComponent'],
1431+
ngImports: ['IgxPreventDocumentScrollModule', 'IgxAvatarModule', 'IgxGridModule', 'IgxSwitchModule'],
1432+
ngProviders: ['FinancialDataService']
1433+
})
1434+
}));
14241435

14251436
return configs;
14261437
}

src/app/grid/grid-routes-data.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,6 @@ export const gridsRoutesData = {
123123
'grid-cross-field-validator-service': { displayName: 'Grid Cross Field Validator Service ', parentName: 'Grid' },
124124
'grid-validation-style': { displayName: 'Grid with Validation Styles', parentName: 'Grid' },
125125
'grid-validator-service-extended': { displayName: 'Grid Validator Service Extended', parentName: 'Grid'},
126-
'grid-summary-export': { displayName: 'Grid Summary Export', parentName: 'Grid'}
126+
'grid-summary-export': { displayName: 'Grid Summary Export', parentName: 'Grid'},
127+
'grid-state-persistence': { displayName: 'Grid State Persistence', parentName: 'Grid'}
127128
};
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<div class="switches">
2+
<button igxButton="raised" (click)="restoreState()">
3+
<igx-icon class="btn-icon">restore</igx-icon>
4+
<span>Restore</span>
5+
</button>
6+
<button igxButton="raised" (click)="saveState()">
7+
<igx-icon class="btn-icon">save</igx-icon>
8+
<span>Save</span>
9+
</button>
10+
<button igxButton="raised" (click)="clearStorage()">
11+
<igx-icon class="btn-icon">delete</igx-icon>
12+
<span>Clear</span>
13+
</button>
14+
15+
<button igxButton="raised" (click)="grid.sortStrategy = customStrategy">
16+
<igx-icon class="btn-icon">Set Strategy</igx-icon>
17+
<span>Set NooP custom Sort Strategy</span>
18+
</button>
19+
</div>
20+
21+
<div class="grid__wrapper">
22+
<igx-grid [igxPreventDocumentScroll]="true" [igxGridState]="options" #grid [data]="data | async" [height]="'500px'"
23+
width="100%" [autoGenerate]='false' [allowFiltering]="true" (columnInit)="onColumnInit($event)">
24+
<igx-column [field]="'Category'" [sortable]="true" [width]="'120px'"></igx-column>
25+
<igx-column [field]="'Type'" [sortable]="true" [width]="'150px'" [filterable]='false'></igx-column>
26+
<igx-column [field]="'Open Price'" [sortable]="true" [width]="'120px'" dataType="number">
27+
</igx-column>
28+
<igx-column [field]="'Price'" [sortable]="true" [width]="'120px'" dataType="number">
29+
<ng-template igxCell let-column let-val #price>
30+
{{formatCurrency(val)}}
31+
</ng-template>
32+
</igx-column>
33+
<igx-column [field]="'Change'" [sortable]="true" [width]="'120px'" dataType="number"
34+
[headerClasses]="'headerAlignSyle'">
35+
<ng-template igxHeader>
36+
<span>Change</span>
37+
</ng-template>
38+
39+
<ng-template igxCell let-val #change>
40+
<div class="currency-badge-container">
41+
<igx-badge *ngIf="val>0" type="success" position="bottom-right" icon="arrow_upward"
42+
class="badge-left"></igx-badge>
43+
<igx-badge *ngIf="val<0" type="error" position="bottom-right" icon="arrow_downward"
44+
class="error badge-left"></igx-badge>
45+
<span class="cellAlignSyle" [class.up]="val>0" [class.down]="val<0">{{ formatNumber(val) }}</span>
46+
</div>
47+
</ng-template>
48+
</igx-column>
49+
<igx-column [field]="'Change(%)'" [sortable]="true" [width]="'130px'" dataType="number">
50+
<ng-template igxCell let-column let-val #changePercentage>
51+
{{formatNumber(val)}}
52+
</ng-template>
53+
<ng-template igxHeader>
54+
<span>Change(%)</span>
55+
</ng-template>
56+
57+
<ng-template igxCell let-val>
58+
<span class="cellAlignSyle" [class.up]="val>0" [class.down]="val<0">{{ formatNumber(val) }}%</span>
59+
</ng-template>
60+
</igx-column>
61+
<igx-column [field]="'Change On Year(%)'" [sortable]="true" [width]="'150px'" dataType="number">
62+
<ng-template igxCell let-val #changeOnYear>
63+
<div class="currency-badge-container">
64+
<igx-badge *ngIf="val>0" type="success" position="bottom-right" icon="arrow_upward"
65+
class="badge-left"></igx-badge>
66+
<igx-badge *ngIf="val<0" type="error" position="bottom-right" icon="arrow_downward"
67+
class="error badge-left"></igx-badge>
68+
<span class="cellAlignSyle" [class.up]="val>0" [class.down]="val<0">{{ formatNumber(val) }}%</span>
69+
</div>
70+
</ng-template>
71+
</igx-column>
72+
<igx-column [field]="'Buy'" [sortable]="true" [width]="'130px'" dataType="number">
73+
<ng-template igxCell let-column let-val #buy>
74+
{{formatCurrency(val)}}
75+
</ng-template>
76+
</igx-column>
77+
<igx-column [field]="'Sell'" [sortable]="true" [width]="'130px'" dataType="number">
78+
<ng-template igxCell let-column let-val #sell>
79+
{{formatCurrency(val)}}
80+
</ng-template>
81+
</igx-column>
82+
<igx-column [field]="'Spread'" [sortable]="true" [width]="'130px'" dataType="number">
83+
<ng-template igxCell let-column let-val #spread>
84+
{{formatNumber(val)}}
85+
</ng-template>
86+
</igx-column>
87+
<igx-column [field]="'Volume'" [sortable]="true" [width]="'130px'" dataType="number">
88+
<ng-template igxCell let-column let-val #volume>
89+
{{formatNumber(val)}}
90+
</ng-template>
91+
</igx-column>
92+
<igx-column [field]="'High(D)'" [sortable]="true" [width]="'130px'" dataType="number"
93+
[formatter]="formatCurrency">
94+
<ng-template igxCell let-column let-val #highD>
95+
{{formatCurrency(val)}}
96+
</ng-template></igx-column>
97+
<igx-column [field]="'Low(D)'" [sortable]="true" [width]="'130px'" dataType="number">
98+
<ng-template igxCell let-column let-val #lowD>
99+
{{formatCurrency(val)}}
100+
</ng-template>
101+
</igx-column>
102+
<igx-column [field]="'High(Y)'" [sortable]="true" [width]="'130px'" dataType="number">
103+
<ng-template igxCell let-column let-val #highY>
104+
{{formatCurrency(val)}}
105+
</ng-template>
106+
</igx-column>
107+
<igx-column [field]="'Low(Y)'" [sortable]="true" [width]="'130px'" dataType="number">
108+
<ng-template igxCell let-column let-val #lowY>
109+
{{formatCurrency(val)}}
110+
</ng-template>
111+
</igx-column>
112+
<igx-column [field]="'Start(Y)'" [sortable]="true" [width]="'130px'" dataType="number">
113+
<ng-template igxCell let-column let-val #startY>
114+
{{formatCurrency(val)}}
115+
</ng-template>
116+
</igx-column>
117+
</igx-grid>
118+
<br />
119+
</div>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.cellAlignSyle {
2+
text-align: right;
3+
float:right;
4+
}
5+
.cellAlignSyle > span {
6+
float:right;
7+
}
8+
.up {
9+
color: green;
10+
}
11+
.down {
12+
color: red;
13+
}
14+
.headerAlignSyle {
15+
text-align: right !important;
16+
}
17+
18+
.grid__wrapper {
19+
margin: 0 auto;
20+
padding: 16px;
21+
}
22+
23+
.currency-badge-container {
24+
width: 80px;
25+
float: right;
26+
}
27+
28+
.badge-left {
29+
float: left;
30+
}
31+
32+
.switches {
33+
display: flex;
34+
justify-content: flex-start;
35+
align-items: center;
36+
flex: 1 0 0%;
37+
min-width: 100%;
38+
padding-right: 20px;
39+
font-size: 0.9rem;
40+
margin: 10px;
41+
42+
>button {
43+
margin-right: 10px;
44+
}
45+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}

src/app/grid/grids-routing.module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ import { GridValidatorServiceCrossFieldComponent } from './grid-validator-servic
129129
import { GridValidationStyleComponent } from './grid-validation-style/grid-validation-style.component';
130130
import { GridValidatorServiceExtendedComponent } from './grid-validator-service-extended/grid-validator-service-extended.component';
131131
import { GridSummaryExportComponent } from './grid-summary-export/grid-summary-export.component';
132+
import { GridStatePersistenceSampleComponent } from './grid-state-persistence-sample/grid-state-persistance-sample.component';
133+
132134
// tslint:enable:max-line-length
133135

134136
export const gridsRoutes: Routes = [
@@ -746,6 +748,11 @@ export const gridsRoutes: Routes = [
746748
component: GridSummaryExportComponent,
747749
data: gridsRoutesData['grid-summary-export'],
748750
path: 'grid-summary-export'
751+
},
752+
{
753+
component: GridStatePersistenceSampleComponent,
754+
data: gridsRoutesData['grid-state-persistence'],
755+
path: 'grid-state-persistence'
749756
}
750757
];
751758

src/app/grid/grids.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ import { GridValidatorServiceCrossFieldComponent } from './grid-validator-servic
138138
import { GridValidationStyleComponent } from './grid-validation-style/grid-validation-style.component';
139139
import { GridValidatorServiceExtendedComponent, PhoneFormatDirective } from './grid-validator-service-extended/grid-validator-service-extended.component';
140140
import { GridSummaryExportComponent } from './grid-summary-export/grid-summary-export.component';
141+
import { GridStatePersistenceSampleComponent } from './grid-state-persistence-sample/grid-state-persistance-sample.component';
141142

142143
@NgModule({
143144
declarations: [
@@ -266,7 +267,8 @@ import { GridSummaryExportComponent } from './grid-summary-export/grid-summary-e
266267
GridValidationStyleComponent,
267268
GridValidatorServiceExtendedComponent,
268269
PhoneFormatDirective,
269-
GridSummaryExportComponent
270+
GridSummaryExportComponent,
271+
GridStatePersistenceSampleComponent
270272
],
271273
imports: [
272274
CommonModule,

0 commit comments

Comments
 (0)