File tree Expand file tree Collapse file tree 10 files changed +86
-7
lines changed Expand file tree Collapse file tree 10 files changed +86
-7
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,9 @@ function addProducts(quantity) {
13
13
price : 100 + i ,
14
14
supplierId : id + 2 ,
15
15
discount : "10%" ,
16
- categoryId : "catorage-" + id + 6
16
+ category : {
17
+ id : "catorage-" + id + 6 ,
18
+ } ,
17
19
} ) ;
18
20
}
19
21
}
@@ -92,7 +94,7 @@ React.render(
92
94
< TableHeaderColumn dataField = "price" width = "100px" dataFormat = { priceFormatter } editable = { false } > Product Price</ TableHeaderColumn >
93
95
< TableHeaderColumn dataField = "supplierId" editable = { true } > Supplier ID</ TableHeaderColumn >
94
96
< 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 >
96
98
</ BootstrapTable > ,
97
99
document . getElementById ( "basic" )
98
100
) ;
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ const routes = (
18
18
< Route path = 'getting-started' component = { GettingStarted } />
19
19
< Route path = 'examples' >
20
20
< Route path = 'basic' component = { require ( './basic/demo' ) } />
21
+ < Route path = 'custom-data-access' component = { require ( './custom-data-access/demo' ) } />
21
22
< Route path = 'column' component = { require ( './column/demo' ) } />
22
23
< Route path = 'sort' component = { require ( './sort/demo' ) } />
23
24
< Route path = 'column-format' component = { require ( './column-format/demo' ) } />
Original file line number Diff line number Diff line change @@ -36,6 +36,9 @@ class App extends React.Component {
36
36
const examples = [ {
37
37
text : 'Basic Table' ,
38
38
href : 'basic'
39
+ } , {
40
+ text : 'Custom Data Access' ,
41
+ href : 'custom-data-access'
39
42
} , {
40
43
text : 'Work on Column' ,
41
44
href : 'column'
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change 66
66
},
67
67
"dependencies" : {
68
68
"classnames" : " ^2.1.2" ,
69
- "react-toastr" : " ^2.3.1 "
69
+ "react-toastr" : " 2.4.0 "
70
70
},
71
71
"peerDependencies" : {
72
72
"react" : " ^0.14.3 || ^15.0.0"
Original file line number Diff line number Diff line change @@ -118,6 +118,7 @@ class BootstrapTable extends Component {
118
118
return React . Children . map ( children , ( column , i ) => {
119
119
return {
120
120
name : column . props . dataField ,
121
+ dataAccess : column . props . dataAccess ,
121
122
align : column . props . dataAlign ,
122
123
sort : column . props . dataSort ,
123
124
format : column . props . dataFormat ,
Original file line number Diff line number Diff line change @@ -32,7 +32,10 @@ class TableBody extends Component {
32
32
33
33
const tableRows = this . props . data . map ( function ( data , r ) {
34
34
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
+
36
39
if ( this . editing &&
37
40
column . name !== this . props . keyField && // Key field can't be edit
38
41
column . editable && // column is editable? default is true, user can set it false
Original file line number Diff line number Diff line change @@ -130,9 +130,10 @@ TableHeaderColumn.propTypes = {
130
130
headerAlign : PropTypes . string ,
131
131
dataSort : PropTypes . bool ,
132
132
onSort : PropTypes . func ,
133
- dataFormat : PropTypes . func ,
134
133
csvFormat : PropTypes . func ,
135
134
csvHeader : PropTypes . string ,
135
+ dataAccess : PropTypes . func ,
136
+ dataFormat : PropTypes . func ,
136
137
isKey : PropTypes . bool ,
137
138
editable : PropTypes . any ,
138
139
hidden : PropTypes . bool ,
@@ -168,9 +169,10 @@ TableHeaderColumn.defaultProps = {
168
169
dataAlign : 'left' ,
169
170
headerAlign : undefined ,
170
171
dataSort : false ,
171
- dataFormat : undefined ,
172
172
csvFormat : undefined ,
173
173
csvHeader : undefined ,
174
+ dataAccess : undefined ,
175
+ dataFormat : undefined ,
174
176
isKey : false ,
175
177
editable : true ,
176
178
onSort : undefined ,
Original file line number Diff line number Diff line change @@ -386,7 +386,9 @@ export class TableDataStore {
386
386
let valid = true ;
387
387
let filterVal ;
388
388
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 ] ;
390
392
if ( targetVal === null ) return false ;
391
393
392
394
switch ( filterObj [ key ] . type ) {
You can’t perform that action at this time.
0 commit comments