|
| 1 | +/* eslint prefer-template: 0 */ |
| 2 | +import React from 'react'; |
| 3 | +import BootstrapTable from 'react-bootstrap-table-next'; |
| 4 | +import cellEditFactory, { Type } from 'react-bootstrap-table2-editor'; |
| 5 | +import Code from 'components/common/code-block'; |
| 6 | +import { stockGenerator } from 'utils/common'; |
| 7 | + |
| 8 | +const products = stockGenerator(); |
| 9 | + |
| 10 | +const columns = [{ |
| 11 | + dataField: 'id', |
| 12 | + text: 'Stock ID' |
| 13 | +}, { |
| 14 | + dataField: 'name', |
| 15 | + text: 'Stock Name' |
| 16 | +}, { |
| 17 | + dataField: 'price', |
| 18 | + text: 'Price', |
| 19 | + type: 'number' |
| 20 | +}, { |
| 21 | + dataField: 'visible', |
| 22 | + text: 'Visible?', |
| 23 | + type: 'bool', |
| 24 | + editor: { |
| 25 | + type: Type.CHECKBOX, |
| 26 | + value: 'true:false' |
| 27 | + } |
| 28 | +}, { |
| 29 | + dataField: 'inStockDate', |
| 30 | + text: 'Stock Date', |
| 31 | + type: 'date', |
| 32 | + formatter: (cell) => { |
| 33 | + let dateObj = cell; |
| 34 | + if (typeof cell !== 'object') { |
| 35 | + dateObj = new Date(cell); |
| 36 | + } |
| 37 | + return `${('0' + dateObj.getUTCDate()).slice(-2)}/${('0' + (dateObj.getUTCMonth() + 1)).slice(-2)}/${dateObj.getUTCFullYear()}`; |
| 38 | + }, |
| 39 | + editor: { |
| 40 | + type: Type.DATE |
| 41 | + } |
| 42 | +}]; |
| 43 | + |
| 44 | +const sourceCode = `\ |
| 45 | +import BootstrapTable from 'react-bootstrap-table-next'; |
| 46 | +import cellEditFactory from 'react-bootstrap-table2-editor'; |
| 47 | +
|
| 48 | +const columns = [{ |
| 49 | + dataField: 'id', |
| 50 | + text: 'Stock ID' |
| 51 | +}, { |
| 52 | + dataField: 'name', |
| 53 | + text: 'Stock Name' |
| 54 | +}, { |
| 55 | + dataField: 'price', |
| 56 | + text: 'Price', |
| 57 | + type: 'number' |
| 58 | +}, { |
| 59 | + dataField: 'visible', |
| 60 | + text: 'Visible?', |
| 61 | + type: 'bool', |
| 62 | + editor: { |
| 63 | + type: Type.CHECKBOX, |
| 64 | + value: 'true:false' |
| 65 | + } |
| 66 | +}, { |
| 67 | + dataField: 'inStockDate', |
| 68 | + text: 'Stock Date', |
| 69 | + type: 'date', |
| 70 | + formatter: (cell) => { |
| 71 | + let dateObj = cell; |
| 72 | + if (typeof cell !== 'object') { |
| 73 | + dateObj = new Date(cell); |
| 74 | + } |
| 75 | + return \`$\{('0' + dateObj.getUTCDate()).slice(-2)}/$\{('0' + (dateObj.getUTCMonth() + 1)).slice(-2)}/$\{dateObj.getUTCFullYear()}\`; |
| 76 | + }, |
| 77 | + editor: { |
| 78 | + type: Type.DATE |
| 79 | + } |
| 80 | +}]; |
| 81 | +
|
| 82 | +function afterSaveCell(oldValue, newValue) { |
| 83 | + console.log('--after save cell--'); |
| 84 | + console.log('New Value was apply as'); |
| 85 | + console.log(newValue); |
| 86 | + console.log(\`and the type is $\{typeof newValue}\`); |
| 87 | +} |
| 88 | +
|
| 89 | +<BootstrapTable |
| 90 | + keyField="id" |
| 91 | + data={ products } |
| 92 | + columns={ columns } |
| 93 | + cellEdit={ cellEditFactory({ |
| 94 | + mode: 'click', |
| 95 | + blurToSave: true, |
| 96 | + afterSaveCell |
| 97 | + }) } |
| 98 | +/> |
| 99 | +`; |
| 100 | + |
| 101 | +function afterSaveCell(oldValue, newValue) { |
| 102 | + console.log('--after save cell--'); |
| 103 | + console.log('New Value was apply as'); |
| 104 | + console.log(newValue); |
| 105 | + console.log(`and the type is ${typeof newValue}`); |
| 106 | +} |
| 107 | + |
| 108 | +export default () => ( |
| 109 | + <div> |
| 110 | + <h3>Save Cell Value with Specified Data Type</h3> |
| 111 | + <BootstrapTable |
| 112 | + keyField="id" |
| 113 | + data={ products } |
| 114 | + columns={ columns } |
| 115 | + cellEdit={ cellEditFactory({ |
| 116 | + mode: 'click', |
| 117 | + blurToSave: true, |
| 118 | + afterSaveCell |
| 119 | + }) } |
| 120 | + /> |
| 121 | + <Code>{ sourceCode }</Code> |
| 122 | + </div> |
| 123 | +); |
0 commit comments