Skip to content

Commit 4e76e13

Browse files
jdeniaujfremy
authored andcommitted
custom data accessor
1 parent a785907 commit 4e76e13

File tree

10 files changed

+86
-7
lines changed

10 files changed

+86
-7
lines changed

demo/js/demo.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ function addProducts(quantity) {
1313
price: 100 + i,
1414
supplierId: id+2,
1515
discount: "10%",
16-
categoryId: "catorage-"+id+6
16+
category: {
17+
id: "catorage-"+id+6,
18+
},
1719
});
1820
}
1921
}
@@ -92,7 +94,7 @@ React.render(
9294
<TableHeaderColumn dataField="price" width="100px" dataFormat={priceFormatter} editable={false}>Product Price</TableHeaderColumn>
9395
<TableHeaderColumn dataField="supplierId" editable={true}>Supplier ID</TableHeaderColumn>
9496
<TableHeaderColumn dataField="discount" editable={false}>Discount(Percentage)</TableHeaderColumn>
95-
<TableHeaderColumn dataField="categoryId" editable={true}>Category ID</TableHeaderColumn>
97+
<TableHeaderColumn dataField="categoryId" dataAccess={(item) => item.category.id} editable={true}>Category ID</TableHeaderColumn>
9698
</BootstrapTable>,
9799
document.getElementById("basic")
98100
);

examples/js/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const routes = (
1818
<Route path='getting-started' component={ GettingStarted }/>
1919
<Route path='examples'>
2020
<Route path='basic' component={ require('./basic/demo') } />
21+
<Route path='custom-data-access' component={ require('./custom-data-access/demo') } />
2122
<Route path='column' component={ require('./column/demo') } />
2223
<Route path='sort' component={ require('./sort/demo') } />
2324
<Route path='column-format' component={ require('./column-format/demo') } />

examples/js/components/App.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class App extends React.Component {
3636
const examples = [ {
3737
text: 'Basic Table',
3838
href: 'basic'
39+
}, {
40+
text: 'Custom Data Access',
41+
href: 'custom-data-access'
3942
}, {
4043
text: 'Work on Column',
4144
href: 'column'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint max-len: 0 */
2+
import React from 'react';
3+
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
4+
5+
6+
const products = [];
7+
8+
function addProducts(quantity) {
9+
const startId = products.length;
10+
for (let i = 0; i < quantity; i++) {
11+
const id = startId + i;
12+
products.push({
13+
id: id,
14+
name: 'Item name ' + id,
15+
price: 2100 + i,
16+
category: {
17+
id: 20 + i,
18+
name: 'Category name ' + i
19+
}
20+
});
21+
}
22+
}
23+
24+
addProducts(5);
25+
26+
function categoryName(product) {
27+
return product.category.name;
28+
}
29+
30+
export default class TrClassStringTable extends React.Component {
31+
render() {
32+
return (
33+
<BootstrapTable data={ products }>
34+
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
35+
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
36+
<TableHeaderColumn dataField='price' dataAccess={ categoryName }>Category name</TableHeaderColumn>
37+
</BootstrapTable>
38+
);
39+
}
40+
}
41+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint max-len: 0 */
2+
import React from 'react';
3+
import CustomDataAccessTable from './custom-data-access';
4+
5+
class Demo extends React.Component {
6+
render() {
7+
return (
8+
<div>
9+
<div className='col-md-offset-1 col-md-8'>
10+
<div className='panel panel-default'>
11+
<div className='panel-heading'>Use a custom function to access data</div>
12+
<div className='panel-body'>
13+
<h5>Source in /examples/js/custom-data-access/custom-data-access.js</h5>
14+
<CustomDataAccessTable />
15+
</div>
16+
</div>
17+
</div>
18+
</div>
19+
);
20+
}
21+
}
22+
23+
export default Demo;
24+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
},
6767
"dependencies": {
6868
"classnames": "^2.1.2",
69-
"react-toastr": "^2.3.1"
69+
"react-toastr": "2.4.0"
7070
},
7171
"peerDependencies": {
7272
"react": "^0.14.3 || ^15.0.0"

src/BootstrapTable.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class BootstrapTable extends Component {
118118
return React.Children.map(children, (column, i) => {
119119
return {
120120
name: column.props.dataField,
121+
dataAccess: column.props.dataAccess,
121122
align: column.props.dataAlign,
122123
sort: column.props.dataSort,
123124
format: column.props.dataFormat,

src/TableBody.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ class TableBody extends Component {
3232

3333
const tableRows = this.props.data.map(function(data, r) {
3434
const tableColumns = this.props.columns.map(function(column, i) {
35-
const fieldValue = data[column.name];
35+
const fieldValue = typeof column.dataAccess === 'function' ?
36+
column.dataAccess(data) :
37+
data[column.name];
38+
3639
if (this.editing &&
3740
column.name !== this.props.keyField && // Key field can't be edit
3841
column.editable && // column is editable? default is true, user can set it false

src/TableHeaderColumn.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ TableHeaderColumn.propTypes = {
130130
headerAlign: PropTypes.string,
131131
dataSort: PropTypes.bool,
132132
onSort: PropTypes.func,
133-
dataFormat: PropTypes.func,
134133
csvFormat: PropTypes.func,
135134
csvHeader: PropTypes.string,
135+
dataAccess: PropTypes.func,
136+
dataFormat: PropTypes.func,
136137
isKey: PropTypes.bool,
137138
editable: PropTypes.any,
138139
hidden: PropTypes.bool,
@@ -168,9 +169,10 @@ TableHeaderColumn.defaultProps = {
168169
dataAlign: 'left',
169170
headerAlign: undefined,
170171
dataSort: false,
171-
dataFormat: undefined,
172172
csvFormat: undefined,
173173
csvHeader: undefined,
174+
dataAccess: undefined,
175+
dataFormat: undefined,
174176
isKey: false,
175177
editable: true,
176178
onSort: undefined,

src/store/TableDataStore.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ export class TableDataStore {
386386
let valid = true;
387387
let filterVal;
388388
for (const key in filterObj) {
389-
let targetVal = row[key];
389+
let targetVal = typeof this.colInfos[key].dataAccess === 'function' ?
390+
this.colInfos[key].dataAccess(row) :
391+
row[key];
390392
if (targetVal === null) return false;
391393

392394
switch (filterObj[key].type) {

0 commit comments

Comments
 (0)