@lucasheight/kendo-grid-state
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

npm version

Angular kendo-grid-State directive

Purpose

A helper library that implements a directive to manage grid state during session or between sessions for @Progress Kendo UI for Angular Grid.

Features

  • State persistence is managed entirely in the directive.
  • State storage can be session, local or custom. Defaults to session.
  • Persists expanded rows.
  • Persists column visibility.
  • Persists column resize.
  • Persists column reorder.
  • Persists grid sort, page, page size, group, filter etc..

How to use

Install

Install the Angular library with NPM:

    npm install --save @lucasheight/kendo-grid-state

Using the library

To enable grid state, import the module GridStateModule, add the directive to the grid, provide a unique key for this grid e.g. gridState="ANiceGrid". By default the state is stored in browser sessionStorage, this can be overiden by changing the APP_STORAGE provider (See below) :

@NgModule({
  declarations: [AppComponent, GridServiceComponent, GridDirectiveComponent],
  imports: [BrowserModule, BrowserAnimationsModule, CommonModule, HttpClientModule, GridModule, GridStateModule],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}
<kendo-grid
  gridState="ANiceGrid"
  (stateReady)="onGotState($event)"
  [data]="data$ | async"
  [pageable]="{
    buttonCount: 5,
    info: true,
    type: 'numeric',
    pageSizes: true,
    previousNex: true
  }"
  [loading]="loading"
  [pageSize]="gridState.take"
  [filter]="gridState.filter"
  [groupable]="false"
  [group]="gridState.group"
  [sortable]="true"
  [skip]="gridState.skip"
  [sort]="gridState.sort"
  [filterable]="true"
  [resizable]="true"
  [reorderable]="true"
  [columnMenu]="true"
  (dataStateChange)="onStateChange($event)"
>
  <kendo-grid-column field="ProductName"></kendo-grid-column>
  <kendo-grid-column field="SupplierID" filter="numeric"></kendo-grid-column>
  <kendo-grid-column field="QuantityPerUnit"></kendo-grid-column>
</kendo-grid>

In the component handle the stateReady event.

  loading: boolean = false;
  gridState: State = { skip: 0, take: 5 };
  data$: Observable<GridDataResult>;
  onGotState = (e: State): void => {
    this.onStateChange(e as DataStateChangeEvent);
  };
  public onStateChange = (e: DataStateChangeEvent): void => {
    this.loading = true;
    this.gridState = e;
    this.service.query(toODataString(e));
  };

Changing Gridstate storage provider

To change the application storage for all grids in your application, add the APP_STORAGE provider in the app module:

For example, this module sets the provider to use localStorage instead of the default sessionStorage.

@NgModule({
  declarations: [AppComponent, GridDirectiveComponent],
  imports: [BrowserModule, BrowserAnimationsModule, CommonModule, HttpClientModule, GridModule, GridStateModule],
  providers: [{ provide: APP_STORAGE, useFactory: () => localStorage }],
  bootstrap: [AppComponent],
})
export class AppModule {}

Custom storage providers

To implement your own custom storage provider, implement the Storage interface. Then add your factory to the provider. E.g:

export const CustomStorage: Storage = {
  length: undefined,
  clear(): void {
    throw new Error("Method not implemented.");
  },
  getItem(key: string): string {
    return "hello custom storage";
  },
  key(index: number): string {
    return "hello from custom storage";
  },
  removeItem(key: string): void {
    console.log("remove custom storage", key);
  },
  setItem(key: string, value: string): void {
    console.log("set custom storage", key);
  }
};
@NgModule({
  declarations: [AppComponent, GridDirectiveComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    CommonModule,
    HttpClientModule,
    GridModule,
    GridStateModule,
  ],
  providers: [{ provide: APP_STORAGE, useFactory: () => CustomStorage }],
  bootstrap: [AppComponent],
})

A demo can be found on stackblitz here.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.1.22latest

Version History

VersionDownloads (Last 7 Days)Published
1.1.22
1.0.70
1.1.10
1.0.60
1.0.40
1.0.30
1.0.20
1.0.10
0.1.120
0.1.110
0.1.100
0.1.90
0.1.80
0.1.60
0.1.50
0.1.40
0.1.30
0.1.20
0.1.10
0.0.210
0.0.200
0.0.190
0.0.180
0.0.170
0.0.160
0.0.150
0.0.140
0.0.130
0.0.120
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i @lucasheight/kendo-grid-state

Weekly Downloads

2

Version

1.1.2

License

MIT

Unpacked Size

74.1 kB

Total Files

21

Last publish

Collaborators

  • lucasheight