|
| 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 | +::: |
0 commit comments