Skip to content

Commit ec1f96c

Browse files
committed
fix #1082
1 parent 00b1558 commit ec1f96c

File tree

5 files changed

+129
-4
lines changed

5 files changed

+129
-4
lines changed

docs/columns.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Available properties in a column object:
1313
* [formatExtraData](#formatExtraData)
1414
* [type](#type)
1515
* [sort](#sort)
16+
* [sortValue](#sortValue)
1617
* [sortFunc](#sortFunc)
1718
* [sortCaret](#sortCaret)
1819
* [onSort](#onSort)
@@ -141,8 +142,42 @@ Specify the data type on column. Available value so far is `string`, `number`, `
141142
## <a name='sort'>column.sort - [Bool]</a>
142143
Enable the column sort via a `true` value given.
143144

145+
## <a name='sortValue'>column.sortValue - [Function]</a>
146+
`column.sortValue` only work when `column.sort` enabled. This prop allow you to replace the value when table sorting.
147+
148+
For example, consider following data:
149+
150+
```js
151+
const types = ['Cloud Service', 'Message Service', 'Add Service', 'Edit Service', 'Money'];
152+
const data = [{id: 1, type: 2}, {id: 2, type: 1}, {id: 3, type:0}];
153+
const columns = [{
154+
dataField: 'id',
155+
text: 'Job ID'
156+
}, {
157+
dataField: 'type',
158+
text: 'Job Type'
159+
sort: true,
160+
formatter: (cell, row) => types[cell]
161+
}]
162+
```
163+
164+
In above case, when user try to sort Job Type column which will sort the original value: 0, 1, 2 but we display the type name via [`column.formatter`](#formatter), which will lead confuse because we are sorting by type value instead of type name. So `sortValue` is a way for you to decide what kind of value should be adopted when sorting on a specify column:
165+
166+
```js
167+
const columns = [{
168+
dataField: 'id',
169+
text: 'Job ID'
170+
}, {
171+
dataField: 'type',
172+
text: 'Job Type'
173+
sort: true,
174+
formatter: (cell, row) => types[cell],
175+
sortValue: (cell, row) => types[cell] // we use type name to sort.
176+
}]
177+
```
178+
144179
## <a name='sortFunc'>column.sortFunc - [Function]</a>
145-
`column.sortFunc` only work when `column.sort` is enable. `sortFunc` allow you to define your sorting algorithm. This callback function accept six arguments:
180+
`column.sortFunc` only work when `column.sort` enabled. `sortFunc` allow you to define your sorting algorithm. This callback function accept six arguments:
146181

147182
```js
148183
{
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* eslint no-unused-vars: 0 */
2+
import React from 'react';
3+
4+
import BootstrapTable from 'react-bootstrap-table-next';
5+
import Code from 'components/common/code-block';
6+
import { jobsGenerator1 } from 'utils/common';
7+
8+
const jobs = jobsGenerator1(8);
9+
10+
const types = ['Cloud Service', 'Message Service', 'Add Service', 'Edit Service', 'Money'];
11+
12+
const columns = [{
13+
dataField: 'id',
14+
text: 'Job ID'
15+
}, {
16+
dataField: 'name',
17+
text: 'Job Name'
18+
}, {
19+
dataField: 'owner',
20+
text: 'Job Owner'
21+
}, {
22+
dataField: 'type',
23+
text: 'Job Type',
24+
sort: true,
25+
formatter: (cell, row) => types[cell],
26+
sortValue: (cell, row) => types[cell]
27+
}];
28+
29+
const sourceCode = `\
30+
import BootstrapTable from 'react-bootstrap-table-next';
31+
32+
const types = ['Cloud Service', 'Message Service', 'Add Service', 'Edit Service', 'Money'];
33+
34+
const columns = [{
35+
dataField: 'id',
36+
text: 'Job ID'
37+
}, {
38+
dataField: 'name',
39+
text: 'Job Name'
40+
}, {
41+
dataField: 'owner',
42+
text: 'Job Owner'
43+
}, {
44+
dataField: 'type',
45+
text: 'Job Type',
46+
sort: true,
47+
formatter: (cell, row) => types[cell],
48+
sortValue: (cell, row) => types[cell]
49+
}];
50+
51+
<BootstrapTable keyField='id' data={ products } columns={ columns } />
52+
`;
53+
54+
export default class Test extends React.Component {
55+
constructor(props) {
56+
super(props);
57+
this.state = { data: jobs };
58+
}
59+
60+
handleClick = () => {
61+
this.setState(() => {
62+
const newProducts = jobsGenerator1(21);
63+
return {
64+
data: newProducts
65+
};
66+
});
67+
}
68+
69+
render() {
70+
return (
71+
<div>
72+
<button className="btn btn-default" onClick={ this.handleClick }>Change Data</button>
73+
<BootstrapTable keyField="id" data={ this.state.data } columns={ columns } />
74+
<Code>{ sourceCode }</Code>
75+
</div>
76+
);
77+
}
78+
}

packages/react-bootstrap-table2-example/src/utils/common.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export const withOnSale = rows => rows.map((row) => {
2929
return row;
3030
});
3131

32+
export const withRandomPrice = rows => rows.map((row) => {
33+
row.price = Math.floor((Math.random() * 10) + 2000);
34+
return row;
35+
});
36+
3237
export const productsQualityGenerator = (quantity = 5, factor = 0) =>
3338
Array.from({ length: quantity }, (value, index) => ({
3439
id: index + factor,

packages/react-bootstrap-table2-example/stories/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import EnableSortTable from 'examples/sort/enable-sort-table';
104104
import DefaultSortTable from 'examples/sort/default-sort-table';
105105
import DefaultSortDirectionTable from 'examples/sort/default-sort-direction';
106106
import SortEvents from 'examples/sort/sort-events';
107+
import CustomSortValue from 'examples/sort/custom-sort-value';
107108
import CustomSortTable from 'examples/sort/custom-sort-table';
108109
import CustomSortCaretTable from 'examples/sort/custom-sort-caret';
109110
import HeaderSortingClassesTable from 'examples/sort/header-sorting-classes';
@@ -357,6 +358,7 @@ storiesOf('Sort Table', module)
357358
.add('Default Sort Table', () => <DefaultSortTable />)
358359
.add('Default Sort Direction Table', () => <DefaultSortDirectionTable />)
359360
.add('Sort Events', () => <SortEvents />)
361+
.add('Custom Sort Value', () => <CustomSortValue />)
360362
.add('Custom Sort Fuction', () => <CustomSortTable />)
361363
.add('Custom Sort Caret', () => <CustomSortCaretTable />)
362364
.add('Custom Classes on Sorting Header Column', () => <HeaderSortingClassesTable />)

packages/react-bootstrap-table2/src/store/sort.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ function comparator(a, b) {
1414
return result;
1515
}
1616

17-
export const sort = (data, sortOrder, { dataField, sortFunc }) => {
17+
export const sort = (data, sortOrder, { dataField, sortFunc, sortValue }) => {
1818
const _data = [...data];
1919
_data.sort((a, b) => {
2020
let result;
2121
let valueA = _.get(a, dataField);
2222
let valueB = _.get(b, dataField);
23-
valueA = _.isDefined(valueA) ? valueA : '';
24-
valueB = _.isDefined(valueB) ? valueB : '';
23+
if (sortValue) {
24+
valueA = sortValue(valueA, a);
25+
valueB = sortValue(valueB, b);
26+
} else {
27+
valueA = _.isDefined(valueA) ? valueA : '';
28+
valueB = _.isDefined(valueB) ? valueB : '';
29+
}
2530

2631
if (sortFunc) {
2732
result = sortFunc(valueA, valueB, sortOrder, dataField, a, b);

0 commit comments

Comments
 (0)