@@ -6,35 +6,35 @@ import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
6
6
const products = [ ] ;
7
7
8
8
function addProducts ( quantity ) {
9
+ let value ;
9
10
const startId = products . length ;
11
+ let total = 0 ;
10
12
for ( let i = 0 ; i < quantity ; i ++ ) {
11
13
const id = startId + i ;
12
- if ( i === quantity - 1 ) {
13
- products . push ( {
14
- id : id ,
15
- name : 'Footer ' ,
16
- price : 2100 + Math . floor ( Math . random ( ) * 100 ) + 1 ,
17
- isFooter : true
18
- } ) ;
19
- } else {
20
- products . push ( {
21
- id : id ,
22
- name : 'Item name ' + id ,
23
- price : 2100 + Math . floor ( Math . random ( ) * 100 ) + 1 ,
24
- isFooter : false
25
- } ) ;
26
- }
14
+ value = 2100 + Math . floor ( Math . random ( ) * 100 ) + 1 ;
15
+ products . push ( {
16
+ id : id ,
17
+ name : 'Item name ' + id ,
18
+ cost : value
19
+ } ) ;
20
+ total += value ;
27
21
}
22
+ /* add footer row with total calculations */
23
+ products . push ( {
24
+ id : products . length ,
25
+ name : 'Total' ,
26
+ cost : total
27
+ } ) ;
28
28
}
29
29
30
30
addProducts ( 20 ) ;
31
31
32
32
function revertSortFunc ( a , b , order ) { // order is desc or asc
33
33
34
34
if ( order === 'desc' ) {
35
- return a . price - b . price ;
35
+ return a . cost - b . cost ;
36
36
} else {
37
- return b . price - a . price ;
37
+ return b . cost - a . cost ;
38
38
}
39
39
}
40
40
@@ -55,7 +55,7 @@ export default class CustomSortTableHasFooter extends React.Component {
55
55
< BootstrapTable data = { products } options = { options } >
56
56
< TableHeaderColumn dataField = 'id' isKey = { true } dataSort = { true } > Product ID</ TableHeaderColumn >
57
57
< TableHeaderColumn dataField = 'name' dataSort = { true } sortFunc = { sortStrings } > Product Name</ TableHeaderColumn >
58
- < TableHeaderColumn dataField = 'price ' dataSort = { true } sortFunc = { revertSortFunc } > Product Price </ TableHeaderColumn >
58
+ < TableHeaderColumn dataField = 'cost ' dataSort = { true } sortFunc = { revertSortFunc } > Product Cost </ TableHeaderColumn >
59
59
</ BootstrapTable >
60
60
) ;
61
61
}
0 commit comments