-
Notifications
You must be signed in to change notification settings - Fork 782
/
manage-multi-sort-external-table.js
87 lines (74 loc) · 2.21 KB
/
manage-multi-sort-external-table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* eslint max-len: 0 */
/* eslint no-console: 0 */
import React from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
const products = [];
function addProducts(quantity) {
const startId = products.length;
let price = 2100;
for (let i = 0; i < quantity; i++) {
const id = startId + i;
if (id % 5 === 0) {
price += 100;
}
products.push({
id: id,
name: 'Item name ' + (id % 8 + 1),
price: price
});
}
}
addProducts(15);
export default class ExternalMultiSort extends React.Component {
constructor(props) {
super(props);
this.state = {
sortName: [],
sortOrder: []
};
this.onSortChange = this.onSortChange.bind(this);
this.cleanSort = this.cleanSort.bind(this);
}
onSortChange(name, order) {
const sortName = [];
const sortOrder = [];
for (let i = 0; i < this.state.sortName.length; i++) {
if (this.state.sortName[i] !== name) {
sortName.push(this.state.sortName[i]);
sortOrder.push(this.state.sortOrder[i]);
}
}
sortName.push(name);
sortOrder.push(order);
this.setState({
sortName,
sortOrder
});
}
cleanSort() {
this.setState({
sortName: [],
sortOrder: []
});
}
render() {
const options = {
// reassign the multi sort list by an Array
// if you dont want to control al the sort list, you can only assign the String to sortName and sort Order
sortName: this.state.sortName,
sortOrder: this.state.sortOrder,
onSortChange: this.onSortChange
};
return (
<div>
<button className='btn ben-default' onClick={ this.cleanSort }>Clean</button>
<p style={ { color: 'red' } }>sort: sortName={ this.state.sortName }, sortOrder={ this.state.sortOrder }</p>
<BootstrapTable data={ products } options={ options } multiColumnSort={ 2 }>
<TableHeaderColumn dataField='id' isKey={ true } dataSort={ true }>Product ID</TableHeaderColumn>
<TableHeaderColumn dataField='name' dataSort={ true }>Product Name</TableHeaderColumn>
<TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
</BootstrapTable>
</div>
);
}
}