1 |
|
2 | # Providing Grid Data
|
3 |
|
4 | ## Data can be provided to a grid in two ways.
|
5 |
|
6 | 1. As a Local Data Source (in memory)
|
7 | 2. As a Remote Data Source, via AJAX (by providing a route, or a function which returns a `Promise`)
|
8 |
|
9 | ### Local Data
|
10 |
|
11 | #### If data is provided as a local variable it should be described as an array of objects, and declared using `data = [];`.
|
12 |
|
13 | ```javascript
|
14 | const data = [
|
15 | {
|
16 | name: 'Michael Jordan',
|
17 | position: 'Shooting Guard'
|
18 | },
|
19 | {
|
20 | name: 'Charles Barkley',
|
21 | position: 'Power Forward'
|
22 | }, ...
|
23 | ];
|
24 |
|
25 | const grid = <Grid data={ data } />
|
26 | ```
|
27 |
|
28 | ### Remote Data, using a Remote Resource
|
29 |
|
30 | #### If data is provided as a request route, it should be described as a string and declared using `dataSource = path/to/data/source;`.
|
31 |
|
32 | ```javascript
|
33 | const dataSource = 'route/to/your/data/source';
|
34 | const grid = <Grid dataSource={ dataSource } />
|
35 | ```
|
36 |
|
37 | ### Remote Data, using a function
|
38 |
|
39 | #### If data is provided as a function, it must return a `Promise`
|
40 |
|
41 | ```javascript
|
42 |
|
43 | export const dataSource = function getData({
|
44 | pageIndex, pageSize
|
45 | }) {
|
46 | return new Promise((resolve) => {
|
47 | const request = new XMLHttpRequest();
|
48 |
|
49 | const config = {
|
50 | method: 'GET',
|
51 | route: 'http://react-redux-grid.herokuapp.com/getfakeData'
|
52 | };
|
53 |
|
54 | if (pageIndex) {
|
55 | config.route = `${config.route}?pageIndex=${pageIndex}&pageSize=${pageSize}`; // eslint-disable-line max-len
|
56 | }
|
57 |
|
58 | request.open(config.method, config.route, true);
|
59 |
|
60 | request.addEventListener(
|
61 | 'load', (res) => {
|
62 | const response = JSON.parse(res.target.responseText);
|
63 |
|
64 | // if you have more data than is being shown
|
65 | // ensure you return a total, so the pager knows
|
66 | // what paging actions are possible
|
67 |
|
68 | resolve({
|
69 | data: response.data,
|
70 | total: response.total
|
71 | });
|
72 | }
|
73 | );
|
74 |
|
75 | request.send(config.data || null);
|
76 | });
|
77 | };
|
78 |
|
79 | const grid = <Grid dataSource={dataSource} />
|
80 | ```
|
81 |
|
\ | No newline at end of file |