|
| 1 | +import React from 'react'; |
| 2 | +import BootstrapTable from 'react-bootstrap-table-next'; |
| 3 | +import filterFactory, { selectFilter } from 'react-bootstrap-table2-filter'; |
| 4 | +import Code from 'components/common/code-block'; |
| 5 | +import { productsQualityGenerator } from 'utils/common'; |
| 6 | + |
| 7 | +const products = productsQualityGenerator(6); |
| 8 | + |
| 9 | +const selectOptions = { |
| 10 | + 0: 'good', |
| 11 | + 1: 'Bad', |
| 12 | + 2: 'unknown' |
| 13 | +}; |
| 14 | + |
| 15 | +const selectOptionsArr = [{ |
| 16 | + value: 0, |
| 17 | + label: 'good' |
| 18 | +}, { |
| 19 | + value: 1, |
| 20 | + label: 'Bad' |
| 21 | +}, { |
| 22 | + value: 2, |
| 23 | + label: 'unknown' |
| 24 | +}]; |
| 25 | + |
| 26 | +const columns1 = [{ |
| 27 | + dataField: 'id', |
| 28 | + text: 'Product ID' |
| 29 | +}, { |
| 30 | + dataField: 'name', |
| 31 | + text: 'Product Name' |
| 32 | +}, { |
| 33 | + dataField: 'quality', |
| 34 | + text: 'Product Quailty', |
| 35 | + formatter: cell => selectOptions[cell], |
| 36 | + filter: selectFilter({ |
| 37 | + options: selectOptions |
| 38 | + }) |
| 39 | +}]; |
| 40 | + |
| 41 | +const columns2 = [{ |
| 42 | + dataField: 'id', |
| 43 | + text: 'Product ID' |
| 44 | +}, { |
| 45 | + dataField: 'name', |
| 46 | + text: 'Product Name' |
| 47 | +}, { |
| 48 | + dataField: 'quality', |
| 49 | + text: 'Product Quailty', |
| 50 | + formatter: cell => selectOptionsArr.filter(opt => opt.value === cell)[0].label || '', |
| 51 | + filter: selectFilter({ |
| 52 | + options: selectOptionsArr |
| 53 | + }) |
| 54 | +}]; |
| 55 | + |
| 56 | +const columns3 = [{ |
| 57 | + dataField: 'id', |
| 58 | + text: 'Product ID' |
| 59 | +}, { |
| 60 | + dataField: 'name', |
| 61 | + text: 'Product Name' |
| 62 | +}, { |
| 63 | + dataField: 'quality', |
| 64 | + text: 'Product Quailty', |
| 65 | + formatter: cell => selectOptionsArr.filter(opt => opt.value === cell)[0].label || '', |
| 66 | + filter: selectFilter({ |
| 67 | + options: () => selectOptionsArr |
| 68 | + }) |
| 69 | +}]; |
| 70 | + |
| 71 | +const sourceCode = `\ |
| 72 | +import BootstrapTable from 'react-bootstrap-table-next'; |
| 73 | +import filterFactory, { selectFilter } from 'react-bootstrap-table2-filter'; |
| 74 | +
|
| 75 | +// Object map options |
| 76 | +const selectOptions = { |
| 77 | + 0: 'good', |
| 78 | + 1: 'Bad', |
| 79 | + 2: 'unknown' |
| 80 | +}; |
| 81 | +
|
| 82 | +// Array options |
| 83 | +const selectOptionsArr = [{ |
| 84 | + value: 0, |
| 85 | + label: 'good' |
| 86 | +}, { |
| 87 | + value: 1, |
| 88 | + label: 'Bad' |
| 89 | +}, { |
| 90 | + value: 2, |
| 91 | + label: 'unknown' |
| 92 | +}]; |
| 93 | +
|
| 94 | +const columns1 = [..., { |
| 95 | + dataField: 'quality', |
| 96 | + text: 'Product Quailty', |
| 97 | + formatter: cell => selectOptions[cell], |
| 98 | + filter: selectFilter({ |
| 99 | + options: selectOptions |
| 100 | + }) |
| 101 | +}]; |
| 102 | +<BootstrapTable keyField='id' data={ products } columns={ columns1 } filter={ filterFactory() } /> |
| 103 | +
|
| 104 | +const columns2 = [..., { |
| 105 | + dataField: 'quality', |
| 106 | + text: 'Product Quailty', |
| 107 | + formatter: cell => selectOptionsArr.filter(opt => opt.value === cell)[0].label || '', |
| 108 | + filter: selectFilter({ |
| 109 | + options: selectOptionsArr |
| 110 | + }) |
| 111 | +}]; |
| 112 | +<BootstrapTable keyField='id' data={ products } columns={ columns2 } filter={ filterFactory() } /> |
| 113 | +
|
| 114 | +const columns3 = [..., { |
| 115 | + dataField: 'quality', |
| 116 | + text: 'Product Quailty', |
| 117 | + formatter: cell => selectOptionsArr.filter(opt => opt.value === cell)[0].label || '', |
| 118 | + filter: selectFilter({ |
| 119 | + options: () => selectOptionsArr |
| 120 | + }) |
| 121 | +}]; |
| 122 | +<BootstrapTable keyField='id' data={ products } columns={ columns3 } filter={ filterFactory() } /> |
| 123 | +`; |
| 124 | + |
| 125 | +export default () => ( |
| 126 | + <div> |
| 127 | + <h2>Options as an object</h2> |
| 128 | + <BootstrapTable |
| 129 | + keyField="id" |
| 130 | + data={ products } |
| 131 | + columns={ columns1 } |
| 132 | + filter={ filterFactory() } |
| 133 | + /> |
| 134 | + <h2>Options as an array</h2> |
| 135 | + <BootstrapTable |
| 136 | + keyField="id" |
| 137 | + data={ products } |
| 138 | + columns={ columns2 } |
| 139 | + filter={ filterFactory() } |
| 140 | + /> |
| 141 | + <h2>Options as a function which return an array</h2> |
| 142 | + <BootstrapTable |
| 143 | + keyField="id" |
| 144 | + data={ products } |
| 145 | + columns={ columns3 } |
| 146 | + filter={ filterFactory() } |
| 147 | + /> |
| 148 | + <Code>{ sourceCode }</Code> |
| 149 | + </div> |
| 150 | +); |
0 commit comments