Skip to content

Commit 050906e

Browse files
committedNov 1, 2020
fix: updating selector to data + adding the row selection example
1 parent 9443fd9 commit 050906e

File tree

7 files changed

+35069
-5161
lines changed

7 files changed

+35069
-5161
lines changed
 

‎docs/config/columns.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ new Grid({
3737
| Name | Description | Type | Example |
3838
|-----------------------|----------------------------|------------------------------|-------------------------------------------------|
3939
| id `optional` | column ID | `string` | `phoneNumber` |
40-
| selector `optional` | Cell selector fo JSON data | `function` | `(row) => row.name.firstName` |
40+
| data `optional` | Cell default data | `function` or TCell | `(row) => row.name.firstName` or `myData` |
4141
| name | column name | `string` | `Name` |
4242
| width `optional` | width of the column | `string` | `200px` or `30%` |
4343
| sort `optional` | to enable/disable sort | `boolean` | `true` or `0` |

‎docs/examples/cell-formatting.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ const grid = new Grid({
4848
'Salary 2',
4949
{
5050
name: 'Sum',
51+
data: null,
5152
formatter: (_, row) => \`$\${(row.cells[0].data + row.cells[1].data).toLocaleString()} USD\`
5253
},
5354
],
5455
data: Array(5).fill().map(x => [
5556
Math.round(Math.random() * 100000),
56-
Math.round(Math.random() * 100000),
57-
null
57+
Math.round(Math.random() * 100000)
5858
])
5959
});
6060
`

‎docs/examples/import-json.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ const grid = new Grid({
7979
`
8080
const grid = new Grid({
8181
columns: [{
82-
selector: (row) => row.name.first,
82+
data: (row) => row.name.first,
8383
name: 'First Name'
8484
}, {
85-
selector: (row) => row.name.last,
85+
data: (row) => row.name.last,
8686
name: 'Last Name'
8787
}, {
8888
id: 'email',

‎docs/examples/row-selection.md

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
id: row-selection
3+
title: Row 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+
import { LiveExample } from "../../lib/liveExample.js";
17+
import { RowSelection } from "gridjs-selection";
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+
```
44+
45+
## Example
46+
47+
Add a new column to your table definition and install the plugin:
48+
49+
<LiveExample children={
50+
`
51+
const grid = new Grid({
52+
columns: [
53+
{
54+
id: 'myCheckbox',
55+
name: 'Select',
56+
plugin: {
57+
// install the RowSelection plugin
58+
component: RowSelection,
59+
// RowSelection config
60+
props: {
61+
// use the "email" column as the row identifier
62+
id: (row) => row.cell(2).data
63+
}
64+
}
65+
},
66+
{
67+
name: 'Name',
68+
formatter: (cell) => \`Name: \${cell}\`
69+
},
70+
'Email',
71+
],
72+
sort: true,
73+
data: Array(5).fill().map(x => [
74+
faker.name.findName(),
75+
faker.internet.email(),
76+
])
77+
});
78+
`
79+
} scope={{RowSelection}} />
80+
81+
:::note
82+
In the example above, the 3rd cell (which is `row.cell(2).data`) has been selected as the row identifier which is
83+
the "Email" field. Note that the first cell is the checkbox column.
84+
:::
85+
86+
## Events
87+
88+
You can also subscribe to the `RowSelection` store and receive updates as soon as a row is checked or unchecked:
89+
90+
<LiveExample children={
91+
`
92+
const grid = new Grid({
93+
columns: [
94+
{
95+
id: 'awesomeCheckbox',
96+
name: 'Select',
97+
plugin: {
98+
component: RowSelection,
99+
props: {
100+
id: (row) => row.cell(2).data
101+
}
102+
}
103+
},
104+
{
105+
name: 'Name',
106+
formatter: (cell) => \`Name: \${cell}\`
107+
},
108+
'Email',
109+
],
110+
sort: true,
111+
data: Array(5).fill().map(x => [
112+
faker.name.findName(),
113+
faker.internet.email(),
114+
])
115+
});
116+
117+
grid.on('ready', () => {
118+
// 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+
});
125+
})
126+
`
127+
} scope={{RowSelection}} />
128+
129+

0 commit comments

Comments
 (0)
Please sign in to comment.