Sorting filter select options
See original GitHub issueWe needed to sort the <options> used in selectFilter, but because the existing code uses Object.keys() to do this there was no way to ensure a sort order.
We also had to create boilerplate code to share options between selectFilter, which took options in the form{ value: label, ...} versus the the editor Type.Select which took them as [ { value, label }, ...].
We solved this by extending selectFilter to just use the same method as editor, which preserves the sort order that you create the options with. Here is the file we used to do it:
import React from 'react';
import PropTypes from 'prop-types';
import SelectFilter from 'react-bootstrap-table2-filter/lib/src/components/select';
// Modified version of code to allow for custom sorting
// https://github.com/react-bootstrap-table/react-bootstrap-table2/blob/master/packages/react-bootstrap-table2-filter/src/components/select.js#L61-L73
class SortedSelectFilter extends SelectFilter {
// We have to modify this one as we changed it
static propTypes = {
...SelectFilter.propTypes,
options: PropTypes.array.isRequired,
};
getOptions() {
const optionTags = [];
const { options, placeholder, column, withoutEmptyOption } = this.props;
if (!withoutEmptyOption) {
optionTags.push(
<option key="-1" value="">
{placeholder || `Select ${column.text}...`}
</option>,
);
}
options.forEach(({ value, label }) => {
optionTags.push(
<option key={value} value={value}>
{label}
</option>,
);
});
return optionTags;
}
}
const sortedSelectFilter = (props = {}) => ({
Filter: SortedSelectFilter,
props,
});
export default sortedSelectFilter;
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Filters and sort options - Product Documentation - Search.io
1. Give your filter a Title - The title will be shown in the sidebar if the filter is of type List or...
Read more >Adding sorting options for the select filter [#3194363] - Drupal
I'm adding an option to choose to order the results in the select with "no sorting" as default. User interface changes. New "Order...
Read more >Filter and Sort - Goldman Sachs Design System
Filters set a hard boundary on the search results and exclude anything not falling within the categories selected, while sort sets a soft...
Read more >Sort & filter your data - Computer - Google Docs Editors Help
Select the column you'd like to be sorted first and choose a sorting order. ... To see filter options, go to the top...
Read more >Sort, filter, and search pages - Sitefinity CMS Site components
To display the filtering options for pages, click Filter pages (Filter pages) ... When you select this option all previous sorting and filtering...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The proper way maybe we make the
optionsaccept an object or an array. If array given, we can promise the order will be the preserve.RELEASE NOTE
Example
Please tag me if the issue still remain @jehartzog