Skip to content

Commit e0bd246

Browse files
committed
add plugins support
1 parent 21538b2 commit e0bd246

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

‎libs/gridjs-angular/src/lib/gridjs-angular.component.stories.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component } from '@angular/core';
2-
import { html } from 'gridjs';
2+
import { h, html, PluginPosition } from 'gridjs';
33
import { GridJsAngularComponent } from './gridjs-angular.component';
44
import { GridJsAngularModule } from './gridjs-angular.module';
55

@@ -77,3 +77,25 @@ export const events = () => ({
7777
},
7878
component: GridJsEventsExample,
7979
});
80+
81+
export const plugins = () => ({
82+
component: GridJsAngularComponent,
83+
props: {
84+
sort: true,
85+
columns: ['Name', 'Email', 'Phone Number'],
86+
plugins: [
87+
{
88+
id: 'myplugin',
89+
component: h(() => h('h1', {}, 'Hello world!'), {}),
90+
position: PluginPosition.Header,
91+
},
92+
],
93+
data: [
94+
['John', 'john@example.com', '(353) 01 222 3333'],
95+
['Mark', 'mark@gmail.com', '(01) 22 888 4444'],
96+
['Eoin', 'eoin@gmail.com', '0097 22 654 00033'],
97+
['Sarah', 'sarahcdd@gmail.com', '+322 876 1233'],
98+
['Afshin', 'afshin@mail.com', '(353) 22 87 8356'],
99+
],
100+
},
101+
});

‎libs/gridjs-angular/src/lib/gridjs-angular.component.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { GridEvents } from 'gridjs/dist/src/events';
1414
import type Header from 'gridjs/dist/src/header';
1515
import type { Language, Translator } from 'gridjs/dist/src/i18n/language';
1616
import type Pipeline from 'gridjs/dist/src/pipeline/pipeline';
17-
import type { PluginManager } from 'gridjs/dist/src/plugin';
17+
import type { Plugin, PluginManager } from 'gridjs/dist/src/plugin';
1818
import type { ServerStorageOptions } from 'gridjs/dist/src/storage/server';
1919
import type Storage from 'gridjs/dist/src/storage/storage';
2020
import type Tabular from 'gridjs/dist/src/tabular';
@@ -38,13 +38,12 @@ import {
3838
})
3939
export class GridJsAngularComponent
4040
implements AfterViewInit, OnChanges, OnDestroy, GridJsConfig {
41-
private nativeElement: any;
41+
private nativeElement: HTMLElement;
4242
private gridInstance: Grid;
4343
private initialized: boolean;
44-
private destroyed: boolean;
4544
private listeners: Map<string, (...args: any[]) => void> = new Map();
4645
@Input() gridConfig: Partial<UserConfig>;
47-
46+
@Input() plugins: Plugin[] = [];
4847
// TODO: auto generate Inputs/Output to easily sync with grid-js main package
4948
// props
5049
@Input() eventEmitter: GridJsEventEmitter<GridEvents>;
@@ -80,27 +79,35 @@ export class GridJsAngularComponent
8079
}
8180

8281
ngAfterViewInit(): void {
83-
this.gridInstance = new Grid(this.getFullConfig());
82+
this.gridInstance = new Grid(this.getConfig(this.gridConfig));
83+
this.registerPlugins();
8484
this.registerEvents();
8585
this.gridInstance.render(this.nativeElement);
8686
this.initialized = true;
8787
}
8888

89-
public ngOnChanges(changes: any): void {
89+
ngOnChanges(changes: any): void {
9090
if (this.initialized) {
91-
this.gridInstance.updateConfig(this.getFullConfig()).forceRender();
91+
this.updateConfig(this.gridConfig);
9292
}
9393
}
9494

95-
public ngOnDestroy(): void {
95+
ngOnDestroy(): void {
9696
if (this.initialized) {
97-
this.destroyed = true;
9897
if (this.gridInstance) {
9998
this.unregisterEvents();
100-
// this.gridInstance.destroy()
99+
this.gridInstance = null;
101100
}
102101
}
103102
}
103+
// public api to interact with grid instance
104+
getGridInstance() {
105+
return this.gridInstance;
106+
}
107+
108+
updateConfig(config: Partial<UserConfig>) {
109+
this.gridInstance.updateConfig(this.getConfig(config)).forceRender();
110+
}
104111

105112
private registerEvents() {
106113
for (const event of GRIDJS_EVENTS) {
@@ -122,13 +129,21 @@ export class GridJsAngularComponent
122129
}
123130
}
124131

125-
private getFullConfig(): UserConfig {
126-
const newConfig = { ...this.gridConfig };
132+
private registerPlugins() {
133+
for (const plugin of this.plugins) {
134+
if (!this.gridInstance.plugin.get(plugin.id)) {
135+
this.gridInstance.plugin.add(plugin);
136+
}
137+
}
138+
}
139+
private getConfig(config: Partial<UserConfig>): UserConfig {
140+
const newConfig = { ...config };
127141
for (const [key, value] of Object.entries(this)) {
128142
if (GRIDJS_PROPS.includes(key as any)) {
129143
newConfig[key] = value;
130144
}
131145
}
146+
this.gridConfig = newConfig;
132147
return newConfig;
133148
}
134149
}

0 commit comments

Comments
 (0)