|
| 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