|
| 1 | +/* eslint max-len: 0 */ |
| 2 | +import React from 'react'; |
| 3 | +import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table'; |
| 4 | + |
| 5 | +const products = []; |
| 6 | + |
| 7 | +function addProducts(quantity) { |
| 8 | + const startId = products.length; |
| 9 | + for (let i = 0; i < quantity; i++) { |
| 10 | + const id = startId + i; |
| 11 | + products.push({ |
| 12 | + id: id, |
| 13 | + name: 'Item name ' + id, |
| 14 | + price: 2100 + i |
| 15 | + }); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +addProducts(100); |
| 20 | + |
| 21 | +const options = { |
| 22 | + sizePerPageList: [ 5, 10, 15, 20 ], |
| 23 | + sizePerPage: 10, |
| 24 | + sortName: 'id', |
| 25 | + sortOrder: 'desc' |
| 26 | +}; |
| 27 | +const selectRowProp = { |
| 28 | + mode: 'checkbox', |
| 29 | + clickToSelect: true, |
| 30 | + bgColor: 'rgb(238, 193, 213)' |
| 31 | +}; |
| 32 | +const filterOptions = { |
| 33 | + type: 'TextFilter', |
| 34 | + placeholder: 'Product Name' |
| 35 | +}; |
| 36 | + |
| 37 | +export default class SelectAll extends React.Component { |
| 38 | + constructor(props) { |
| 39 | + super(props); |
| 40 | + this.state = { |
| 41 | + selected: [], |
| 42 | + filteredProducts: products |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + onSelect(row, isSelected) { |
| 47 | + const selectedProducts = this.state.selected; |
| 48 | + if (isSelected) { |
| 49 | + selectedProducts.push(row.id); |
| 50 | + } else { |
| 51 | + const index = selectedProducts |
| 52 | + .indexOf(row.universalId); |
| 53 | + selectedProducts.splice(index, 1); |
| 54 | + } |
| 55 | + this.setState({ selected: selectedProducts }); |
| 56 | + } |
| 57 | + |
| 58 | + onSelectAll(isSelected) { |
| 59 | + const items = isSelected |
| 60 | + ? this.state.filteredProducts.map(product => product.id) |
| 61 | + : []; |
| 62 | + this.setState({ selected: items }); |
| 63 | + } |
| 64 | + |
| 65 | + afterColumnFilter(filterConds, result) { |
| 66 | + this.setState({ filteredProducts: result }); |
| 67 | + } |
| 68 | + |
| 69 | + render() { |
| 70 | + Object.assign(selectRowProp, { |
| 71 | + selected: this.state.selected, |
| 72 | + onSelect: this.onSelect.bind(this), |
| 73 | + onSelectAll: this.onSelectAll.bind(this) }); |
| 74 | + Object.assign(options, { |
| 75 | + afterColumnFilter: this.afterColumnFilter.bind(this) }); |
| 76 | + |
| 77 | + return ( |
| 78 | + <div> |
| 79 | + <div>Product selected: { this.state.selected.length }</div> |
| 80 | + <BootstrapTable ref='table' selectRow={ selectRowProp } data={ products } pagination={ true } options={ options }> |
| 81 | + <TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn> |
| 82 | + <TableHeaderColumn dataField='name' filter={ filterOptions }>Product Name</TableHeaderColumn> |
| 83 | + <TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn> |
| 84 | + </BootstrapTable> |
| 85 | + </div> |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
0 commit comments