Skip to content

Commit a62c8ae

Browse files
committed
feat: adding cell attributes
1 parent 2246084 commit a62c8ae

File tree

4 files changed

+90
-15
lines changed

4 files changed

+90
-15
lines changed

docs/config/columns.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ new Grid({
3434

3535
<div className="full-width">
3636

37-
| Name | Description | Type | Example |
38-
|----------------------|-------------------------|-------------------|-------------------------------------------------|
39-
| id `optional` | column ID for JSON data | string or function | `phoneNumber` or `(row) => row.name.firstName` |
40-
| name | column name | string | `Name` |
41-
| width `optional` | width of the column | string | `200px` or `30%` |
42-
| sort `optional` | to enable/disable sort | boolean | `true` or `0` |
43-
| formatter `optional` | custom cell formatting | function | `(cell: TCell, row: Row<TCell>, column: TColumn) => ComponentChild;` |
37+
| Name | Description | Type | Example |
38+
|-----------------------|-------------------------|------------------------------|-------------------------------------------------|
39+
| id `optional` | column ID for JSON data | `string` or function | `phoneNumber` or `(row) => row.name.firstName` |
40+
| name | column name | `string` | `Name` |
41+
| width `optional` | width of the column | `string` | `200px` or `30%` |
42+
| sort `optional` | to enable/disable sort | `boolean` | `true` or `0` |
43+
| formatter `optional` | custom cell formatting | function | `(cell: TCell, row: Row<TCell>, column: TColumn) => ComponentChild;` |
44+
| attributes `optional` | custom cell attributes | `HTMLAttributes` or function | `(cell: TCell, row: Row<TCell>, column: TColumn) => HTMLAttributes;` |
4445

4546
</div>
4647

docs/examples/cell-attributes.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
id: cell-attributes
3+
title: Cell Attributes
4+
keywords:
5+
- javascript
6+
- table
7+
- javascript table
8+
- gridjs
9+
- grid js
10+
- cell attributes
11+
- cell attrs
12+
13+
---
14+
15+
import { LiveExample } from "../../lib/liveExample.js";
16+
17+
Add custom attributes to each cell of your table using the `attributes` config. This example
18+
adds `data-field="name"` to all cells of the "Name" column:
19+
20+
<LiveExample children={
21+
`
22+
const grid = new Grid({
23+
columns: [
24+
{
25+
name: 'Name',
26+
attributes: {
27+
'data-field': 'name'
28+
}
29+
},
30+
'Email',
31+
],
32+
data: Array(5).fill().map(x => [
33+
faker.name.findName(),
34+
faker.internet.email(),
35+
])
36+
});
37+
`
38+
} />
39+
40+
:::info
41+
`attributes` config accepts callback function as well:
42+
43+
```js
44+
{
45+
'attributes': (cell, row, column) => { ... }
46+
}
47+
```
48+
:::
49+
50+
<LiveExample children={
51+
`
52+
const grid = new Grid({
53+
columns: [
54+
{
55+
name: 'Name',
56+
attributes: (cell) => {
57+
return {
58+
'data-cell-content': cell,
59+
'onclick': () => alert(cell),
60+
'style': 'cursor: pointer'
61+
};
62+
}
63+
},
64+
'Email',
65+
],
66+
data: Array(5).fill().map(x => [
67+
faker.name.findName(),
68+
faker.internet.email(),
69+
])
70+
});
71+
`
72+
} />
73+

package-lock.json

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sidebars.js

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ module.exports = {
8080
label: 'Customizing',
8181
items: [
8282
'examples/cell-formatting',
83+
'examples/cell-attributes',
8384
'examples/row-buttons',
8485
'examples/html-cells',
8586
'examples/html-header-cells',

0 commit comments

Comments
 (0)