@@ -16,35 +16,15 @@ keywords:
16
16
import { LiveExample } from "../../lib/liveExample.js";
17
17
import { RowSelection } from "gridjs-selection";
18
18
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
+ :::
44
23
45
24
## Example
46
25
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.
48
28
49
29
<LiveExample children={
50
30
`
@@ -70,6 +50,7 @@ const grid = new Grid({
70
50
'Email',
71
51
] ,
72
52
sort: true,
53
+ search: true,
73
54
data: Array(5).fill().map(x => [
74
55
faker.name.findName(),
75
56
faker.internet.email(),
@@ -83,17 +64,21 @@ In the example above, the 3rd cell (which is `row.cell(2).data`) has been select
83
64
the "Email" field. Note that the first cell is the checkbox column.
84
65
:::
85
66
86
- ## Events
87
67
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.
89
72
90
73
<LiveExample children={
91
74
`
92
75
const grid = new Grid({
93
76
columns: [
94
77
{
95
- id: 'awesomeCheckbox ',
78
+ id: 'selectRow ',
96
79
name: 'Select',
80
+ // select all rows by default!
81
+ data: () => true,
97
82
plugin: {
98
83
component: RowSelection,
99
84
props: {
@@ -113,17 +98,95 @@ const grid = new Grid({
113
98
faker.internet.email(),
114
99
] )
115
100
});
116
-
101
+
117
102
grid.on('ready', () => {
118
103
// 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);
125
107
})
126
108
`
127
109
} scope={{RowSelection}} />
128
110
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}} />
129
189
190
+ ::: tip
191
+ Follow the [ Advanced Plugin] ( ./plugin/advanced-plugins.md ) article to learn more about writing Grid.js plugins!
192
+ :::
0 commit comments