-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Whe nusing a custom sort function, pass any other extra data.
The example shows how a column that displays enum values can be sorted according to those values and not the enum number. const qualityType = { 0: 'good', 1: 'bad', 2: 'unknown' }; Indeed, instead of sorting item tables according to the enum keys 0, 1 and 2 it is possible to sort now by the enum values "good", "bad" and "unknown" alphabetically which makes more sense since this is what the user can see. ASC order will show "bad" items, then "good" items then "unknown" items (alphabetical order is respected), instead of showing "good", then "bad", then "unknown".
- Loading branch information
Showing
5 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table'; | ||
|
||
const products = []; | ||
|
||
const qualityType = { | ||
0: 'good', | ||
1: 'bad', | ||
2: 'unknown' | ||
}; | ||
|
||
function addProducts(quantity) { | ||
const startId = products.length; | ||
for (let i = 0; i < quantity; i++) { | ||
const id = startId + i; | ||
products.push({ | ||
id: id, | ||
name: 'Item name ' + id, | ||
quality: i % 3 | ||
}); | ||
} | ||
} | ||
|
||
addProducts(5); | ||
|
||
function enumFormatter(cell, row, enumObject) { | ||
return enumObject[cell]; | ||
} | ||
|
||
function sortByName(a, b, order, field, enumObject) { | ||
if (order === 'desc') { | ||
if (enumObject[a[field]] > enumObject[b[field]]) { | ||
return -1; | ||
} else if (enumObject[a[field]] < enumObject[b[field]]) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
if (enumObject[a[field]] < enumObject[b[field]]) { | ||
return -1; | ||
} else if (enumObject[a[field]] > enumObject[b[field]]) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
export default class CustomSortWithExtraDataTable extends React.Component { | ||
render() { | ||
return ( | ||
<BootstrapTable data={ products }> | ||
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn> | ||
<TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn> | ||
<TableHeaderColumn dataField='quality' | ||
dataFormat={ enumFormatter } formatExtraData={ qualityType } | ||
dataSort={ true } | ||
sortFunc={ sortByName } sortFuncExtraData={ qualityType }> | ||
Product Quality</TableHeaderColumn> | ||
</BootstrapTable> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters