Skip to content

Commit 1c31aab

Browse files
committed
enhance expand row - added indication about expanded rows
1 parent 9d4f126 commit 1c31aab

File tree

9 files changed

+117
-13
lines changed

9 files changed

+117
-13
lines changed

css/react-bootstrap-table.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@
173173
animation-duration: .3s;
174174
}
175175

176+
td.react-bs-table-expand-cell {
177+
cursor: pointer;
178+
}
179+
176180
@keyframes shake {
177181
from, to {
178182
transform: translate3d(0, 0, 0);

examples/js/expandRow/demo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Demo extends React.Component {
2828
{ renderLinks('expandRow/expand-row-by-column.js') }
2929
<ManageExpandExternal/>
3030
</Panel>
31-
<Panel header={ 'Expand Row with Selection' }>
31+
<Panel header={ 'Expand Row with Selection - Keep select column first' }>
3232
<span>You can enable/disable the expanding if you click row to select,
3333
configure <code>clickToExpand</code> in <code>selectRow</code> props, default is false</span>
3434
{ renderLinks('expandRow/expand-row-with-selection.js') }

examples/js/expandRow/expand-row-by-column.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export default class ExpandRow extends React.Component {
7878
options={ options }
7979
expandableRow={ this.isExpandableRow }
8080
expandComponent={ this.expandComponent }
81+
expandColumnOptions={ { expandColumnVisible: true } }
8182
search>
8283
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
8384
<TableHeaderColumn dataField='name' expandable={ false }>Product Name</TableHeaderColumn>

examples/js/expandRow/expand-row-with-selection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export default class ExpandRow extends React.Component {
8282
options={ options }
8383
expandableRow={ this.isExpandableRow }
8484
expandComponent={ this.expandComponent }
85+
expandColumnOptions={ { expandColumnVisible: true, expandColumnBeforeSelectColumn: false } }
8586
selectRow={ selectRow }>
8687
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
8788
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>

examples/js/expandRow/expandRow.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ export default class ExpandRow extends React.Component {
6868
);
6969
}
7070

71+
expandColumnComponent({ isExpandableRow, isExpanded }) {
72+
let content = '';
73+
74+
if (isExpandableRow) {
75+
content = (isExpanded ? '(-)' : '(+)' );
76+
} else {
77+
content = ' ';
78+
}
79+
return (
80+
<div> { content } </div>
81+
);
82+
}
83+
7184
render() {
7285
const options = {
7386
expandRowBgColor: 'rgb(242, 255, 163)'
@@ -77,6 +90,7 @@ export default class ExpandRow extends React.Component {
7790
options={ options }
7891
expandableRow={ this.isExpandableRow }
7992
expandComponent={ this.expandComponent }
93+
expandColumnOptions={ { expandColumnVisible: true, expandColumnComponent: this.expandColumnComponent } }
8094
search>
8195
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
8296
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>

src/BootstrapTable.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,11 @@ class BootstrapTable extends Component {
326326
const toolBar = this.renderToolBar();
327327
const tableFilter = this.renderTableFilter(columns);
328328
const isSelectAll = this.isSelectAll();
329-
const colGroups = Util.renderColGroup(columns, this.props.selectRow);
329+
const expandColumnOptions = this.props.expandColumnOptions;
330+
if (typeof expandColumnOptions.expandColumnBeforeSelectColumn === 'undefined') {
331+
expandColumnOptions.expandColumnBeforeSelectColumn = true;
332+
}
333+
const colGroups = Util.renderColGroup(columns, this.props.selectRow, expandColumnOptions);
330334
let sortIndicator = this.props.options.sortIndicator;
331335
if (typeof this.props.options.sortIndicator === 'undefined') sortIndicator = true;
332336
return (
@@ -355,7 +359,10 @@ class BootstrapTable extends Component {
355359
condensed={ this.props.condensed }
356360
isFiltered={ this.filter ? true : false }
357361
isSelectAll={ isSelectAll }
358-
reset={ this.state.reset }>
362+
reset={ this.state.reset }
363+
expandColumnVisible={ expandColumnOptions.expandColumnVisible }
364+
expandColumnComponent={ expandColumnOptions.expandColumnComponent }
365+
expandColumnBeforeSelectColumn={ expandColumnOptions.expandColumnBeforeSelectColumn }>
359366
{ this.props.children }
360367
</TableHeader>
361368
<TableBody ref='body'
@@ -375,6 +382,7 @@ class BootstrapTable extends Component {
375382
keyField={ this.store.getKeyField() }
376383
condensed={ this.props.condensed }
377384
selectRow={ this.props.selectRow }
385+
expandColumnOptions={ this.props.expandColumnOptions }
378386
cellEdit={ this.props.cellEdit }
379387
selectedRowKeys={ this.state.selectedRowKeys }
380388
onRowClick={ this.handleRowClick }
@@ -1233,12 +1241,22 @@ BootstrapTable.propTypes = {
12331241
csvFileName: PropTypes.oneOfType([ PropTypes.string, PropTypes.func ]),
12341242
ignoreSinglePage: PropTypes.bool,
12351243
expandableRow: PropTypes.func,
1236-
expandComponent: PropTypes.func
1244+
expandComponent: PropTypes.func,
1245+
expandColumnOptions: PropTypes.shape({
1246+
expandColumnVisible: PropTypes.bool,
1247+
expandColumnComponent: PropTypes.func,
1248+
expandColumnBeforeSelectColumn: PropTypes.bool
1249+
})
12371250
};
12381251
BootstrapTable.defaultProps = {
12391252
scrollTop: undefined,
12401253
expandComponent: undefined,
12411254
expandableRow: undefined,
1255+
expandColumnOptions: {
1256+
expandColumnVisible: false,
1257+
expandColumnComponent: undefined,
1258+
expandColumnBeforeSelectColumn: true
1259+
},
12421260
height: '100%',
12431261
maxHeight: undefined,
12441262
striped: false,

src/TableBody.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ class TableBody extends Component {
3131
const noneditableRows = (cellEdit.nonEditableRows && cellEdit.nonEditableRows()) || [];
3232
const unselectable = this.props.selectRow.unselectable || [];
3333
const isSelectRowDefined = this._isSelectRowDefined();
34-
const tableHeader = Utils.renderColGroup(this.props.columns, this.props.selectRow, 'header');
34+
const tableHeader = Utils.renderColGroup(this.props.columns,
35+
this.props.selectRow, this.props.expandColumnOptions);
3536
const inputType = this.props.selectRow.mode === Const.ROW_SELECT_SINGLE ? 'radio' : 'checkbox';
3637
const CustomComponent = this.props.selectRow.customComponent;
38+
const ExpandColumnCustomComponent = this.props.expandColumnOptions.expandColumnComponent;
3739
let expandColSpan = this.props.columns.filter(col => !col.hidden).length;
3840
if (isSelectRowDefined && !this.props.selectRow.hideSelectColumn) {
3941
expandColSpan += 1;
4042
}
43+
if (this.props.expandColumnOptions.expandColumnVisible) {
44+
expandColSpan += 1;
45+
}
4146

4247
const tableRows = this.props.data.map(function(data, r) {
4348
const tableColumns = this.props.columns.map(function(column, i) {
@@ -118,6 +123,11 @@ class TableBody extends Component {
118123
const selected = this.props.selectedRowKeys.indexOf(key) !== -1;
119124
const selectRowColumn = isSelectRowDefined && !this.props.selectRow.hideSelectColumn ?
120125
this.renderSelectRowColumn(selected, inputType, disable, CustomComponent, r, data) : null;
126+
const expandedRowColumn = this.renderExpandRowColumn(
127+
this.props.expandableRow && this.props.expandableRow(data),
128+
this.props.expanding.indexOf(key) > -1,
129+
ExpandColumnCustomComponent, r, data
130+
);
121131
// add by bluespring for className customize
122132
let trClassName = this.props.trClassName;
123133
if (isFun(this.props.trClassName)) {
@@ -134,7 +144,13 @@ class TableBody extends Component {
134144
onSelectRow={ this.handleSelectRow }
135145
onExpandRow={ this.handleClickCell }
136146
unselectableRow={ disable }>
147+
{ this.props.expandColumnOptions.expandColumnVisible &&
148+
this.props.expandColumnOptions.expandColumnBeforeSelectColumn &&
149+
expandedRowColumn }
137150
{ selectRowColumn }
151+
{ this.props.expandColumnOptions.expandColumnVisible &&
152+
!this.props.expandColumnOptions.expandColumnBeforeSelectColumn &&
153+
expandedRowColumn }
138154
{ tableColumns }
139155
</TableRow> ];
140156

@@ -232,6 +248,7 @@ class TableBody extends Component {
232248
} = this.props;
233249
const selectRowAndExpand = this._isSelectRowDefined() && !clickToExpand ? false : true;
234250
columnIndex = this._isSelectRowDefined() ? columnIndex - 1 : columnIndex;
251+
columnIndex = this._isExpandColumnVisible() ? columnIndex - 1 : columnIndex;
235252

236253
if (expandableRow &&
237254
selectRowAndExpand &&
@@ -309,11 +326,34 @@ class TableBody extends Component {
309326
);
310327
}
311328

329+
renderExpandRowColumn(isExpandableRow, isExpanded, CustomComponent, rowIndex = null) {
330+
let content = null;
331+
if (CustomComponent) {
332+
content = (<CustomComponent isExpandableRow={ isExpandableRow } isExpanded={ isExpanded } />);
333+
} else if (isExpandableRow) {
334+
content = (isExpanded ? '-' : '+' );
335+
} else {
336+
content = ' ';
337+
}
338+
339+
return (
340+
<td
341+
className='react-bs-table-expand-cell'
342+
onClick={ () => this.handleClickCell(rowIndex + 1) }>
343+
{ content }
344+
</td>
345+
);
346+
}
347+
312348
_isSelectRowDefined() {
313349
return this.props.selectRow.mode === Const.ROW_SELECT_SINGLE ||
314350
this.props.selectRow.mode === Const.ROW_SELECT_MULTI;
315351
}
316352

353+
_isExpandColumnVisible() {
354+
return this.props.expandColumnOptions.expandColumnVisible;
355+
}
356+
317357
getHeaderColGrouop = () => {
318358
return this.refs.header.childNodes;
319359
}

src/TableHeader.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,18 @@ class TableHeader extends Component {
5151
const rows = [];
5252
let rowKey = 0;
5353

54-
if (!this.props.hideSelectColumn) {
55-
rows[0] = [ this.renderSelectRowHeader(rowCount + 1, rowKey++) ];
56-
}
57-
54+
rows[0] = [];
55+
rows[0].push( [
56+
this.props.expandColumnVisible &&
57+
this.props.expandColumnBeforeSelectColumn &&
58+
<th className='react-bs-table-expand-cell'> </th>
59+
], [
60+
this.renderSelectRowHeader(rowCount + 1, rowKey++)
61+
], [
62+
this.props.expandColumnVisible &&
63+
!this.props.expandColumnBeforeSelectColumn &&
64+
<th className='react-bs-table-expand-cell'> </th>
65+
]);
5866
const { sortIndicator, sortList, onSort, reset } = this.props;
5967

6068
React.Children.forEach(this.props.children, (elm) => {
@@ -141,7 +149,10 @@ TableHeader.propTypes = {
141149
sortIndicator: PropTypes.bool,
142150
customComponent: PropTypes.func,
143151
colGroups: PropTypes.element,
144-
reset: PropTypes.bool
152+
reset: PropTypes.bool,
153+
expandColumnVisible: PropTypes.bool,
154+
expandColumnComponent: PropTypes.func,
155+
expandColumnBeforeSelectColumn: PropTypes.bool
145156
};
146157

147158
export default TableHeader;

src/util.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ export default {
4646
return typeof window !== 'undefined' && typeof window.document !== 'undefined';
4747
},
4848

49-
renderColGroup(columns, selectRow) {
49+
renderColGroup(columns, selectRow, expandColumnOptions = {}) {
5050
let selectRowHeader = null;
51+
let expandRowHeader = null;
5152
const isSelectRowDefined = selectRow.mode === Const.ROW_SELECT_SINGLE ||
5253
selectRow.mode === Const.ROW_SELECT_MULTI;
5354
if (isSelectRowDefined) {
@@ -56,9 +57,16 @@ export default {
5657
minWidth: selectRow.columnWidth || 30
5758
};
5859
if (!selectRow.hideSelectColumn) {
59-
selectRowHeader = (<col style={ style } key={ -1 }></col>);
60+
selectRowHeader = (<col key='select-col' style={ style }></col>);
6061
}
6162
}
63+
if (expandColumnOptions.expandColumnVisible) {
64+
const style = {
65+
width: expandColumnOptions.columnWidth || 30,
66+
minWidth: expandColumnOptions.columnWidth || 30
67+
};
68+
expandRowHeader = (<col key='expand-col' style={ style }></col>);
69+
}
6270
const theader = columns.map(function(column, i) {
6371
const style = {
6472
display: column.hidden ? 'none' : null
@@ -74,7 +82,14 @@ export default {
7482

7583
return (
7684
<colgroup>
77-
{ selectRowHeader }{ theader }
85+
{ expandColumnOptions.expandColumnVisible &&
86+
expandColumnOptions.expandColumnBeforeSelectColumn &&
87+
expandRowHeader }
88+
{ selectRowHeader }
89+
{ expandColumnOptions.expandColumnVisible &&
90+
!expandColumnOptions.expandColumnBeforeSelectColumn &&
91+
expandRowHeader }
92+
{ theader }
7893
</colgroup>
7994
);
8095
}

0 commit comments

Comments
 (0)