import React from 'react' import _ from 'lodash' import namor from 'namor' import CodeHighlight from './components/codeHighlight' import ReactTable from '../src/index' const rawData = _.map(_.range(3424), d => { return { firstName: namor.generate({ words: 1, numLen: 0 }), lastName: namor.generate({ words: 1, numLen: 0 }), age: Math.floor(Math.random() * 30) } }) // Now let's mock the server. It's job is simple: use the table model to sort and return the page data const requestData = (pageSize, page, sorting, filtering) => { return new Promise((resolve, reject) => { // On the server, you'll likely use SQL or noSQL or some other query language to do this. // For this mock, we'll just use lodash let filteredData = rawData; if (filtering.length) { filteredData = filtering.reduce( (filteredSoFar, nextFilter) => { return filteredSoFar.filter( (row) => { return (row[nextFilter.id]+"").includes(nextFilter.value) }) } , filteredData) } const sortedData = _.orderBy(filteredData, sorting.map(sort => { return row => { if (row[sort.id] === null || row[sort.id] === undefined) { return -Infinity } return typeof row[sort.id] === 'string' ? row[sort.id].toLowerCase() : row[sort.id] } }), sorting.map(d => d.desc ? 'desc' : 'asc')) // Be sure to send back the rows to be displayed and any other pertinent information, like how many pages there are total. const res = { rows: sortedData.slice(pageSize * page, (pageSize * page) + pageSize), pages: Math.ceil(filteredData.length / pageSize) } // Here we'll simulate a server response with 500ms of delay. setTimeout(() => resolve(res), 500) }) } const ServerSide = React.createClass({ getInitialState () { // To handle our data server-side, we need a few things in the state to help us out: return { data: [], pages: null, loading: true } }, fetchData (state, instance) { // Whenever the table model changes, or the user sorts or changes pages, this method gets called and passed the current table model. // You can set the `loading` prop of the table to true to use the built-in one or show you're own loading bar if you want. this.setState({loading: true}) // Request the data however you want. Here, we'll use our mocked service we created earlier requestData(state.pageSize, state.page, state.sorting, state.filtering) .then((res) => { console.log(res.rows) // Now just get the rows of data to your React Table (and update anything else like total pages or loading) this.setState({ data: res.rows, pages: res.pages, loading: false }) }) }, render () { return (