-
-
Notifications
You must be signed in to change notification settings - Fork 120
Grid State & Preset
Look at your developer console before leaving the page
The Grid State
are what we defined as the currently used Columns
/ Filters
/ Sorters
/ Pagination
of the actual grid (pagination is only returned when used with Backend Service API). The columns also include their (size, position order & visibility (show/hidden)).
Presets can be used to preset a grid with certain Columns
/ Filters
/ Sorters
/ Pagination
. When we say Columns
, we actually mean their size, order position and visibility (shown/hidden) in the grid.
So basically, the idea is to save the Grid State
in Local Storage (or DB) before the grid gets destroyed and once we come back to that same page we can preset the grid with the exact same state as it was before leaving the page (just like if we were doing a forward/back button with browser history).
Note
It currently does not hold the columns
(order position with show/hide flags), but there are a plan to also add them in the future. If you wish to help and/or contribute, please reach out by opening a new issue.
You can get the Grid State
at any point in time. However if you wish to save the grid state before leaving the page and store that in Local Storage, then the best way is to use the onBeforeGridDestroy
Event Emitter.
<angular-slickgrid
gridId="grid2"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onAngularGridCreated)="angularGridReady($event)"
(onBeforeGridDestroy)="saveCurrentGridState($event)">
</angular-slickgrid>
import { GridState, GridStateService } from 'angular-slickgrid';
@Component({
templateUrl: './grid-demo.component.html'
})
@Injectable()
export class GridDemoComponent {
constructor() { }
angularGridReady(angularGrid: any) {
this.angularGrid = angularGrid;
}
// you can save it to Local Storage of DB in this call
saveCurrentGridState(grid) {
const gridState: GridState = this.angularGrid.gridStateService.getCurrentGridState();
console.log('Leaving page with current grid state', gridState);
}
}
What happens when we use the grid presets
and a Filter Default SearchTerm? In this case, the presets
will win over filter searchTerm(s). The cascading order of priorities is the following
- Do we have any
presets
? Yes use them, else go to step 2 - Do we have any
Filter searchTerm(s)
? Yes use them, else go to step 3 - No
presets
and nosearchTerm(s)
, load grid with default grid & column definitions
The current structure of a Grid Presets is the following
export interface CurrentFilter {
columnId: string;
operator?: OperatorType | OperatorString;
searchTerm?: SearchTerm;
searchTerms?: SearchTerm[];
}
export interface CurrentSorter {
columnId: string;
direction: SortDirection | SortDirectionString;
}
export interface GridState {
filters?: CurrentFilter[];
sorters?: CurrentSorter[];
pagination?: {
pageNumber: number;
pageSize: number;
};
}
For example, we can set presets
on a grid like so:
Component
import { GridState} from 'angular-slickgrid';
@Component({
templateUrl: './grid-demo.component.html'
})
@Injectable()
export class GridDemoComponent {
constructor() { }
ngOnInit(): void {
this.columnDefinitions = [
{ id: 'name', name: 'Name', field: 'name', filterable: true, sortable: true, sortable: true },
{ id: 'duration', name: 'Duration', field: 'duration', filterable: true, sortable: true },
{ id: 'complete', name: '% Complete', field: 'percentComplete', filterable: true, sortable: true },
];
this.gridOptions = {
enableFiltering: true,
// use columnDef searchTerms OR use presets as shown below
presets: {
filters: [
{ columnId: 'duration', searchTerms: [2, 22, 44] },
{ columnId: 'complete', searchTerm: '>5' }
],
sorters: [
{ columnId: 'duration', direction: 'DESC' },
{ columnId: 'complete', direction: 'ASC' }
],
// with Backend Service ONLY, you can also add Pagination info
pagination: { pageNumber: 2, pageSize: 20 }
}
};
}
There are 2 ways of subscribing to GridState Service event changed.
- Through
(onGridStateServiceChanged)
Event Emitter (recommended) - Through
onGridStateChanged
Observable on the GridState Service.
Examples
<angular-slickgrid gridId="grid1"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onGridStateServiceChanged)="gridStateChanged($event)">
</angular-slickgrid>
import { GridStateChange } from 'angular-slickgrid';
export class ExampleComponent implements OnInit {
gridStateChanged(gridState: GridStateChange) {
console.log('Grid State changed:: ', gridState);
}
}
<angular-slickgrid
gridId="grid2"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onAngularGridCreated)="angularGridReady($event)"
(onBeforeGridDestroy)="saveCurrentGridState($event)">
</angular-slickgrid>
import { GridStateChange} from 'angular-slickgrid';
export class ExampleComponent implements OnInit, OnDestroy {
gridStateSub: Subscription;
angularGridReady(angularGrid: any) {
this.angularGrid = angularGrid;
this.gridStateSub = this.angularGrid.gridStateService.onGridStateChanged.subscribe((data: GridStateChange) => console.log(data));
}
ngOnDestroy() {
this.gridStateSub.unsubscribe();
}
}
Contents
- Angular-Slickgrid Wiki
- Installation
- Styling
- Interfaces/Models
- Testing Patterns
- Column Functionalities
- Global Grid Options
- Localization
- Events
- Grid Functionalities
- Auto-Resize / Resizer Service
- Resize by Cell Content
- Composite Editor
- Context Menu
- Custom Tooltip
- Add/Delete/Update or Highlight item
- Dynamically Change Row CSS Classes
- Excel Copy Buffer
- Export to Excel
- Export to File (CSV/Txt)
- Grid State & Presets
- Grouping & Aggregators
- Row Detail
- SlickGrid Controls
- SlickGrid Plugins
- Pinning (frozen) of Columns/Rows
- Tree Data Grid
- SlickGrid & DataView objects
- Addons (controls/plugins)
- Backend Services