Skip to content

Commit 014c536

Browse files
committed
feat: adding more selection examples
1 parent 050906e commit 014c536

File tree

5 files changed

+205
-38
lines changed

5 files changed

+205
-38
lines changed

docs/examples/row-selection.md

+99-36
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,15 @@ keywords:
1616
import { LiveExample } from "../../lib/liveExample.js";
1717
import { RowSelection } from "gridjs-selection";
1818

19-
## Install
20-
21-
Install the `gridjs-selection` package using yarn/npm:
22-
23-
```bash
24-
npm install gridjs-selection --save
25-
```
26-
27-
or use a CDN:
28-
29-
```html
30-
<script src="https://unpkg.com/gridjs-selection/dist/gridjs-selection.production.min.js"></script>
31-
```
32-
33-
then import the `RowSelection` plugin:
34-
35-
```js
36-
import { RowSelection } from "gridjs-selection";
37-
```
38-
39-
In UMD, `gridjs-selection` plugin globally exposed `gridjs.selection`, e.g:
40-
41-
```js
42-
gridjs.selection.RowSelection
43-
```
19+
:::tip
20+
Install the `gridjs-selection` plugin if you haven't already.
21+
Follow the [installation manual](./examples/selection.md).
22+
:::
4423

4524
## Example
4625

47-
Add a new column to your table definition and install the plugin:
26+
Add a new column to your table definition and install the plugin. You need to define an `id` function for your selection
27+
plugin. The `id` function must return a unique `string` for each row and is called to determine the identifier for each row.
4828

