Skip to content

Commit 464ecde

Browse files
committed
Added code for custom buttons
1 parent c824832 commit 464ecde

File tree

6 files changed

+97
-5
lines changed

6 files changed

+97
-5
lines changed

examples/js/app.js

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const routes = (
3131
<Route path='others' component={ require('./others/demo') } />
3232
<Route path='complex' component={ require('./complex/demo') } />
3333
<Route path='remote' component={ require('./remote/demo') } />
34+
<Route path='custom' component={ require('./custom/demo') } />
3435
</Route>
3536
<Route path='*' component={ PageNotFound }/>
3637
</Route>

examples/js/components/App.js

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class App extends React.Component {
7575
}, {
7676
text: 'A complex demo',
7777
href: 'complex'
78+
}, {
79+
text: 'Custom buttons',
80+
href: 'custom'
7881
} ];
7982

8083
const exampleMenuItems = examples.map((item, idx) => {

examples/js/custom/custom-buttons.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* eslint no-alert: 0 */
2+
import React from 'react';
3+
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
4+
5+
const products = Array.apply(null, Array(10)).map( (v, id) => {
6+
return {
7+
id: id,
8+
name: 'Item name ' + id,
9+
price: 2100 + id
10+
};
11+
});
12+
13+
class CustomButtons extends React.Component {
14+
render() {
15+
const customButtons = [
16+
{
17+
text: 'Display an alert',
18+
handler: () => alert('Custom button Clicked')
19+
},
20+
{
21+
text: 'Do not click here !',
22+
handler: () => alert('You, bad boy !'),
23+
bsStyle: 'warning',
24+
icon: 'alert'
25+
}
26+
];
27+
return (
28+
<BootstrapTable
29+
data={ products }
30+
customButtons={ customButtons }>
31+
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
32+
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
33+
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
34+
</BootstrapTable>
35+
);
36+
}
37+
}
38+
39+
export default CustomButtons;

examples/js/custom/demo.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import renderLinks from '../utils';
3+
import { Col, Panel } from 'react-bootstrap';
4+
import CustomButtons from './custom-buttons';
5+
6+
class Demo extends React.Component {
7+
render() {
8+
return (
9+
<Col md={ 8 } mdOffset={ 1 }>
10+
<Panel header={ 'A react-bootstrap-table with custom buttons' }>
11+
{ renderLinks('custom/custom-buttons.js') }
12+
<CustomButtons />
13+
</Panel>
14+
</Col>
15+
);
16+
}
17+
}
18+
19+
export default Demo;

src/BootstrapTable.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,8 @@ class BootstrapTable extends Component {
746746
|| insertRow
747747
|| deleteRow
748748
|| search
749-
|| this.props.exportCSV) {
749+
|| this.props.exportCSV
750+
|| this.props.customButtons.length > 0) {
750751
let columns;
751752
if (Array.isArray(children)) {
752753
columns = children.map(function(column) {
@@ -794,7 +795,9 @@ class BootstrapTable extends Component {
794795
onDropRow={ this.handleDropRow }
795796
onSearch={ this.handleSearch }
796797
onExportCSV={ this.handleExportCSV }
797-
onShowOnlySelected={ this.handleShowOnlySelected }/>
798+
onShowOnlySelected={ this.handleShowOnlySelected }
799+
customButtons={ this.props.customButtons } />
800+
798801
</div>
799802
);
800803
} else {
@@ -977,7 +980,13 @@ BootstrapTable.propTypes = {
977980
}),
978981
exportCSV: PropTypes.bool,
979982
csvFileName: PropTypes.string,
980-
ignoreSinglePage: PropTypes.bool
983+
ignoreSinglePage: PropTypes.bool,
984+
customButtons: React.PropTypes.arrayOf(React.PropTypes.shape({
985+
text: React.PropTypes.string.isRequired,
986+
icon: React.PropTypes.string,
987+
bsStyle: React.PropTypes.string,
988+
handler: React.PropTypes.func.isRequired
989+
}))
981990
};
982991
BootstrapTable.defaultProps = {
983992
height: '100%',
@@ -1062,7 +1071,8 @@ BootstrapTable.defaultProps = {
10621071
},
10631072
exportCSV: false,
10641073
csvFileName: 'spreadsheet.csv',
1065-
ignoreSinglePage: false
1074+
ignoreSinglePage: false,
1075+
customButtons: []
10661076
};
10671077

10681078
export default BootstrapTable;

src/toolbar/ToolBar.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint no-console: 0 */
12
import React, { Component, PropTypes } from 'react';
23
import classSet from 'classnames';
34
import Const from '../Const';
@@ -187,6 +188,7 @@ class ToolBar extends Component {
187188
let deleteBtn = null;
188189
let exportCSV = null;
189190
let showSelectedOnlyBtn = null;
191+
let customButtons = null;
190192

191193
if (this.props.enableInsert) {
192194
insertBtn = (
@@ -233,6 +235,17 @@ class ToolBar extends Component {
233235
</button>
234236
);
235237
}
238+
if (this.props.customButtons.length > 0) {
239+
customButtons = this.props.customButtons.map((b, i) => (
240+
<button type='button'
241+
key={ i }
242+
className={ 'btn btn-' + (b.bsStyle ? b.bsStyle : 'primary') }
243+
onClick={ b.handler }>
244+
{ b.icon ? (<i className={ 'glyphicon glyphicon-' + b.icon }></i>) : null }
245+
{ b.text }
246+
</button>
247+
));
248+
}
236249

237250
const searchTextInput = this.renderSearchPanel();
238251
const modal = this.props.enableInsert ? this.renderInsertRowModal() : null;
@@ -244,6 +257,7 @@ class ToolBar extends Component {
244257
{ exportCSV }
245258
{ insertBtn }
246259
{ deleteBtn }
260+
{ customButtons }
247261
{ showSelectedOnlyBtn }
248262
</div>
249263
</div>
@@ -379,7 +393,13 @@ ToolBar.propTypes = {
379393
saveText: PropTypes.string,
380394
closeText: PropTypes.string,
381395
clearSearch: PropTypes.bool,
382-
ignoreEditable: PropTypes.bool
396+
ignoreEditable: PropTypes.bool,
397+
customButtons: React.PropTypes.arrayOf(React.PropTypes.shape({
398+
text: React.PropTypes.string.isRequired,
399+
icon: React.PropTypes.string,
400+
bsStyle: React.PropTypes.string,
401+
handler: React.PropTypes.func.isRequired
402+
}))
383403
};
384404

385405
ToolBar.defaultProps = {

0 commit comments

Comments
 (0)