Skip to content

Commit bd9b06c

Browse files
committed
2 parents c7758d6 + e83463e commit bd9b06c

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

examples/js/selection/all-select.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
products.push({
12+
id: id,
13+
name: 'Item name ' + id,
14+
price: 2100 + i
15+
});
16+
}
17+
}
18+
19+
addProducts(100);
20+
21+
const options = {
22+
sizePerPageList: [ 5, 10, 15, 20 ],
23+
sizePerPage: 10,
24+
sortName: 'id',
25+
sortOrder: 'desc'
26+
};
27+
const selectRowProp = {
28+
mode: 'checkbox',
29+
clickToSelect: true,
30+
bgColor: 'rgb(238, 193, 213)'
31+
};
32+
const filterOptions = {
33+
type: 'TextFilter',
34+
placeholder: 'Product Name'
35+
};
36+
37+
export default class SelectAll extends React.Component {
38+
constructor(props) {
39+
super(props);
40+
this.state = {
41+
selected: [],
42+
filteredProducts: products
43+
};
44+
}
45+
46+
onSelect(row, isSelected) {
47+
const selectedProducts = this.state.selected;
48+
if (isSelected) {
49+
selectedProducts.push(row.id);
50+
} else {
51+
const index = selectedProducts
52+
.indexOf(row.universalId);
53+
selectedProducts.splice(index, 1);
54+
}
55+
this.setState({ selected: selectedProducts });
56+
}
57+
58+
onSelectAll(isSelected) {
59+
const items = isSelected
60+
? this.state.filteredProducts.map(product => product.id)
61+
: [];
62+
this.setState({ selected: items });
63+
}
64+
65+
afterColumnFilter(filterConds, result) {
66+
this.setState({ filteredProducts: result });
67+
}
68+
69+
render() {
70+
Object.assign(selectRowProp, {
71+
selected: this.state.selected,
72+
onSelect: this.onSelect.bind(this),
73+
onSelectAll: this.onSelectAll.bind(this) });
74+
Object.assign(options, {
75+
afterColumnFilter: this.afterColumnFilter.bind(this) });
76+
77+
return (
78+
<div>
79+
<div>Product selected: { this.state.selected.length }</div>
80+
<BootstrapTable ref='table' selectRow={ selectRowProp } data={ products } pagination={ true } options={ options }>
81+
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
82+
<TableHeaderColumn dataField='name' filter={ filterOptions }>Product Name</TableHeaderColumn>
83+
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
84+
</BootstrapTable>
85+
</div>
86+
);
87+
}
88+
}

examples/js/selection/demo.js

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SelectValidationTable from './select-validation-table';
1212
import RowClickTable from './row-click-table';
1313
import OnlySelectedTable from './only-show-selected-table';
1414
import ExternallyManagedSelection from './externally-managed-selection';
15+
import SelectAll from './all-select';
1516

1617
class Demo extends React.Component {
1718
render() {
@@ -126,6 +127,15 @@ class Demo extends React.Component {
126127
</div>
127128
</div>
128129
</div>
130+
<div className='col-md-offset-1 col-md-8'>
131+
<div className='panel panel-default'>
132+
<div className='panel-heading'>Select all (with pagination and filter)</div>
133+
<div className='panel-body'>
134+
<h5>Source in all-select.js</h5>
135+
<SelectAll />
136+
</div>
137+
</div>
138+
</div>
129139
</div>
130140
);
131141
}

0 commit comments

Comments
 (0)