Skip to content

Commit 8a27ba0

Browse files
committed
feat: server config
1 parent d288601 commit 8a27ba0

8 files changed

+212
-5
lines changed

docs/examples/server-side-pagination.md

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { Grid, html } from "gridjs";
77
import CodeBlock from "@theme/CodeBlock"
88
import { useEffect, useRef } from "react";
99

10+
Add `server` property to the `pagination` config to enable server-side pagination. Also, make sure the `total` property
11+
is correctly defined in the main `server` config block:
12+
1013
<CodeBlock children={
1114
`
1215
const grid = new Grid({
@@ -44,3 +47,7 @@ function () {
4447
}
4548
`
4649
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef, html }} />
50+
51+
:::tip
52+
You can also send POST HTTP calls if you add `method: 'POST'` to the main `server` config.
53+
:::

docs/examples/server-side-search.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import { Grid } from "gridjs";
77
import CodeBlock from "@theme/CodeBlock"
88
import { useEffect, useRef } from "react";
99

10+
Add `server` config to your search definition to enable server-side search:
11+
1012
<CodeBlock children={
1113
`
1214
const grid = new Grid({
1315
search: {
1416
server: {
1517
url: (prev, keyword) => \`\${prev}?search=\${keyword}\`
16-
}
18+
},
19+
placeholder: 'Search in title...'
1720
},
1821
columns: ['Title', 'Director', 'Producer'],
1922
server: {

docs/examples/server-side-sort.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Grid, html } from "gridjs";
77
import CodeBlock from "@theme/CodeBlock"
88
import { useEffect, useRef } from "react";
99

10-
Server side search:
10+
Simply add `server` config to the `sort` block to enable server-side sorting:
1111

1212
<CodeBlock children={
1313
`

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Grid.js is a table plugin written in TypeScript.
1212
It is developed to be used with all popular JavaScript frameworks
1313
include **React**, **Angular.js**, **Vue** or without any frameworks!
1414

15-
- Small. Only 9kb (minified and gzipped)
15+
- Small. Only 12kb with all plugins (minified and gzipped)
1616
- Fast! Grid.js has an internal pipeline that takes care of caching and processing data
1717
- Works with all web frameworks
1818
- Supports all modern web-browsers

docs/server-side.md

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
id: server-side
3+
title: Server-side setup
4+
---
5+
6+
import { Grid } from "gridjs";
7+
import CodeBlock from "@theme/CodeBlock"
8+
import { useEffect, useRef } from "react";
9+
10+
Grid.js supports server-side storage by default which basically takes care of sending HTTP calls to a server backend, pulling and
11+
parsing the results and feeding the pipeline with the received data.
12+
13+
In addition to that, components like search, sort and pagination
14+
can be connected to a server-side backend. Simply add `server` option to the existing plugin configurations to enable server-side functionality.
15+
16+
Lets setup a Grid.js instance that pulls from a server-side API.
17+
18+
## `server` config
19+
20+
First of all, make sure you have defined the generic `server` config in your Grid.js instance:
21+
22+
```js {3-8}
23+
const grid = new Grid({
24+
columns: ['Title', 'Director', 'Producer'],
25+
server: {
26+
url: 'https://swapi.dev/api/films/',
27+
then: data => data.results.map(movie =>
28+
[movie.title, movie.director, movie.producer]
29+
)
30+
}
31+
});
32+
```
33+
34+
Here we are basically telling the instance that:
35+
36+
- Our data source is a `ServerStorage` (instead of in-memory storage).
37+
- The base URL is `https://swapi.dev/api/films/`
38+
- Once we have received the data, let's feed the table with `movie.title`, `movie.director` and `movie.producer` which is
39+
our table columns (`then` function)
40+
41+
The HTTP method is implicitly set to `GET` but we can change it to `POST` using the `method` property:
42+
43+
```js {4}
44+
const grid = new Grid({
45+
server: {
46+
method: 'POST',
47+
// ...
48+
}
49+
});
50+
```
51+
52+
At this point, we have a fully functional server-side table, lets take a look!
53+
54+
<CodeBlock children={
55+
`
56+
const grid = new Grid({
57+
columns: ['Title', 'Director', 'Producer'],
58+
server: {
59+
url: 'https://swapi.dev/api/films/',
60+
then: data => data.results.map(movie => [movie.title, movie.director, movie.producer])
61+
}
62+
});
63+
`
64+
}
65+
transformCode={(code) =>
66+
`
67+
function () {
68+
${code}
69+
70+
const wrapperRef = useRef(null);
71+
72+
useEffect(() => {
73+
grid.render(wrapperRef.current);
74+
});
75+
76+
return (
77+
<div ref={wrapperRef} />
78+
);
79+
}
80+
`
81+
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef }} />
82+
83+
<br/>
84+
85+
Awesome! Now lets take a look at the search plugin.
86+
87+
## Client-side search
88+
89+
Using Grid.js, you can pull your data from a server-side backend and let the **client-side search** handle the search functionality.
90+
Grid.js is smart enough to understand that you want to pull the data once and then run the search on existing rows:
91+
92+
<CodeBlock children={
93+
`
94+
const grid = new Grid({
95+
search: true,
96+
columns: ['Title', 'Director', 'Producer'],
97+
server: {
98+
url: 'https://swapi.dev/api/films/',
99+
then: data => data.results.map(movie => [movie.title, movie.director, movie.producer])
100+
}
101+
});
102+
`
103+
}
104+
transformCode={(code) =>
105+
`
106+
function () {
107+
${code}
108+
109+
const wrapperRef = useRef(null);
110+
111+
useEffect(() => {
112+
grid.render(wrapperRef.current);
113+
});
114+
115+
return (
116+
<div ref={wrapperRef} />
117+
);
118+
}
119+
`
120+
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef }} />
121+
122+
<br/>
123+
124+
:::tip
125+
In this example, we are pulling the data once and only during the initial load of the library.
126+
:::
127+
128+
Great! Now, lets connect the search plugin to the server.
129+
130+
## Server-side search
131+
132+
All Grid.js plugins support server-side storage. All you need to do is defining the behaviour of each component using the
133+
`url` or `body` parameters:
134+
135+
```js {3}
136+
search: {
137+
server: {
138+
url: (prev, keyword) => `${prev}?search=${keyword}`
139+
}
140+
}
141+
```
142+
143+
In this example, we are telling the search plugin to refine the base URL (defined in the main `server` section) and append
144+
the keyword to base URL. `prev` is the base URL (or the URL received from the previous step of the pipeline) and `keyword`
145+
is the actual keyword input by the user.
146+
147+
This is the final version of our config that includes the server-side search:
148+
149+
<CodeBlock children={
150+
`
151+
const grid = new Grid({
152+
search: {
153+
server: {
154+
url: (prev, keyword) => \`\${prev}?search=\${keyword}\`
155+
},
156+
placeholder: 'Search in title...'
157+
},
158+
columns: ['Title', 'Director', 'Producer'],
159+
server: {
160+
url: 'https://swapi.dev/api/films/',
161+
then: data => data.results.map(movie => [movie.title, movie.director, movie.producer])
162+
}
163+
});
164+
`
165+
}
166+
transformCode={(code) =>
167+
`
168+
function () {
169+
${code}
170+
171+
const wrapperRef = useRef(null);
172+
173+
useEffect(() => {
174+
grid.render(wrapperRef.current);
175+
});
176+
177+
return (
178+
<div ref={wrapperRef} />
179+
);
180+
}
181+
`
182+
} live={true} scope={{ Grid, CodeBlock, useEffect, useRef }} />
183+
184+
<br/>
185+
186+
:::note
187+
Check out the "Server-side" section of the examples for more details.
188+
:::

sidebars.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module.exports = {
44
Usage: [
55
'install',
66
'hello-world',
7-
'config'
7+
'config',
8+
'server-side',
89
],
910
Examples: [{
1011
type: 'category',

src/pages/docs.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import {Redirect} from '@docusaurus/router';
3+
4+
function Docs() {
5+
return <Redirect to="/docs/index" />;
6+
}
7+
8+
export default Docs;

src/pages/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ function Footer() {
407407
}
408408

409409
function Install() {
410-
const example = `new Gridjs({
410+
const example = `new Grid({
411411
columns: ['Name', 'Email'],
412412
data: [
413413
['John', '[email protected]'],

0 commit comments

Comments
 (0)