Skip to content

Commit 32385d3

Browse files
committed
feat: xml example
1 parent 7f64f43 commit 32385d3

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

docs/examples/server-side-search.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import { Grid } from "gridjs";
1515
import CodeBlock from "@theme/CodeBlock"
1616
import { useEffect, useRef } from "react";
1717

18-
Add `server` config to your search definition to enable server-side search:
18+
Add `server` config to your search definition to enable server-side search. Note that in this example, we have to handle
19+
the HTTP status code 404 ourselves because api.scryfall.com return 404 error if there is no matching record in `?q=` query string:
1920

2021
<CodeBlock children={
2122
`
@@ -29,6 +30,13 @@ const grid = new Grid({
2930
columns: ['Name', 'Language', 'Released At', 'Artist'],
3031
server: {
3132
url: 'https://api.scryfall.com/cards',
33+
handle: (res) => {
34+
// no matching records found
35+
if (res.status === 404) return {data: []};
36+
if (res.ok) return res.json();
37+
38+
throw Error('oh no :(');
39+
},
3240
then: data => data.data.map(card => [card.name, card.lang, card.released_at, card.artist])
3341
}
3442
});

docs/examples/xml.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
id: xml
3+
title: Import XML
4+
keywords:
5+
- javascript
6+
- table
7+
- javascript table
8+
- gridjs
9+
- grid js
10+
- import
11+
- import data
12+
- import xml
13+
- xml
14+
---
15+
16+
import { Grid } from "gridjs";
17+
import CodeBlock from "@theme/CodeBlock"
18+
import { useEffect, useRef } from "react";
19+
20+
Using the `handler` callback you can parse and handle HTTP calls yourself. The dafault handler tries to cast the response
21+
to a JSON format but you can override it to parse the data in a different format.
22+
23+
In this example, we are parsing `https://gridjs.io/sitemap.xml` which is a XML formatted document:
24+
25+
<CodeBlock children={
26+
`
27+
const grid = new Grid({
28+
sort: true,
29+
search: true,
30+
pagination: true,
31+
columns: ['Location', 'Change Frequency', 'Priority'],
32+
server: {
33+
url: 'https://gridjs.io/sitemap.xml',
34+
handle: (res) => {
35+
return res.text().then(str => (new window.DOMParser()).parseFromString(str, "text/xml"));
36+
},
37+
then: data => {
38+
return Array.from(data.querySelectorAll('url'))
39+
.map(row => [
40+
row.querySelector('loc').innerHTML,
41+
row.querySelector('changefreq').innerHTML,
42+
row.querySelector('priority').innerHTML,
43+
]);
44+
}
45+
}
46+
});
47+
`
48+
}
49+
transformCode={(code) =>
50+
`
51+
function () {
52+
${code}
53+
54+
const wrapperRef = useRef(null);
55+
56+
useEffect(() => {
57+
grid.render(wrapperRef.current);
58+
});
59+
60+
return (
61+
<div ref={wrapperRef} />
62+
);
63+
}
64+
`
65+
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef }} />
66+
67+
<br/>

docs/hello-world.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ import "gridjs/dist/theme/mermaid.css";
6868

6969
### React
7070

71+
Here is an example of using Grid.js in a React app (without the `gridjs-react` wrapper)
72+
7173
```jsx
7274
import { Grid } from "gridjs";
7375
import "gridjs/dist/theme/mermaid.css";
7476

7577
function helloWorld () {
78+
const wrapperRef = useRef(null);
79+
7680
const grid = new Grid({
7781
columns: ['Name', 'Email', 'Phone Number'],
7882
data: [
@@ -82,14 +86,15 @@ function helloWorld () {
8286
});
8387

8488
useEffect(() => {
85-
grid.render(document.getElementById('wrapper'));
89+
grid.render(wrapperRef.current);
8690
});
8791

88-
return (
89-
<div id="wrapper" />
90-
);
92+
return <div ref={wrapperRef} />;
9193
}
92-
9394
```
9495

96+
:::tip
97+
Above example is just to demonstrate how to import and initiate Grid.js.
98+
Please use the `gridjs-react` component instead. See [React integration](./integrations/react.md).
99+
:::
95100

sidebars.js

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = {
4242
'examples/import-async',
4343
'examples/server',
4444
'examples/from',
45+
'examples/xml',
4546
]
4647
}, {
4748
type: 'category',

0 commit comments

Comments
 (0)