Skip to content

Commit d288601

Browse files
committed
feat: adding server-side plugins
1 parent dd66f9b commit d288601

5 files changed

+161
-1
lines changed
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
id: server-side-pagination
3+
title: Server Side Pagination
4+
---
5+
6+
import { Grid, html } from "gridjs";
7+
import CodeBlock from "@theme/CodeBlock"
8+
import { useEffect, useRef } from "react";
9+
10+
<CodeBlock children={
11+
`
12+
const grid = new Grid({
13+
columns: ['Pokemon', 'URL'],
14+
pagination: {
15+
limit: 5,
16+
server: {
17+
url: (prev, page, limit) => \`\${prev}?limit=\${limit}&offset=\${page * limit}\`
18+
}
19+
},
20+
server: {
21+
url: 'https://pokeapi.co/api/v2/pokemon',
22+
then: data => data.results.map(pokemon => [
23+
pokemon.name, html(\`<a href='\${pokemon.url}'>Link to \${pokemon.name}</a>\`)
24+
]),
25+
total: data => data.count
26+
}
27+
});
28+
`
29+
}
30+
transformCode={(code) =>
31+
`
32+
function () {
33+
${code}
34+
35+
const wrapperRef = useRef(null);
36+
37+
useEffect(() => {
38+
grid.render(wrapperRef.current);
39+
});
40+
41+
return (
42+
<div ref={wrapperRef} />
43+
);
44+
}
45+
`
46+
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef, html }} />

docs/examples/server-side-search.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
id: server-side-search
3+
title: Server Side Search
4+
---
5+
6+
import { Grid } from "gridjs";
7+
import CodeBlock from "@theme/CodeBlock"
8+
import { useEffect, useRef } from "react";
9+
10+
<CodeBlock children={
11+
`
12+
const grid = new Grid({
13+
search: {
14+
server: {
15+
url: (prev, keyword) => \`\${prev}?search=\${keyword}\`
16+
}
17+
},
18+
columns: ['Title', 'Director', 'Producer'],
19+
server: {
20+
url: 'https://swapi.dev/api/films/',
21+
then: data => data.results.map(movie => [movie.title, movie.director, movie.producer])
22+
}
23+
});
24+
`
25+
}
26+
transformCode={(code) =>
27+
`
28+
function () {
29+
${code}
30+
31+
const wrapperRef = useRef(null);
32+
33+
useEffect(() => {
34+
grid.render(wrapperRef.current);
35+
});
36+
37+
return (
38+
<div ref={wrapperRef} />
39+
);
40+
}
41+
`
42+
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef }} />

docs/examples/server-side-sort.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
id: server-side-sort
3+
title: Server Side Sorting
4+
---
5+
6+
import { Grid, html } from "gridjs";
7+
import CodeBlock from "@theme/CodeBlock"
8+
import { useEffect, useRef } from "react";
9+
10+
Server side search:
11+
12+
<CodeBlock children={
13+
`
14+
const grid = new Grid({
15+
sort: {
16+
multiColumn: false,
17+
server: {
18+
url: (prev, columns) => {
19+
if (!columns.length) return prev;
20+
21+
const col = columns[0];
22+
const dir = col.direction === 1 ? 'asc' : 'desc';
23+
let colName = ['name', 'rarity'][col.index];
24+
25+
return \`\${prev}&order=\${colName}&dir=\${dir}\`;
26+
}
27+
}
28+
},
29+
columns: [
30+
'Name',
31+
'Rarity',
32+
{
33+
name: 'Image',
34+
width: '50px',
35+
sort: false,
36+
formatter: (img) => html(\`<center><img src='\${img}'/></center>\`)
37+
}
38+
],
39+
server: {
40+
url: 'https://api.scryfall.com/cards/search?q=Inspiring',
41+
then: data => data.data.map(card => [card.name, card.rarity, card.image_uris.small]),
42+
total: data => data.total_cards
43+
}
44+
});
45+
`
46+
}
47+
transformCode={(code) =>
48+
`
49+
function () {
50+
${code}
51+
52+
const wrapperRef = useRef(null);
53+
54+
useEffect(() => {
55+
grid.render(wrapperRef.current);
56+
});
57+
58+
return (
59+
<div ref={wrapperRef} />
60+
);
61+
}
62+
`
63+
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef, html }} />

docs/examples/server.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: server
3-
title: Server Side
3+
title: Import server-side data
44
---
55

66
import { Grid } from "gridjs";

sidebars.js

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ module.exports = {
2727
'examples/server',
2828
'examples/from',
2929
]
30+
}, {
31+
type: 'category',
32+
label: 'Server-side',
33+
items: [
34+
'examples/server',
35+
'examples/server-side-search',
36+
'examples/server-side-pagination',
37+
'examples/server-side-sort',
38+
]
3039
}, {
3140
type: 'category',
3241
label: 'Advanced',

0 commit comments

Comments
 (0)