Skip to content

Commit 9a0369e

Browse files
committed
Major changes for server-side data
1 parent 48e6d16 commit 9a0369e

File tree

6 files changed

+605
-514
lines changed

6 files changed

+605
-514
lines changed

README.md

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ A fast, lightweight, opinionated table and datagrid built on React
99
## Features
1010

1111
- Lightweight at 3kb (and just 2kb more for styles)
12-
- No composition needed
13-
- Uses customizable JSX and callbacks for everything
14-
- Client-side pagination and sorting
15-
- Server-side data
12+
- Fully customizable JSX and callbacks for everything
13+
- Supports both Client-side & Server-side pagination and sorting
1614
- Minimal design & easily themeable
1715

1816
## [Demo](http://react-table.zabapps.com)
@@ -196,47 +194,39 @@ const columns = [{
196194

197195
<a name="server-side-data"></a>
198196
## Server-side Data
199-
If you want to handle pagination, and sorting on the server, `react-table` makes it easy on you. Instead of passing the `data` prop an array, you provide a function instead.
197+
If you want to handle pagination, and sorting on the server, `react-table` makes it easy on you.
200198

201-
This function will be called on mount, pagination events, and sorting events. It also provides you all of the parameters to help you query and format your data.
199+
1. Feed React Table `data` from somewhere dynamic. eg. `state`, a redux store, etc...
200+
1. Add `manual` as a prop. This informs React Table that you'll be handling sorting and pagination server-side
201+
1. Subscribe to the `onChange` prop. This function is called any time sorting or pagination is changed by the user
202+
1. In the `onChange` callback, request your data using the provided information in the params of the function (state and instance)
203+
1. Update your data with the rows to be displayed
204+
1. Optionally set how many pages there are total
202205

203206
```javascript
204207
<ReactTable
205-
data={(params, callback) => {
206-
207-
// params will give you all the info you need to query and sort your data
208-
params == {
209-
page: 0, // The page index the user is requesting
210-
pageSize: 20, // The current pageSize
211-
pages: -1, // The amount of existing pages (-1 means there is no page data yet)
212-
sorting: [ // An array of column sort models (yes, you can multi-sort!)
213-
{
214-
id: 'columnID', // The columnID (usually the accessor string, but can be overridden for server-side or required if the column accessor is a function)
215-
ascending: true or false
216-
}
217-
]
218-
}
219-
220-
// Query your data however you'd like, then structure your response like so:
221-
const result = {
222-
rows: [...], // Your data for the current page/sorting model
223-
pages: 10 // optionally provide how many pages exist (this is only needed if you choose to display page numbers, and only the first time you make the call or if the page count changes)
224-
}
225-
226-
// You can return a promise that resolve the result
227-
return Axios.post('/myDataEnpoint', params) // resolves to `result`
228-
229-
// or use the manual callback whenever you please
230-
setTimeout(() => {
231-
callback(result)
232-
}, 5000)
233-
234-
// That's it!
235-
208+
...
209+
data={this.state.data} // should default to []
210+
pages={this.state.pages} // should default to -1 (which means we don't know how many pages we have)
211+
manual // informs React Table that you'll be handling sorting and pagination server-side
212+
onChange={(state, instance) => {
213+
Axios.post('mysite.com/data', {
214+
page: state.page,
215+
pageSize: state.pageSize,
216+
sorting: state.sorting
217+
})
218+
.then((res) => {
219+
this.setState({
220+
data: res.data.rows,
221+
pages: res.data.pages
222+
})
223+
})
236224
}}
237225
/>
238226
```
239227

228+
For a detailed example, take a peek at our [async table mockup](https://github.com/tannerlinsley/react-table/blob/master/example/src/screens/async.js)
229+
240230
<a name="multi-sort"></a>
241231
## Multi-Sort
242232
When clicking on a column header, hold shift to multi-sort! You can toggle `ascending` `descending` and `none` for multi-sort columns. Clicking on a header without holding shift will clear the multi-sort and replace it with the single sort of that column. It's quite handy!

0 commit comments

Comments
 (0)