|
| 1 | +--- |
| 2 | +id: html-header-cells |
| 3 | +title: HTML in header cells |
| 4 | +keywords: |
| 5 | + - javascript |
| 6 | + - table |
| 7 | + - javascript table |
| 8 | + - gridjs |
| 9 | + - grid js |
| 10 | + - html cell |
| 11 | + - html header |
| 12 | +--- |
| 13 | + |
| 14 | +import { Grid, html, h } from "gridjs"; |
| 15 | +import CodeBlock from "@theme/CodeBlock" |
| 16 | +import { useEffect, useRef } from "react"; |
| 17 | +import * as faker from 'faker'; |
| 18 | + |
| 19 | +Import the `html` function first: |
| 20 | + |
| 21 | +```js |
| 22 | +import { Grid, html } from "gridjs"; |
| 23 | +``` |
| 24 | + |
| 25 | +Then you can use that in `columns` or `name` field of `columns`: |
| 26 | + |
| 27 | +<CodeBlock children={ |
| 28 | +` |
| 29 | +const grid = new Grid({ |
| 30 | + columns: [ |
| 31 | + { |
| 32 | + name: html('<i>Name</i>'), |
| 33 | + }, |
| 34 | + html('<div style="border: 1px solid #ccc;padding: 5px;border-radius: 5px;text-align: center;">Email</div>'), |
| 35 | + ], |
| 36 | + data: Array(5).fill().map(x => [ |
| 37 | + faker.name.findName(), |
| 38 | + faker.internet.email(), |
| 39 | + ]) |
| 40 | +}); |
| 41 | +` |
| 42 | +} |
| 43 | + transformCode={(code) => |
| 44 | +` |
| 45 | +function () { |
| 46 | + ${code} |
| 47 | + |
| 48 | + const wrapperRef = useRef(null); |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + grid.render(wrapperRef.current); |
| 52 | + }); |
| 53 | + |
| 54 | + return ( |
| 55 | + <div ref={wrapperRef} /> |
| 56 | + ); |
| 57 | +} |
| 58 | +` |
| 59 | +} live={true} scope={{ Grid, CodeBlock, useEffect, useRef, faker, html }} /> |
| 60 | + |
| 61 | +<br/> |
| 62 | + |
| 63 | +You can also create a virtual DOM and attach it to header cells: |
| 64 | + |
| 65 | +```js |
| 66 | +import { Grid, h } from "gridjs"; |
| 67 | +``` |
| 68 | + |
| 69 | +<CodeBlock children={ |
| 70 | +` |
| 71 | +const grid = new Grid({ |
| 72 | + columns: [ |
| 73 | + h('b', {}, 'Name'), |
| 74 | + h('div', { |
| 75 | + style: { |
| 76 | + border: '1px solid #ccc', |
| 77 | + padding: '5px', |
| 78 | + 'border-radius': '5px', |
| 79 | + 'text-align': 'center', |
| 80 | + } |
| 81 | + }, 'Email'), |
| 82 | + ], |
| 83 | + data: Array(5).fill().map(x => [ |
| 84 | + faker.name.findName(), |
| 85 | + faker.internet.email(), |
| 86 | + ]), |
| 87 | + search: true |
| 88 | +}); |
| 89 | +` |
| 90 | +} |
| 91 | + transformCode={(code) => |
| 92 | +` |
| 93 | +function () { |
| 94 | + ${code} |
| 95 | + |
| 96 | + const wrapperRef = useRef(null); |
| 97 | + |
| 98 | + useEffect(() => { |
| 99 | + grid.render(wrapperRef.current); |
| 100 | + }); |
| 101 | + |
| 102 | + return ( |
| 103 | + <div ref={wrapperRef} /> |
| 104 | + ); |
| 105 | +} |
| 106 | +` |
| 107 | +} live={true} scope={{ Grid, CodeBlock, useEffect, useRef, faker, h }} /> |
0 commit comments