mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
* beautify code block with google-code-prettify * prettyPrint when componentDidMount * css style for code block * skip rule 'no-unresolved' for eslint * conflict with webpack resolve path * refactor all code block in example folder with component <Code /> * refactor scss folder structure * specify the responsibility for each stylesheet with file name * load local color themes, tomorrow, for google code prettify * re-select demo color and save into variable * unify the color system for storybook
87 lines
1.4 KiB
JavaScript
87 lines
1.4 KiB
JavaScript
import React from 'react';
|
|
|
|
import { BootstrapTable } from 'react-bootstrap-table2';
|
|
import Code from 'common/codeBlock';
|
|
|
|
const products = [];
|
|
|
|
function addProducts(quantity) {
|
|
const startId = products.length;
|
|
for (let i = 0; i < quantity; i += 1) {
|
|
const id = startId + i;
|
|
products.push({
|
|
id,
|
|
name: `Item name ${id}`,
|
|
price: 2100 + i,
|
|
onSale: Math.random() >= 0.5
|
|
});
|
|
}
|
|
}
|
|
|
|
addProducts(5);
|
|
|
|
function priceFormatter(cell, row) {
|
|
if (row.onSale) {
|
|
return (
|
|
<span><strong style={ { color: 'red' } }>$ { cell } NTD(Sales!!)</strong></span>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<span>$ { cell } NTD</span>
|
|
);
|
|
}
|
|
|
|
const columns = [{
|
|
dataField: 'id',
|
|
text: 'Product ID'
|
|
}, {
|
|
dataField: 'name',
|
|
text: 'Product Name'
|
|
}, {
|
|
dataField: 'price',
|
|
text: 'Product Price',
|
|
formatter: priceFormatter
|
|
}];
|
|
|
|
const sourceCode = `\
|
|
function priceFormatter(cell, row) {
|
|
if (row.onSale) {
|
|
return (
|
|
<span>
|
|
<strong style={ { color: 'red' } }>$ { cell } NTD(Sales!!)</strong>
|
|
</span>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<span>$ { cell } NTD</span>
|
|
);
|
|
}
|
|
|
|
const columns = [
|
|
// omit...
|
|
{
|
|
dataField: 'price',
|
|
text: 'Product Price',
|
|
formatter: priceFormatter
|
|
}];
|
|
|
|
<BootstrapTable
|
|
keyField="id"
|
|
data={ products }
|
|
columns={ columns }
|
|
/>
|
|
`;
|
|
|
|
export default () => (
|
|
<div>
|
|
<BootstrapTable
|
|
keyField="id"
|
|
data={ products }
|
|
columns={ columns }
|
|
/>
|
|
<Code>{ sourceCode }</Code>
|
|
</div>
|
|
);
|