4929
<LiveExample children={
5030
`
@@ -70,6 +50,7 @@ const grid = new Grid({
7050
'Email',
7151
],
7252
sort: true,
53+
search: true,
7354
data: Array(5).fill().map(x => [
7455
faker.name.findName(),
7556
faker.internet.email(),
@@ -83,17 +64,21 @@ In the example above, the 3rd cell (which is `row.cell(2).data`) has been select
8364
the "Email" field. Note that the first cell is the checkbox column.
8465
:::
8566

86-
## Events
8767

88-
You can also subscribe to the `RowSelection` store and receive updates as soon as a row is checked or unchecked:
68+
## Selected rows
69+
70+
The selection plugin uses a Redux architecture and that means each selection instance has its own unique Store which keeps
71+
the list of selected rows.
8972

9073
<LiveExample children={
9174
`
9275
const grid = new Grid({
9376
columns: [
9477
{
95-
id: 'awesomeCheckbox',
78+
id: 'selectRow',
9679
name: 'Select',
80+
// select all rows by default!
81+
data: () => true,
9782
plugin: {
9883
component: RowSelection,
9984
props: {
@@ -113,17 +98,95 @@ const grid = new Grid({
11398
faker.internet.email(),
11499
])
115100
});
116-
101+
117102
grid.on('ready', () => {
118103
// find the plugin with the give plugin ID
119-
const checkboxPlugin = grid.config.plugin.get('awesomeCheckbox');
120-
121-
// subscribe to the store events
122-
checkboxPlugin.props.store.on('updated', function (state, prevState) {
123-
console.log('checkbox updated', state, prevState);
124-
});
104+
const checkboxPlugin = grid.config.plugin.get('selectRow');
105+
// read the selected rows from the plugin's store
106+
console.log('selected rows:', checkboxPlugin.props.store.state);
125107
})
126108
`
127109
} scope={{RowSelection}} />
128110

111+
## Row selection extension
112+
113+
Grid.js enables you to write custom plugins and extend the core functionality. In this example, we are developing a custom
114+
plugin which listens to the Selection plugin events and populates a list of selected rows.
115+
116+
117+
<LiveExample children={
118+
`
119+
class SelectionsList extends BaseComponent {
120+
constructor(props, context) {
121+
super(props, context);
122+
123+
this.state = {
124+
selectedRows: []
125+
};
126+
}
127+
128+
componentDidMount() {
129+
const grid = this.config.instance;
130+
131+
grid.on('ready', () => {
132+
// find the plugin with the give plugin ID
133+
const checkboxPlugin = this.config.plugin.get('selectRow');
134+
135+
// subscribe to the store events
136+
checkboxPlugin.props.store.on('updated', (state) => {
137+
this.setState({
138+
selectedRows: state.rowIds
139+
});
140+
});
141+
});
142+
}
143+
144+
render() {
145+
if (!this.state.selectedRows.length) {
146+
return h('b', {}, 'Select some rows...');
147+
}
148+
149+
return h(
150+
'ul',
151+
{},
152+
this.state.selectedRows.map((rowId) => h('li', {}, rowId))
153+
);
154+
}
155+
}
156+
157+
const grid = new Grid({
158+
columns: [
159+
{
160+
id: 'selectRow',
161+
name: 'Select',
162+
plugin: {
163+
component: RowSelection,
164+
props: {
165+
id: (row) => row.cell(2).data
166+
}
167+
}
168+
},
169+
{
170+
name: 'Name',
171+
formatter: (cell) => \`Name: \${cell}\`
172+
},
173+
'Email',
174+
],
175+
sort: true,
176+
data: Array(5).fill().map(x => [
177+
faker.name.findName(),
178+
faker.internet.email(),
179+
])
180+
});
181+
182+
grid.plugin.add({
183+
id: 'selectionsList',
184+
component: SelectionsList,
185+
position: PluginPosition.Footer,
186+
});
187+
`
188+
} scope={{RowSelection}} />
129189

190+
:::tip
191+
Follow the [Advanced Plugin](./plugin/advanced-plugins.md) article to learn more about writing Grid.js plugins!
192+
:::

docs/examples/selection-events.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: selection-events
3+
title: Selection events
4+
keywords:
5+
- javascript
6+
- table
7+
- javascript table
8+
- gridjs
9+
- grid js
10+
- checkbox
11+
- cell checkbox
12+
- row selection
13+
- selection events
14+
15+
---
16+
17+
import { LiveExample } from "../../lib/liveExample.js";
18+
import { RowSelection } from "gridjs-selection";
19+
20+
You can also subscribe to the `RowSelection` store and receive updates as soon as a row is checked or unchecked:
21+
22+
<LiveExample children={
23+
`
24+
const grid = new Grid({
25+
columns: [
26+
{
27+
id: 'awesomeCheckbox',
28+
name: 'Select',
29+
plugin: {
30+
component: RowSelection,
31+
props: {
32+
id: (row) => row.cell(2).data
33+
}
34+
}
35+
},
36+
{
37+
name: 'Name',
38+
formatter: (cell) => \`Name: \${cell}\`
39+
},
40+
'Email',
41+
],
42+
sort: true,
43+
data: Array(5).fill().map(x => [
44+
faker.name.findName(),
45+
faker.internet.email(),
46+
])
47+
});
48+
49+
grid.on('ready', () => {
50+
// find the plugin with the give plugin ID
51+
const checkboxPlugin = grid.config.plugin.get('awesomeCheckbox');
52+
53+
// subscribe to the store events
54+
checkboxPlugin.props.store.on('updated', function (state, prevState) {
55+
console.log('checkbox updated', state, prevState);
56+
});
57+
})
58+
`
59+
} scope={{RowSelection}} />

docs/examples/selection.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
id: selection
3+
title: Selection
4+
keywords:
5+
- javascript
6+
- table
7+
- javascript table
8+
- gridjs
9+
- grid js
10+
- checkbox
11+
- cell checkbox
12+
- row selection
13+
14+
---
15+
16+
Selection plugin enables users to select rows or cells. You can then retrieve the selected rows/cells from the plugin store.
17+
18+
## Install
19+
20+
Install the `gridjs-selection` package using yarn/npm:
21+
22+
```bash
23+
npm install gridjs-selection --save
24+
```
25+
26+
or use a CDN:
27+
28+
```html
29+
<script src="https://unpkg.com/gridjs-selection/dist/gridjs-selection.production.min.js"></script>
30+
```
31+
32+
then import the `RowSelection` plugin:
33+
34+
```js
35+
import { RowSelection } from "gridjs-selection";
36+
```
37+
38+
In UMD, `gridjs-selection` plugin globally exposed `gridjs.selection`, e.g:
39+
40+
```js
41+
gridjs.selection.RowSelection
42+
```
43+

docs/plugin/advanced-plugins.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const grid = new Grid({
6060

6161
grid.plugin.add({
6262
id: 'salaryplugin',
63-
component: h(TotalSalaryPlugin, {}),
63+
component: TotalSalaryPlugin,
6464
position: PluginPosition.Footer,
6565
});
6666
`
@@ -93,7 +93,7 @@ const grid = new Grid({
9393

9494
grid.plugin.add({
9595
id: 'bonjour',
96-
component: h(HelloPlugin, {}),
96+
component: HelloPlugin,
9797
position: PluginPosition.Header,
9898
});
9999
`

sidebars.js

+2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ module.exports = {
9393
type: 'category',
9494
label: 'Selection',
9595
items: [
96+
'examples/selection',
9697
'examples/row-selection',
98+
'examples/selection-events',
9799
]
98100
}, {
99101
type: 'category',

0 commit comments

Comments
 (0)