-
Notifications
You must be signed in to change notification settings - Fork 1
/
externalData.html
583 lines (532 loc) · 32.9 KB
/
externalData.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Basic Page Needs
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<meta charset="utf-8">
<title>Griddle - React Grid Component</title>
<meta name="description" content="Griddle - React Grid component. ">
<meta name="author" content="Ryan Lanciaux">
<!-- Mobile Specific Metas
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- FONT
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<link href='//fonts.googleapis.com/css?family=Raleway:400,300,600' rel='stylesheet' type='text/css'>
<!-- CSS
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<link rel="stylesheet" href="styles/normalize.css">
<link rel="stylesheet" href="styles/skeleton.css">
<link rel="stylesheet" href="styles/site.css">
<link rel="stylesheet" href="styles/chartist.css">
<!-- Favicon
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<link rel="icon" type="image/png" href="images/favicon.png" />
<!-- Scripts
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react-dom.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/react/0.13.2/JSXTransformer.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
<script src="scripts/Griddle.js"></script>
<script src="scripts/GriddleWithCallback.js"></script>
<script src="scripts/fakeData.js"></script>
<script src="scripts/ChartistGraph.js"></script>
</head>
<body id="docs-page">
<header class="background-primary-dark border-bottom border-secondary">
<div class="container">
<div class="row">
<div class="eight columns">
<a href="index.html"><img src="images/griddle.png" alt="Griddle"/></a>
</div>
<div class="four columns right">
<iframe src="http://ghbtns.com/github-btn.html?user=GriddleGriddle&repo=Griddle&type=watch&count=true"
allowtransparency="true" frameborder="0" scrolling="0" width="90" height="20"></iframe>
<iframe src="http://ghbtns.com/github-btn.html?user=GriddleGriddle&repo=Griddle&type=fork&count=true"
allowtransparency="true" frameborder="0" scrolling="0" width="90" height="20"></iframe>
<br />
<a href="quickstart.html" class="button border-light text-light big">Documentation</a>
</div>
</div>
</div>
</header>
<div class="container page-content">
<div class="row">
<div class="two columns">
<nav class="side">
<ul>
<li><a href="quickstart.html">Quickstart</a></li>
<li><a href="styling.html">Styling</a></li>
<li><a href="subgrids.html">Subgrids</a></li>
<li><a href="customization.html">Customization</a></li>
<li><a href="externalData.html">External Data</a></li>
<li><a href="infiniteScroll.html">Infinite Scrolling</a></li>
<li><a href="properties.html">Properties</a></li>
<li class="border-top border-lighter-grey"><a href="https://reactjsnews.com/isomorphic-javascript-with-react-node/">Isomorphic Rendering <small>(at ReactJS News)</small></a>
</ul>
</nav>
</div>
<div class="ten columns docs">
<h2 id="external-data">External Data</h2>
<p>Often times it is not practical to load all of a grid's data in one request. Griddle supports loading external data through creation of a wrapper component or writing a callback function (the wrapper component is the recommended route). </p>
<h4 id="wrapper-component-recommended-">Wrapper Component (recommended)</h4>
<p>Griddle comes with a handful of properties that must be set when working with external data.</p>
<dl>
<dt>useExternal</dt>
<dd><strong>bool</strong> - Griddle will run expecting results to be coming from an external source. This affects paging, filtering, sorting, etc. (i.e. Griddle will not sort the local data set but will tell the wrapper component to get the sorted data from the source). Default: false
</dl>
<dl>
<dt>externalSetPage</dt>
<dd><strong>function</strong> - The function that will be used to set the page. Default: null</dd>
</dl>
<dl>
<dt>externalChangeSort</dt>
<dd><strong>function</strong> - The function that will be used to change the sort. Default: null</dd>
</dl>
<dl>
<dt>externalSetFilter</dt>
<dd><strong>function</strong> - The function that will be used to change the filter. Default: null</dd>
</dl>
<dl>
<dt>externalSetPageSize</dt>
<dd><strong>function</strong> - The function that will be used to change the number of results per page. Default: null</dd>
</dl>
<dl>
<dt>externalMaxPage</dt>
<dd><strong>int</strong> - The max number of pages that can be displayed for the result-set. Default: null</dd>
</dl>
<dl>
<dt>externalCurrentPage</dt>
<dd><strong>int</strong> - The page that is currently displayed from the result-set. Default: null</dd>
</dl>
<dl>
<dt>externalSortColumn</dt>
<dd><strong>string</strong> - The column name that determines the sort of the result-set Default: null</dd>
</dl>
<dl>
<dt>externalSortAscending</dt>
<dd><strong>bool</strong> - Specifies the sort direction for the current sort column. Default: true</dd>
</dl>
<hr />
<h5 id="example-wrapper-component">Example Wrapper Component</h5>
<p>Below is a skeleton wrapper component for dealing with external results in Griddle. This wrapper does not need to be used but it could serve as a decent starting point. </p>
<pre><code><span class="keyword">var</span> ExternalComponent = React.createClass({
getInitialState: <span class="keyword">function</span>(){
<span class="keyword">var</span> initial = { <span class="string">"results"</span>: [],
<span class="string">"currentPage"</span>: <span class="number">0</span>,
<span class="string">"maxPages"</span>: <span class="number">0</span>,
<span class="string">"externalResultsPerPage"</span>: <span class="number">5</span>,
<span class="string">"externalSortColumn"</span>:<span class="literal">null</span>,
<span class="string">"externalSortAscending"</span>:<span class="literal">true</span>
};
<span class="keyword">return</span> initial;
},
<span class="comment">//general lifecycle methods</span>
componentWillMount: <span class="keyword">function</span>(){
},
componentDidMount: <span class="keyword">function</span>(){
},
<span class="comment">//what page is currently viewed</span>
setPage: <span class="keyword">function</span>(index){
},
<span class="comment">//this will handle how the data is sorted</span>
sortData: <span class="keyword">function</span>(sort, sortAscending, data){
},
<span class="comment">//this changes whether data is sorted in ascending or descending order</span>
changeSort: <span class="keyword">function</span>(sort, sortAscending){
},
<span class="comment">//this method handles the filtering of the data</span>
setFilter: <span class="keyword">function</span>(filter){
},
<span class="comment">//this method handles determining the page size</span>
setPageSize: <span class="keyword">function</span>(size){
},
render: <span class="keyword">function</span>(){
<span class="keyword">return</span> <span class="xml"><span class="tag"><<span class="title">Griddle</span> <span class="attribute">useExternal</span>=<span class="value">{true}</span> <span class="attribute">externalSetPage</span>=<span class="value">{this.setPage}</span>
<span class="attribute">externalChangeSort</span>=<span class="value">{this.changeSort}</span> <span class="attribute">externalSetFilter</span>=<span class="value">{this.setFilter}</span>
<span class="attribute">externalSetPageSize</span>=<span class="value">{this.setPageSize}</span> <span class="attribute">externalMaxPage</span>=<span class="value">{this.state.maxPages}</span>
<span class="attribute">externalCurrentPage</span>=<span class="value">{this.state.currentPage}</span> <span class="attribute">results</span>=<span class="value">{this.state.results}</span>
<span class="attribute">resultsPerPage</span>=<span class="value">{this.state.externalResultsPerPage}</span>
<span class="attribute">externalSortColumn</span>=<span class="value">{this.state.externalSortColumn}</span>
<span class="attribute">externalSortAscending</span>=<span class="value">{this.state.externalSortAscending}</span>
<span class="attribute">showFilter</span>=<span class="value">{true}</span> <span class="attribute">showSettings</span>=<span class="value">{true}</span> /></span>
}
});</span></code></pre>
<hr />
<h5 id="external-api-example-">External API Example:</h5>
<p>The following example uses the <a href="http://swapi.co/">Star Wars API</a> and the <a href="https://github.com/cfjedimaster/SWAPI-Wrapper">SWAPI-Wrapper</a>. </p>
<p>First off, the skeleton wrapper was modified to include a get external data method. </p>
<pre><code class="lang-javascript">getExternalData: <span class="keyword">function</span>(page){
<span class="keyword">var</span> that = <span class="keyword">this</span>;
page = page||<span class="number">1</span>
swapiModule.getStarships(page, <span class="keyword">function</span>(data) {
that.setState({
results: data.results,
currentPage: page-<span class="number">1</span>,
maxPages: Math.round(data.count/<span class="number">10</span>)
})
});
}</code></pre>
<p>The component was then modified to use getExternalData method for changing the pages and obtaining the initial data. Notice that filtering, sorting, etc are not enabled on this example (the next example has all of these options turned on). </p>
<pre><code class="lang-javascript"><span class="keyword">var</span> ExternalSwapiComponent = React.createClass({
getInitialState: <span class="keyword">function</span>(){
<span class="keyword">var</span> initial = { <span class="string">"results"</span>: [],
<span class="string">"currentPage"</span>: <span class="number">0</span>,
<span class="string">"maxPages"</span>: <span class="number">0</span>,
<span class="string">"externalResultsPerPage"</span>: <span class="number">10</span>,
<span class="string">"externalSortColumn"</span>:<span class="literal">null</span>,
<span class="string">"externalSortAscending"</span>:<span class="literal">true</span>,
<span class="string">"results"</span>: []
};
<span class="keyword">return</span> initial;
},
componentWillMount: <span class="keyword">function</span>(){
},
componentDidMount: <span class="keyword">function</span>(){
<span class="keyword">this</span>.getExternalData();
},
getExternalData: <span class="keyword">function</span>(page){
...
},
setPage: <span class="keyword">function</span>(index){
<span class="comment">//This should interact with the data source to get the page at the given index</span>
index = index > <span class="keyword">this</span>.state.maxPages ? <span class="keyword">this</span>.state.maxPages : index < <span class="number">1</span> ? <span class="number">1</span> : index + <span class="number">1</span>;
<span class="keyword">this</span>.getExternalData(index);
},
setPageSize: <span class="keyword">function</span>(size){
},
render: <span class="keyword">function</span>(){
<span class="comment">//columns={["name", "city", "state", "country"]}</span>
<span class="keyword">return</span> <span class="xml"><span class="tag"><<span class="title">Griddle</span> <span class="attribute">useExternal</span>=<span class="value">{true}</span> <span class="attribute">externalSetPage</span>=<span class="value">{this.setPage}</span> <span class="attribute">enableSort</span>=<span class="value">{false}</span>
<span class="attribute">columns</span>=<span class="value">{["name",</span> "<span class="attribute">model</span>", "<span class="attribute">manufacturer</span>", "<span class="attribute">passengers</span>"]}
<span class="attribute">externalSetPageSize</span>=<span class="value">{this.setPageSize}</span> <span class="attribute">externalMaxPage</span>=<span class="value">{this.state.maxPages}</span>
<span class="attribute">externalChangeSort</span>=<span class="value">{function(){}}</span> <span class="attribute">externalSetFilter</span>=<span class="value">{function(){}}</span>
<span class="attribute">externalCurrentPage</span>=<span class="value">{this.state.currentPage}</span> <span class="attribute">results</span>=<span class="value">{this.state.results}</span> <span class="attribute">tableClassName</span>=<span class="value">"table"</span> <span class="attribute">resultsPerPage</span>=<span class="value">{this.state.externalResultsPerPage}</span>
<span class="attribute">externalSortColumn</span>=<span class="value">{this.state.externalSortColumn}</span> <span class="attribute">externalSortAscending</span>=<span class="value">{this.state.externalSortAscending}</span> /></span>
}
});</span></code></pre>
<div id="griddle-external-swapi"></div>
<script type="text/javascript" src="scripts/swapi.min.js"></script>
<script type="text/jsx">
var Loading = React.createClass({
getDefaultProps: function(){
return {
loadingText: "Loading"
}
},
render: function(){
return <div className="loading">{this.props.loadingText}</div>;
}
});
var ExternalSwapiComponent = React.createClass({
getInitialState: function(){
var initial = { "results": [],
"currentPage": 0,
"isLoading": false,
"maxPages": 0,
"externalResultsPerPage": 10,
"externalSortColumn":null,
"externalSortAscending":true,
"results": []
};
return initial;
},
componentWillMount: function(){
},
componentDidMount: function(){
this.getExternalData();
},
getExternalData: function(page){
var that = this;
page = page||1
this.setState({
isLoading: true
});
swapiModule.getStarships(page, function(data) {
that.setState({
results: data.results,
currentPage: page-1,
maxPages: Math.round(data.count/10),
isLoading: false
})
});
},
setPage: function(index){
//This should interact with the data source to get the page at the given index
index = index > this.state.maxPages ? this.state.maxPages : index < 1 ? 1 : index + 1;
this.getExternalData(index);
},
setPageSize: function(size){
},
render: function(){
//columns={["name", "city", "state", "country"]}
return <Griddle useExternal={true} externalSetPage={this.setPage} enableSort={false} columns={["name", "model", "manufacturer", "passengers"]}
externalSetPageSize={this.setPageSize} externalMaxPage={this.state.maxPages}
externalChangeSort={function(){}} externalSetFilter={function(){}}
externalCurrentPage={this.state.currentPage} results={this.state.results} tableClassName="table" resultsPerPage={this.state.externalResultsPerPage}
externalSortColumn={this.state.externalSortColumn} externalSortAscending={this.state.externalSortAscending} externalLoadingComponent={Loading} externalIsLoading={this.state.isLoading}/>
}
});
ReactDOM.render(<ExternalSwapiComponent />, document.getElementById("griddle-external-swapi"));
</script>
<hr />
<h5 id="simulated-external-results-example-">Simulated External Results Example:</h5>
<p>The following example is using the skeleton template above and simulating what loading results externally could look like (while still using the example data that has been used through-out the Griddle documentation). </p>
<p>Please keep in mind that a good deal of this code is to simulate the type of actions that would generally take place on a server (or wherever the data is coming from) -- most of the time the functions should deal with passing data back and forth to the source of the data (e.g. an API).</p>
<pre><code class="lang-javascript"><span class="keyword">var</span> externalData = fakeData.slice(<span class="number">0</span>, <span class="number">53</span>);
<span class="keyword">var</span> SimulatedExternalComponent = React.createClass({
getInitialState: <span class="keyword">function</span>(){
<span class="keyword">var</span> initial = { <span class="string">"results"</span>: [],
<span class="string">"currentPage"</span>: <span class="number">0</span>,
<span class="string">"maxPages"</span>: <span class="number">0</span>,
<span class="string">"externalResultsPerPage"</span>: <span class="number">5</span>,
<span class="string">"externalSortColumn"</span>:<span class="literal">null</span>,
<span class="string">"externalSortAscending"</span>:<span class="literal">true</span>,
<span class="string">"pretendServerData"</span>: externalData
};
<span class="keyword">return</span> initial;
},
componentWillMount: <span class="keyword">function</span>(){
<span class="keyword">this</span>.setState({
maxPages: Math.round(<span class="keyword">this</span>.state.pretendServerData.length/<span class="keyword">this</span>.state.externalResultsPerPage),
<span class="string">"results"</span>: <span class="keyword">this</span>.state.pretendServerData.slice(<span class="number">0</span>,<span class="keyword">this</span>.state.externalResultsPerPage)
})
},
setPage: <span class="keyword">function</span>(index){
<span class="comment">//This should interact with the data source to get the page at the given index</span>
<span class="keyword">var</span> number = index === <span class="number">0</span> ? <span class="number">0</span> : index * <span class="keyword">this</span>.state.externalResultsPerPage;
<span class="keyword">this</span>.setState(
{
<span class="string">"results"</span>: <span class="keyword">this</span>.state.pretendServerData.slice(number, number+<span class="number">5</span>><span class="keyword">this</span>.state.pretendServerData.length ? <span class="keyword">this</span>.state.pretendServerData.length : number+<span class="keyword">this</span>.state.externalResultsPerPage),
<span class="string">"currentPage"</span>: index
});
},
sortData: <span class="keyword">function</span>(sort, sortAscending, data){
<span class="comment">//sorting should generally happen wherever the data is coming from </span>
sortedData = _.sortBy(data, <span class="keyword">function</span>(item){
<span class="keyword">return</span> item[sort];
});
<span class="keyword">if</span>(sortAscending === <span class="literal">false</span>){
sortedData.reverse();
}
<span class="keyword">return</span> {
<span class="string">"currentPage"</span>: <span class="number">0</span>,
<span class="string">"externalSortColumn"</span>: sort,
<span class="string">"externalSortAscending"</span>: sortAscending,
<span class="string">"pretendServerData"</span>: sortedData,
<span class="string">"results"</span>: sortedData.slice(<span class="number">0</span>,<span class="keyword">this</span>.state.externalResultsPerPage)
};
},
changeSort: <span class="keyword">function</span>(sort, sortAscending){
<span class="comment">//this should change the sort for the given column</span>
<span class="keyword">this</span>.setState(<span class="keyword">this</span>.sortData(sort, sortAscending, <span class="keyword">this</span>.state.pretendServerData));
},
setFilter: <span class="keyword">function</span>(filter){
<span class="comment">//filtering should generally occur on the server (or wherever) </span>
<span class="comment">//this is a lot of code for what should normally just be a method that is used to pass data back and forth</span>
<span class="keyword">var</span> sortedData = <span class="keyword">this</span>.sortData(<span class="keyword">this</span>.state.externalSortColumn, <span class="keyword">this</span>.state.externalSortAscending, externalData);
<span class="keyword">if</span>(filter === <span class="string">""</span>){
<span class="keyword">this</span>.setState(_.extend(sortedData, {maxPages: Math.round(sortedData.pretendServerData.length > <span class="keyword">this</span>.state.externalResultsPerPage ? sortedData.pretendServerData.length/<span class="keyword">this</span>.state.externalResultsPerPage : <span class="number">1</span>)}));
<span class="keyword">return</span>;
}
<span class="keyword">var</span> filteredData = _.filter(sortedData.pretendServerData,
<span class="keyword">function</span>(item) {
<span class="keyword">var</span> arr = _.values(item);
<span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i < arr.length; i++){
<span class="keyword">if</span> ((arr[i]||<span class="string">""</span>).toString().toLowerCase().indexOf(filter.toLowerCase()) >= <span class="number">0</span>){
<span class="keyword">return</span> <span class="literal">true</span>;
}
}
<span class="keyword">return</span> <span class="literal">false</span>;
});
<span class="keyword">this</span>.setState({
pretendServerData: filteredData,
maxPages: Math.round(filteredData.length > <span class="keyword">this</span>.state.externalResultsPerPage ? filteredData.length/<span class="keyword">this</span>.state.externalResultsPerPage : <span class="number">1</span>),
<span class="string">"results"</span>: filteredData.slice(<span class="number">0</span>,<span class="keyword">this</span>.state.externalResultsPerPage)
});
},
setPageSize: <span class="keyword">function</span>(size){
<span class="keyword">this</span>.setState({
currentPage: <span class="number">0</span>,
externalResultsPerPage: size,
maxPages: Math.round(<span class="keyword">this</span>.state.pretendServerData.length > size ? <span class="keyword">this</span>.state.pretendServerData.length/size : <span class="number">1</span>),
results: <span class="keyword">this</span>.state.pretendServerData.slice(<span class="number">0</span>,size)
});
},
render: <span class="keyword">function</span>(){
<span class="keyword">return</span> <span class="xml"><span class="tag"><<span class="title">Griddle</span> <span class="attribute">useExternal</span>=<span class="value">{true}</span> <span class="attribute">externalSetPage</span>=<span class="value">{this.setPage}</span>
<span class="attribute">externalChangeSort</span>=<span class="value">{this.changeSort}</span> <span class="attribute">externalSetFilter</span>=<span class="value">{this.setFilter}</span>
<span class="attribute">externalSetPageSize</span>=<span class="value">{this.setPageSize}</span> <span class="attribute">externalMaxPage</span>=<span class="value">{this.state.maxPages}</span>
<span class="attribute">externalCurrentPage</span>=<span class="value">{this.state.currentPage}</span> <span class="attribute">results</span>=<span class="value">{this.state.results}</span> <span class="attribute">tableClassName</span>=<span class="value">"table"</span> <span class="attribute">resultsPerPage</span>=<span class="value">{this.state.externalResultsPerPage}</span>
<span class="attribute">externalSortColumn</span>=<span class="value">{this.state.externalSortColumn}</span> <span class="attribute">externalSortAscending</span>=<span class="value">{this.state.externalSortAscending}</span> <span class="attribute">showFilter</span>=<span class="value">{true}</span> <span class="attribute">showSettings</span>=<span class="value">{true}</span> /></span>
}
});
React.render(<span class="tag"><<span class="title">SimulatedExternalComponent</span> /></span>, document.getElementById("griddle-external-simulated"));</span></code></pre>
<div id="griddle-external-simulated"></div>
<script type="text/jsx">
var externalData = fakeData.slice(0, 53);
var SimulatedExternalComponent = React.createClass({
getInitialState: function(){
var initial = { "results": [],
"currentPage": 0,
"maxPages": 0,
"externalResultsPerPage": 5,
"externalSortColumn":null,
"externalSortAscending":true,
"pretendServerData": externalData
};
return initial;
},
componentWillMount: function(){
this.setState({
maxPages: Math.ceil(this.state.pretendServerData.length/this.state.externalResultsPerPage),
"results": this.state.pretendServerData.slice(0,this.state.externalResultsPerPage)
})
},
setPage: function(index){
//This should interact with the data source to get the page at the given index
var number = index === 0 ? 0 : index * this.state.externalResultsPerPage;
this.setState(
{
"results": this.state.pretendServerData.slice(number, number+5>this.state.pretendServerData.length ? this.state.pretendServerData.length : number+this.state.externalResultsPerPage),
"currentPage": index
});
},
sortData: function(sort, sortAscending, data){
//sorting should generally happen wherever the data is coming from
sortedData = _.sortBy(data, function(item){
return item[sort];
});
if(sortAscending === false){
sortedData.reverse();
}
return {
"currentPage": 0,
"externalSortColumn": sort,
"externalSortAscending": sortAscending,
"pretendServerData": sortedData,
"results": sortedData.slice(0,this.state.externalResultsPerPage)
};
},
changeSort: function(sort, sortAscending){
//this should change the sort for the given column
this.setState(this.sortData(sort, sortAscending, this.state.pretendServerData));
},
setFilter: function(filter){
//filtering should generally occur on the server (or wherever)
//this is a lot of code for what should normally just be a method that is used to pass data back and forth
var sortedData = this.sortData(this.state.externalSortColumn, this.state.externalSortAscending, externalData);
if(filter === ""){
this.setState(_.extend(sortedData, {maxPages: Math.ceil(sortedData.pretendServerData.length > this.state.externalResultsPerPage ? sortedData.pretendServerData.length/this.state.externalResultsPerPage : 1)}));
return;
}
var filteredData = _.filter(sortedData.pretendServerData,
function(item) {
var arr = _.values(item);
for(var i = 0; i < arr.length; i++){
if ((arr[i]||"").toString().toLowerCase().indexOf(filter.toLowerCase()) >= 0){
return true;
}
}
return false;
});
this.setState({
pretendServerData: filteredData,
maxPages: Math.ceil(filteredData.length > this.state.externalResultsPerPage ? filteredData.length/this.state.externalResultsPerPage : 1),
"results": filteredData.slice(0,this.state.externalResultsPerPage)
});
},
setPageSize: function(size){
this.setState({
currentPage: 0,
externalResultsPerPage: size,
maxPages: Math.ceil(this.state.pretendServerData.length > size ? this.state.pretendServerData.length/size : 1),
results: this.state.pretendServerData.slice(0,size)
});
},
render: function(){
return <Griddle useExternal={true} externalSetPage={this.setPage}
externalChangeSort={this.changeSort} externalSetFilter={this.setFilter}
externalSetPageSize={this.setPageSize} externalMaxPage={this.state.maxPages}
externalCurrentPage={this.state.currentPage} results={this.state.results} tableClassName="table" resultsPerPage={this.state.externalResultsPerPage}
externalSortColumn={this.state.externalSortColumn} externalSortAscending={this.state.externalSortAscending} showFilter={true} showSettings={true} />
}
});
ReactDOM.render(<SimulatedExternalComponent />, document.getElementById("griddle-external-simulated"));
</script>
<hr />
<h4 id="griddle-with-callback">Griddle With Callback</h4>
<p>Previous versions of Griddle had a built-in callback for obtaining external data. This has since been moved into its own component to help simplify the main Griddle component. The call definition is the same. </p>
<h5 id="important-">Important!</h5>
<p><strong>GriddleWithCallback is in its own GitHub repository and npm package. To install GriddleWithCallback <code>npm install griddle-callback</code> and anywhere where it is used, <code>require GriddleWithCallback = require('griddle-callback');</code></strong></p>
<h5 id="example-">Example:</h5>
<p>Lets assume we want to obtain Star Wars data like in the example above except using GriddleWithCallback instead of using the exernal results properties. We will start out by creating a callback method. The Griddle callback has access to <code>filterString, sortColumn, sortAscending, page, pageSize, callback</code> parameters.</p>
<p>For the example, our Callback looks like this: </p>
<pre><code class="lang-javascript"><span class="keyword">var</span> loadData = <span class="keyword">function</span>(filterString, sortColumn, sortAscending, page, pageSize, callback) {
page+=<span class="number">1</span>;
swapiModule.getStarships(page, <span class="keyword">function</span>(data){
callback({
results: data.results,
totalResults: data.count,
pageSize: pageSize
});
});
};</code></pre>
<p><div id="griddle-callback"></div></p>
<script type="text/javascript" src="scripts/GriddleWithCallback.js"></script>
<script type="text/jsx">
var setDefault = function(original, value){
return typeof original === 'undefined' ? value : original;
}
var loadData = function(filterString, sortColumn, sortAscending, page, pageSize, callback) {
page+=1;
swapiModule.getStarships(page, function(data){
callback({
results: data.results,
totalResults: data.count,
pageSize: pageSize
});
});
};
var Loading = React.createClass({
getDefaultProps: function(){
return {
loadingText: "Loading"
}
},
render: function(){
return <div className="loading">{this.props.loadingText}</div>;
}
});
ReactDOM.render(<GriddleWithCallback getExternalResults={loadData} loadingComponent={Loading} enableSort={false} columns={["name", "model", "manufacturer", "passengers"]} resultsPerPage={10} showFilter={false} showSettings={false} />, document.getElementById('griddle-callback'));
</script>
</div>
</div>
</div>
<div class="order-primary content-section background-darker-grey text-light-grey" id="about">
<div class="container">
<div class="row">
<div class="ten columns offset-by-two">
<p>Griddle is a project maintained by <a href="http://twitter.com/ryanlanciaux">Ryan Lanciaux</a> and <a href="http://www.twitter.com/joellanciaux">Joel Lanciaux</a> in collaboration with all of <a href="https://github.com/GriddleGriddle/Griddle/graphs/contributors">the awesome contributors!</a></p>
<small>
© 2015 <a href="http://www.twitter.com/ryanlanciaux">Ryan Lanciaux</a> | <a href="https://www.github.com/DynamicTyped">DynamicTyped</a>
</small>
<br />
<small>
Example data has been generated with <a href="http://json-generator.com">json-generator.com</a>. Any data that resembles real people, places, companies, etc. is coincidence.
</small>
</div>
</div>
</div>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-58716212-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>