Skip to content

Commit ff4f833

Browse files
committed
Merge branch 'master' into v4.0.0-dev
2 parents 4ec5e95 + 74e37d3 commit ff4f833

File tree

6 files changed

+152
-12
lines changed

6 files changed

+152
-12
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* eslint max-len: 0 */
2+
import React from 'react';
3+
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
4+
5+
const products = [];
6+
7+
function addProducts(quantity) {
8+
const startId = products.length;
9+
for (let i = 0; i < quantity; i++) {
10+
const id = startId + i;
11+
if (i < 3) {
12+
products.push({
13+
id: id,
14+
name: 'Item name ' + id,
15+
price: 2100 + i,
16+
expand: [ {
17+
fieldA: 'test1',
18+
fieldB: (i + 1) * 99,
19+
fieldC: (i + 1) * Math.random() * 100,
20+
fieldD: '123eedd' + i
21+
}, {
22+
fieldA: 'test2',
23+
fieldB: i * 99,
24+
fieldC: i * Math.random() * 100,
25+
fieldD: '123eedd' + i
26+
} ]
27+
});
28+
} else {
29+
products.push({
30+
id: id,
31+
name: 'Item name ' + id,
32+
price: 2100 + i
33+
});
34+
}
35+
}
36+
}
37+
addProducts(5);
38+
39+
class BSTable extends React.Component {
40+
render() {
41+
if (this.props.data) {
42+
return (
43+
<BootstrapTable data={ this.props.data }>
44+
<TableHeaderColumn dataField='fieldA' isKey={ true }>Field A</TableHeaderColumn>
45+
<TableHeaderColumn dataField='fieldB'>Field B</TableHeaderColumn>
46+
<TableHeaderColumn dataField='fieldC'>Field C</TableHeaderColumn>
47+
<TableHeaderColumn dataField='fieldD'>Field D</TableHeaderColumn>
48+
</BootstrapTable>);
49+
} else {
50+
return (<p>?</p>);
51+
}
52+
}
53+
}
54+
55+
export default class CustomExpandClass extends React.Component {
56+
constructor(props) {
57+
super(props);
58+
}
59+
60+
isExpandableRow(row) {
61+
if (row.id < 3) return true;
62+
else return false;
63+
}
64+
65+
expandComponent(row) {
66+
return (
67+
<BSTable data={ row.expand } />
68+
);
69+
}
70+
71+
render() {
72+
const options = {
73+
expandBodyClass: function(row, rowIndex) {
74+
if (rowIndex > 1) {
75+
return 'custom-expand-body-1';
76+
} else {
77+
return 'custom-expand-body-0';
78+
}
79+
},
80+
expandParentClass: 'custom-expand-parent' // expandParentClass also accept callback function
81+
};
82+
return (
83+
<BootstrapTable data={ products }
84+
options={ options }
85+
expandableRow={ this.isExpandableRow }
86+
expandComponent={ this.expandComponent }
87+
search>
88+
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
89+
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
90+
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
91+
</BootstrapTable>
92+
);
93+
}
94+
}

examples/js/expandRow/demo.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint max-len: 0 */
2+
require('./style.css');
23
import React from 'react';
34
import ExpandRow from './expandRow';
45
import SingleExpandRow from './single-expanding';
@@ -7,6 +8,7 @@ import ManageExpandExternal from './manage-expanding';
78
import ExpandWithSelection from './expand-row-with-selection';
89
import ExpandWithCellEdit from './expand-row-with-cellEdit';
910
import ExpandIndicator from './expand-indicator';
11+
import CustomExpandClassName from './custom-expand-class';
1012
import CustomExpandIndicator from './custom-expand-indicator';
1113
import renderLinks from '../utils';
1214

