Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom filtering #2042

Closed
ghost opened this issue Aug 21, 2018 · 3 comments · Fixed by #2332
Closed

Support custom filtering #2042

ghost opened this issue Aug 21, 2018 · 3 comments · Fixed by #2332
Milestone

Comments

@ghost
Copy link

ghost commented Aug 21, 2018

@yoeriboven commented on May 7, 2018, 10:40 AM UTC:

It might be a good idea to let the user do the filtering. Currently I'm working on a project in which I want the search to be done in the value of the option or in the text with startsWith as the option.

This could be done like this:

$('.selectpicker').on('typed.bs.select', function (input) {
  // Filter results here

  return results; // Array of results
});

Any suggestions?

This issue was moved by caseyjhol from snapappointments/bootstrap-select-temp#56.

@ghost
Copy link
Author

ghost commented Aug 21, 2018

@devjerry commented on May 29, 2018, 2:26 AM UTC:

I'm in similar need.

In addition to setting 'startsWith' or 'contains' in liveSearchStyle option, there should be an option to pass custom function for filtering.

Something similar as https://select2.org/searching

Example:

Let's say I have following selectbox:

<select class="selectpicker" data-live-search="true">
    <option value="1">Paris - Black office</option>
    <option value="2">Paris - Red office</option>
    <option value="3">London - Black office</option>
    <option value="4">London - Red office</option>
</select>

After typing don bla I would like to have London - Black office in search results. So I would write my custom filtering function like this:

$('.selectpicker').selectpicker({
    liveSearchStyle: function(searchTerm, item) {
        return searchTerm.toLowerCase().split(' ').every(function(word) {
            return item.toLowerCase().indexOf(word) !== -1;
        });
    }
});

In the example item parameter represented current text of <option>. I would imagine it could be an object containing different parameters of current item in iteration (e.g. it's index, <option> text, <option> value, if it's inside <optgroup>, ...).

cedk added a commit to cedk/bootstrap-select that referenced this issue Oct 4, 2019
caseyjhol pushed a commit that referenced this issue May 12, 2020
* Allow to use function as liveSearchStyle

Fix #2042
@caseyjhol caseyjhol added this to the v1.14.0 milestone May 12, 2020
@caseyjhol
Copy link
Member

This change will be a part of the v1.14.0 release. This issue will be updated after it's made available.

caseyjhol pushed a commit that referenced this issue Nov 28, 2020
* Allow to use function as liveSearchStyle

Fix #2042
@caseyjhol
Copy link
Member

Released in v1.14.0-beta!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant