-
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.
Merge pull request #368 from dana2208/Pass_extra_data_to_sort_function
When using a custom sort function, pass any other extra data.
- 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