import React from 'react' import _ from 'lodash' import namor from 'namor' import CodeHighlight from './components/codeHighlight' import ReactTable from '../src/index' class Story extends React.Component { constructor (props) { super(props) const data = _.map(_.range(5553), d => { return { firstName: namor.generate({words: 1, numLen: 0}), lastName: namor.generate({words: 1, numLen: 0}), age: Math.floor(Math.random() * 30) } }) this.state = { tableOptions: { loading: false, showPagination: true, showPageSizeOptions: true, showPageJump: true, collapseOnSortingChange: true, collapseOnPageChange: true, collapseOnDataChange: true, freezeWhenExpanded: false, filterable: true, sortable: true, resizable: true }, data: data } this.setTableOption = this.setTableOption.bind(this) } render () { const columns = [{ Header: 'Name', columns: [{ Header: 'First Name', accessor: 'firstName', filterMethod: (filter, row) => (row[filter.id].startsWith(filter.value) && row[filter.id].endsWith(filter.value)) }, { Header: 'Last Name', id: 'lastName', accessor: d => d.lastName, filterMethod: (filter, row) => (row[filter.id].includes(filter.value)) }] }, { Header: 'Info', columns: [{ Header: 'Age', accessor: 'age' }, { Header: 'Over 21', accessor: 'age', id: 'over', Cell: ({value}) => (value >= 21 ? 'Yes' : 'No'), filterMethod: (filter, row) => { if (filter.value === 'all') { return true } if (filter.value === 'true') { return row[filter.id] >= 21 } return row[filter.id] < 21 }, Filter: ({filter, onChange}) => ( ) } ] }] return (

Table Options

{ Object.keys(this.state.tableOptions).map(optionKey => { const optionValue = this.state.tableOptions[optionKey] return ( ) }) }
{optionKey}
(String(row[filter.id]) === filter.value)} {...this.state.tableOptions} SubComponent={(row) => { return (
You can put any component you want here, even another React Table!

{ return (
It even has access to the row data: {() => JSON.stringify(row, null, 2)}
) }} />
) }} />

Tip: Hold shift when sorting to multi-sort!

Custom Filters In This Example

The default filter for all columns of a table if it is not specified in the configuration is set to match on values that start with the filter text. Example: age.startsWith("2").

This example overrides the default filter behavior by setting the defaultFilterMethod table option to match on values that are exactly equal to the filter text. Example: age == "23")

Each column can also be customized with the column filterMethod option:

In this example the firstName column filters on the value starting with and ending with the filter value.

In this example the lastName column filters on the value including the filter value anywhere in its text.

To completely override the filter that is shown, you can set the Filter column option. Using this option you can specify the JSX that is shown. The option is passed an onChange method that must be called with the value that you wan't to pass to the filterMethod option whenever the filter has changed.

) } setTableOption (event) { const target = event.target const value = target.type === 'checkbox' ? target.checked : target.value const name = target.name this.setState({ tableOptions: { ...this.state.tableOptions, [name]: value } }) } } // Source Code const source = require('!raw-loader!./Filtering') export default () => (
{() => source}
)