question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sorting filter select options

See original GitHub issue

We 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:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
AllenFangcommented, Nov 3, 2018

The proper way maybe we make the options accept an object or an array. If array given, we can promise the order will be the preserve.

0reactions
AllenFangcommented, Nov 4, 2018

RELEASE NOTE

Example

Please tag me if the issue still remain @jehartzog

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found