You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+27-37Lines changed: 27 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,10 +9,8 @@ A fast, lightweight, opinionated table and datagrid built on React
9
9
## Features
10
10
11
11
- 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
16
14
- Minimal design & easily themeable
17
15
18
16
## [Demo](http://react-table.zabapps.com)
@@ -196,47 +194,39 @@ const columns = [{
196
194
197
195
<aname="server-side-data"></a>
198
196
## 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.
200
198
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
202
205
203
206
```javascript
204
207
<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
-
constresult= {
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
-
returnAxios.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
+
})
236
224
}}
237
225
/>
238
226
```
239
227
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
+
240
230
<aname="multi-sort"></a>
241
231
## Multi-Sort
242
232
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