|
| 1 | +Getting Started |
| 2 | +=============== |
| 3 | + |
| 4 | +The easiest way to start using on FixedDataTable is to install it via npm: |
| 5 | + |
| 6 | +```shell |
| 7 | +npm install react --save |
| 8 | +npm install fixed-data-table --save |
| 9 | +``` |
| 10 | + |
| 11 | +If your using a standard build system such as [`browserify`](http://browserify.org/) or [`webpack`](https://webpack.github.io/) it can then be required directly: |
| 12 | +``` |
| 13 | +const React = require('react'); |
| 14 | +const {Table, Column, Cell} = require('fixed-data-table'); |
| 15 | +``` |
| 16 | + |
| 17 | +For layout and styling the default stylesheet will need to be added: `fixed-data-table/dist/fixed-data-table.min.css`. |
| 18 | + |
| 19 | +## Create your `Table` |
| 20 | +Setting up your table can be done via the `Table` component. To be able to handle large amounts of data the table only renders the parts that are visible to the user, in order to calculate this a static `width`, `height`, `rowsCount` and `rowHeight` are required: |
| 21 | + |
| 22 | +```javascript |
| 23 | +const React = require('react'); |
| 24 | +const {Table} = require('fixed-data-table'); |
| 25 | + |
| 26 | +class MyTable extends React.Component { |
| 27 | + render() { |
| 28 | + return ( |
| 29 | + <Table |
| 30 | + rowsCount={100} |
| 31 | + rowHeight={50} |
| 32 | + width={1000} |
| 33 | + height={500}> |
| 34 | + // TODO: Add columns |
| 35 | + </Table> |
| 36 | + ); |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +## Create your `Column`s |
| 42 | +For each column that needs to be displayed a `Column` config with 2 important props are required. The `width` of the column and the `cell` content to render. The `Cell` component can wrap any content to provide default table styles and centering. |
| 43 | + |
| 44 | +```javascript |
| 45 | +const React = require('react'); |
| 46 | +const {Table, Column, Cell} = require('fixed-data-table'); |
| 47 | + |
| 48 | +class MyTable extends React.Component { |
| 49 | + render() { |
| 50 | + return ( |
| 51 | + <Table |
| 52 | + rowsCount={100} |
| 53 | + rowHeight={50} |
| 54 | + width={1000} |
| 55 | + height={500}> |
| 56 | + <Column |
| 57 | + cell={<Cell>Basic content</Cell>} |
| 58 | + width={200} |
| 59 | + /> |
| 60 | + </Table> |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Provide Custom Data |
| 67 | +Typically you will want to render custom data per row, let's add some data to our table and add a header to the column. |
| 68 | + |
| 69 | +```javascript |
| 70 | +const React = require('react'); |
| 71 | +const {Table, Column, Cell} = require('fixed-data-table'); |
| 72 | + |
| 73 | +class MyTable extends React.Component { |
| 74 | + constructor(props) { |
| 75 | + super(props); |
| 76 | + |
| 77 | + this.state = { |
| 78 | + myTableData: [ |
| 79 | + {name: 'Rylan'}, |
| 80 | + {name: 'Amelia'}, |
| 81 | + {name: 'Estevan'}, |
| 82 | + {name: 'Florence'}, |
| 83 | + {name: 'Tressa'}, |
| 84 | + ], |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + render() { |
| 89 | + return ( |
| 90 | + <Table |
| 91 | + rowsCount={this.state.myTableData.length} |
| 92 | + rowHeight={50} |
| 93 | + headerHeight={50} |
| 94 | + width={1000} |
| 95 | + height={500}> |
| 96 | + <Column |
| 97 | + header={<Cell>Name</Cell>} |
| 98 | + cell={props => ( |
| 99 | + <Cell {...props}> |
| 100 | + {this.state.myTableData[props.rowIndex].name} |
| 101 | + </Cell> |
| 102 | + )} |
| 103 | + width={200} |
| 104 | + /> |
| 105 | + </Table> |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +Instead of providing a static `cell` we can use a function for our `cell`, this function will be passed the `rowIndex` plus the `width` and `height` of the cell. By using the `rowIndex` and we can get different data from `this.state.myTableData` for each cell. |
| 112 | + |
| 113 | +## Create Reusable Cells |
| 114 | + |
| 115 | +With larger table setups you will want to reuse the `cell` render functions. To do this you can create your own React Component for each unique `Cell`. Let's add another column that displays emails with links. |
| 116 | + |
| 117 | +```javascript |
| 118 | +const React = require('react'); |
| 119 | +const {Table, Column, Cell} = require('fixed-data-table'); |
| 120 | + |
| 121 | +class MyTextCell extends React.Component { |
| 122 | + render() { |
| 123 | + const {rowIndex, field, data, ...props} = this.props; |
| 124 | + return ( |
| 125 | + <Cell {...props}> |
| 126 | + {data[rowIndex][field]} |
| 127 | + </Cell> |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +class MyLinkCell extends React.Component { |
| 133 | + render() { |
| 134 | + const {rowIndex, field, data, ...props} = this.props; |
| 135 | + const link = data[rowIndex][field]; |
| 136 | + return ( |
| 137 | + <Cell {...props}> |
| 138 | + <a href={link}>{link}</a> |
| 139 | + </Cell> |
| 140 | + ); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +class MyTable extends React.Component { |
| 145 | + constructor(props) { |
| 146 | + super(props); |
| 147 | + |
| 148 | + this.state = { |
| 149 | + myTableData: [ |
| 150 | + {name : 'Rylan', email : '[email protected]'}, |
| 151 | + {name : 'Amelia', email : '[email protected]'}, |
| 152 | + {name : 'Estevan', email : '[email protected]'}, |
| 153 | + {name : 'Florence', email : '[email protected]'}, |
| 154 | + {name : 'Tressa', email : '[email protected]'}, |
| 155 | + ], |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + render() { |
| 160 | + return ( |
| 161 | + <Table |
| 162 | + rowsCount={this.state.myTableData.length} |
| 163 | + rowHeight={50} |
| 164 | + headerHeight={50} |
| 165 | + width={1000} |
| 166 | + height={500}> |
| 167 | + <Column |
| 168 | + header={<Cell>Name</Cell>} |
| 169 | + cell={ |
| 170 | + <MyTextCell |
| 171 | + data={this.state.myTableData} |
| 172 | + field="name" |
| 173 | + /> |
| 174 | + } |
| 175 | + width={200} |
| 176 | + /> |
| 177 | + <Column |
| 178 | + header={<Cell>Email</Cell>} |
| 179 | + cell={ |
| 180 | + <MyLinkCell |
| 181 | + data={this.state.myTableData} |
| 182 | + field="email" |
| 183 | + /> |
| 184 | + } |
| 185 | + width={200} |
| 186 | + /> |
| 187 | + </Table> |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +## Next Steps |
| 194 | + |
| 195 | +The FixedDataTable has many more options, for more information see the [examples section](http://facebook.github.io/fixed-data-table/example-object-data.html) or the [API docs](http://facebook.github.io/fixed-data-table/api-table.html). |
0 commit comments