@@ -29,6 +31,10 @@ class Demo extends React.Component {
2931
{ renderLinks('expandRow/expand-indicator.js') }
3032
<ExpandIndicator/>
3133
</Panel>
34+
<Panel header={ 'Custom Expand Row ClassName' }>
35+
{ renderLinks('expandRow/custom-expand-class.js') }
36+
<CustomExpandClassName/>
37+
</Panel>
3238
<Panel header={ 'Custom Row Expand Indicator' }>
3339
{ renderLinks('expandRow/custom-expand-indicator.js') }
3440
<CustomExpandIndicator/>

examples/js/expandRow/style.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.custom-expand-parent {
2+
background-color: salmon;
3+
}
4+
5+
.custom-expand-body-1 {
6+
font-size: 8px;
7+
color: lawngreen;
8+
background-color: darkkhaki;
9+
}
10+
11+
.custom-expand-body-0 {
12+
font-size: 16px;
13+
font-weight: bold;
14+
background-color: springgreen;
15+
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
16+
}

src/BootstrapTable.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ class BootstrapTable extends Component {
420420
expandableRow={ this.props.expandableRow }
421421
expandRowBgColor={ this.props.options.expandRowBgColor }
422422
expandBy={ this.props.options.expandBy || Const.EXPAND_BY_ROW }
423+
expandBodyClass={ this.props.options.expandBodyClass }
424+
expandParentClass={ this.props.options.expandParentClass }
423425
columns={ columns }
424426
trClassName={ this.props.trClassName }
425427
striped={ this.props.striped }
@@ -1275,13 +1277,17 @@ class BootstrapTable extends Component {
12751277
}
12761278
}
12771279
} else {
1278-
// debugger;
1279-
React.Children.forEach(this.props.children, (child, i) => {
1280-
if (child && child.props.width) {
1281-
header[i].style.width = `${child.props.width}px`;
1282-
header[i].style.minWidth = `${child.props.width}px`;
1280+
for (const i in bodyHeader) {
1281+
if (bodyHeader.hasOwnProperty(i)) {
1282+
const child = bodyHeader[i];
1283+
if (child.style.width) {
1284+
header[i].style.width = child.style.width;
1285+
}
1286+
if (child.style.minWidth) {
1287+
header[i].style.minWidth = child.style.minWidth;
1288+
}
12831289
}
1284-
});
1290+
}
12851291
}
12861292
this.isVerticalScroll = isScroll;
12871293
}
@@ -1479,6 +1485,8 @@ BootstrapTable.propTypes = {
14791485
expanding: PropTypes.array,
14801486
onExpand: PropTypes.func,
14811487
onlyOneExpanding: PropTypes.bool,
1488+
expandBodyClass: PropTypes.oneOfType([ PropTypes.string, PropTypes.func ]),
1489+
expandParentClass: PropTypes.oneOfType([ PropTypes.string, PropTypes.func ]),
14821490
beforeShowError: PropTypes.func,
14831491
printToolBar: PropTypes.bool,
14841492
insertFailIndicator: PropTypes.string
@@ -1628,6 +1636,8 @@ BootstrapTable.defaultProps = {
16281636
expanding: [],
16291637
onExpand: undefined,
16301638
onlyOneExpanding: false,
1639+
expandBodyClass: null,
1640+
expandParentClass: null,
16311641
beforeShowError: undefined,
16321642
printToolBar: true,
16331643
insertFailIndicator: Const.INSERT_FAIL_INDICATOR

src/ExpandComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ExpandComponent extends Component {
1111
style: {
1212
backgroundColor: this.props.bgColor
1313
},
14-
className: classSet(className)
14+
className: this.props.hidden ? null : classSet(className)
1515
};
1616
return (
1717
<tr hidden={ this.props.hidden } width={ this.props.width } { ...trCss }>

src/TableBody.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,19 @@ class TableBody extends Component {
148148
this.props.expanding.indexOf(key) > -1,
149149
ExpandColumnCustomComponent, r, data
150150
);
151+
const haveExpandContent = this.props.expandableRow && this.props.expandableRow(data);
152+
const isExpanding = haveExpandContent && this.props.expanding.indexOf(key) > -1;
153+
151154
// add by bluespring for className customize
152155
let trClassName = this.props.trClassName;
153156
if (isFun(this.props.trClassName)) {
154157
trClassName = this.props.trClassName(data, r);
155158
}
159+
if (isExpanding && this.props.expandParentClass) {
160+
trClassName += isFun(this.props.expandParentClass) ?
161+
this.props.expandParentClass(data, r) :
162+
this.props.expandParentClass;
163+
}
156164
const result = [ <TableRow isSelected={ selected } key={ key } className={ trClassName }
157165
index={ r }
158166
row={ data }
@@ -175,14 +183,17 @@ class TableBody extends Component {
175183
{ tableColumns }
176184
</TableRow> ];
177185

178-
if (this.props.expandableRow && this.props.expandableRow(data)) {
186+
if (haveExpandContent) {
187+
const expandBodyClass = isFun(this.props.expandBodyClass) ?
188+
this.props.expandBodyClass(data, r) :
189+
this.props.expandBodyClass;
179190
result.push(
180191
<ExpandComponent
181192
key={ key + '-expand' }
182193
row={ data }
183-
className={ trClassName }
194+
className={ expandBodyClass }
184195
bgColor={ this.props.expandRowBgColor || this.props.selectRow.bgColor || undefined }
185-
hidden={ !(this.props.expanding.indexOf(key) > -1) }
196+
hidden={ !isExpanding }
186197
colSpan={ expandColSpan }
187198
width={ "100%" }>
188199
{ this.props.expandComponent(data) }
@@ -304,12 +315,13 @@ class TableBody extends Component {
304315
expandBy,
305316
expandableRow,
306317
selectRow: {
307-
clickToExpand
318+
clickToExpand,
319+
hideSelectColumn
308320
},
309321
onlyOneExpanding
310322
} = this.props;
311323
const selectRowAndExpand = this._isSelectRowDefined() && !clickToExpand ? false : true;
312-
columnIndex = this._isSelectRowDefined() ? columnIndex - 1 : columnIndex;
324+
columnIndex = this._isSelectRowDefined() && !hideSelectColumn ? columnIndex - 1 : columnIndex;
313325
columnIndex = this._isExpandColumnVisible() ? columnIndex - 1 : columnIndex;
314326
if (expandableRow &&
315327
selectRowAndExpand &&
@@ -503,6 +515,8 @@ TableBody.propTypes = {
503515
expandBy: PropTypes.string,
504516
expanding: PropTypes.array,
505517
onExpand: PropTypes.func,
518+
expandBodyClass: PropTypes.oneOfType([ PropTypes.string, PropTypes.func ]),
519+
expandParentClass: PropTypes.oneOfType([ PropTypes.string, PropTypes.func ]),
506520
onlyOneExpanding: PropTypes.bool,
507521
beforeShowError: PropTypes.func,
508522
keyBoardNav: PropTypes.oneOfType([ PropTypes.bool, PropTypes.object ]),

0 commit comments

Comments
 (0)