Skip to content

Commit 9c42ec1

Browse files
MKirovaMKirova
authored andcommitted
feat(igxGrid): Persist state of pinned rows via igxGridState directive.
1 parent 53cc019 commit 9c42ec1

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

projects/igniteui-angular/src/lib/grids/state.directive.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface IGridState {
2323
cellSelection?: GridSelectionRange[];
2424
rowSelection?: any[];
2525
columnSelection?: string[];
26+
rowPinning?: any[];
2627
}
2728

2829
export interface IGridStateOptions {
@@ -35,6 +36,7 @@ export interface IGridStateOptions {
3536
cellSelection?: boolean;
3637
rowSelection?: boolean;
3738
columnSelection?: boolean;
39+
rowPinning?: boolean;
3840
}
3941

4042
export interface IColumnState {
@@ -66,6 +68,7 @@ const SORTING = 'sorting';
6668
const GROUPBY = 'groupBy';
6769
const PAGING = 'paging';
6870
const ROW_SELECTION = 'rowSelection';
71+
const ROW_PINNING = 'rowPinning';
6972
const CELL_SELECTION = 'cellSelection';
7073
const COLUMN_SELECTION = 'columnSelection';
7174

@@ -83,7 +86,8 @@ export class IgxGridStateDirective {
8386
paging: true,
8487
cellSelection: true,
8588
rowSelection: true,
86-
columnSelection: true
89+
columnSelection: true,
90+
rowPinning: true
8791
};
8892

8993
private state: IGridState;
@@ -216,6 +220,10 @@ export class IgxGridStateDirective {
216220
this.restoreRowSelection(state as any[]);
217221
break;
218222
}
223+
case ROW_PINNING: {
224+
this.restoreRowPinning(state as any[]);
225+
break;
226+
}
219227
case CELL_SELECTION: {
220228
this.restoreCellSelection(state as GridSelectionRange[]);
221229
break;
@@ -279,6 +287,10 @@ export class IgxGridStateDirective {
279287
Object.assign(state, this.getRowSelection());
280288
break;
281289
}
290+
case ROW_PINNING: {
291+
Object.assign(state, this.getRowPinning());
292+
break;
293+
}
282294
case CELL_SELECTION: {
283295
Object.assign(state, this.getCellSelection());
284296
break;
@@ -360,6 +372,11 @@ export class IgxGridStateDirective {
360372
return { rowSelection: selection };
361373
}
362374

375+
private getRowPinning(): IGridState {
376+
const pinned = this.grid.pinnedRows.map(x => x.rowID);
377+
return { rowPinning: pinned };
378+
}
379+
363380
private getColumnSelection(): IGridState {
364381
const selection = this.grid.selectedColumns().map(c => c.field);
365382
return { columnSelection: selection };
@@ -444,6 +461,10 @@ export class IgxGridStateDirective {
444461
this.grid.selectRows(state);
445462
}
446463

464+
private restoreRowPinning(state: any[]) {
465+
state.forEach(rowID => this.grid.pinRow(rowID));
466+
}
467+
447468
private restoreColumnSelection(state: string[]) {
448469
this.grid.selectColumns(state);
449470
}

src/app/grid-row-pinning/grid-row-pinning.sample.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
<button (click)="grid1.addRow({'ID': 'TEST', 'CompanyName': 'Test'})">Add Row</button>
66
<div class="sample-content">
77
<div class="sample-column">
8+
<div class="sample-buttons">
9+
<button igxButton="raised" (click)="saveGridState()">Save state</button>
10+
<button igxButton="raised" (click)="restoreGridState()">Restore state</button>
11+
</div>
812
<div class="sample-switches">
913
<igx-switch (change)='onRowChange()' style="padding-left: 10px"> Bottom Row Pinning toggle</igx-switch>
1014
<igx-switch (change)='onChange()' style="padding-left: 10px"> Right Column Pinning toggle</igx-switch>
1115
</div>
12-
<igx-grid [allowFiltering]='true' [primaryKey]='"ID"' [pinning]="pinningConfig" [showToolbar]='true' [columnPinning]='true' #grid1 [data]="data" [width]="'800px'" [height]="'500px'" [rowSelectable]="false">
16+
<igx-grid [igxGridState]="options" [allowFiltering]='true' [primaryKey]='"ID"' [pinning]="pinningConfig" [showToolbar]='true' [columnPinning]='true' #grid1 [data]="data" [width]="'800px'" [height]="'500px'" [rowSelectable]="false">
1317
<igx-column width='70px' [filterable]='false'>
1418
<ng-template igxCell let-cell="cell" let-val>
1519
<igx-icon class="pin-icon" (mousedown)="togglePining(cell.row, $event)">

src/app/grid-row-pinning/grid-row-pinning.sample.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, OnInit, ViewChild } from '@angular/core';
2-
import { IgxGridComponent, ColumnPinningPosition, RowPinningPosition, IgxGridRowComponent, IgxTransactionService, IgxGridTransaction } from 'igniteui-angular';
2+
import { IgxGridComponent, ColumnPinningPosition, RowPinningPosition, IgxGridRowComponent, IgxTransactionService, IgxGridTransaction, IgxGridStateDirective } from 'igniteui-angular';
33
import { IPinningConfig } from 'projects/igniteui-angular/src/lib/grids/common/grid.interface';
44

55
@Component({
@@ -12,9 +12,23 @@ import { IPinningConfig } from 'projects/igniteui-angular/src/lib/grids/common/g
1212
export class GridRowPinningSampleComponent implements OnInit {
1313
public pinningConfig: IPinningConfig = { columns: ColumnPinningPosition.Start };
1414

15+
public options = {
16+
cellSelection: true,
17+
rowSelection: true,
18+
filtering: true,
19+
advancedFiltering: true,
20+
paging: true,
21+
sorting: true,
22+
groupBy: true,
23+
columns: false,
24+
rowPinning: true
25+
};
26+
1527
@ViewChild('grid1', { static: true })
1628
grid1: IgxGridComponent;
1729

30+
@ViewChild(IgxGridStateDirective, { static: true }) public state: IgxGridStateDirective;
31+
1832
onRowChange() {
1933
if (this.pinningConfig.rows === RowPinningPosition.Bottom) {
2034
this.pinningConfig = { columns: this.pinningConfig.columns, rows: RowPinningPosition.Top };
@@ -97,4 +111,14 @@ export class GridRowPinningSampleComponent implements OnInit {
97111
}
98112
}
99113

114+
public saveGridState() {
115+
const state = this.state.getState() as string;
116+
window.localStorage.setItem("grid1-state", state);
117+
}
118+
119+
public restoreGridState() {
120+
const state = window.localStorage.getItem("grid1-state");
121+
this.state.setState(state);
122+
}
123+
100124
}

0 commit comments

Comments
 (0)