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

Bootstrap select dropdown items not synchronized with underlying native select options after dynamic add/remove and refresh from code #2724

Open
yankarinRG opened this issue Apr 11, 2022 · 11 comments

Comments

@yankarinRG
Copy link

yankarinRG commented Apr 11, 2022

When bootstrap select is initialized with a select without options, and the options are added and removed programmatically from the code, with the refresh method called to update the select, the elements of the dropdown created by bootstrap select are not synchronized with those of the select.
In the example I attached, the select is initially empty.

Clicking on the "load" button should remove all select options and replaces them with new ones. In this case, bootstrap select will append them instead of replacing them (as in native select).

Clicking the "empty" button should remove all options. Here, bootstrap select will continue to display the previous ones, although all the options of the native select element have been removed.

Version used

  • bootstrap-select at commit 04cc1fd)
  • bootstrap 5.1.3
  • jquery 3.6.0

Minimal, Reproducible Example

https://jsfiddle.net/234vackd/2

@yankarinRG yankarinRG added the bug label Apr 11, 2022
@skiingdomo
Copy link

Hi,

Replaced your inline css and js with cdn hosted libraries, and then your code works:

https://jsfiddle.net/4gkv180d/

@yankarinRG
Copy link
Author

yankarinRG commented Apr 12, 2022

Hello and thank you for the interest. So there was a regression between version 1.14.0-beta2 and commit 04cc1fd?
Or maybe I didn't compile the library correctly? I cloned the repository at the aforementioned commit point and then installed project's dependencies with npm install and built with grunt dist

@skiingdomo
Copy link

Hi mate,

No idea sorry. Im just looking into weather we use this beta on our new site and was concerned about this bug, as it is a well used use case on our site.

@kennysong
Copy link

I think our team has run into this bug as well. Sometimes (I wasn't able to isolate the conditions when this happens), calling .selectpicker('refresh') on the element won't actually refresh it after clearing the <select> and adding new <option>s dynamically.

Workarounds that seem to work for me:

  • .selectpicker('destroy') followed by .selectpicker() (this fixes the JSFiddle)
  • Calling .selectpicker('refresh') twice (this doesn't fix the JSFiddle, though)

@yankarinRG
Copy link
Author

yankarinRG commented Apr 18, 2022

I am also using the workaround of calling selectpicker('destroy') and then selectpicker(), in the meantime of an official bug fix from the author.
It's certainly not the most elegant and probably also optimized solution, but at least it works!

@fwiep
Copy link

fwiep commented Apr 26, 2022

I can confirm the issue is present in latest dev-tag. Using:

  • bootstrap-select v1.14.0-beta3
  • bootstrap 5.1.3
  • jquery 3.6.0

@kennysong 's workaround of destroy and recreate works for me, too.
Can I help in solving this issue?

Thanks,
FWieP

@pahar0
Copy link

pahar0 commented May 8, 2022

I can confirm the issue is present in latest dev-tag. Using:

  • bootstrap-select v1.14.0-beta3
  • bootstrap 5.1.3
  • jquery 3.6.0

@kennysong 's workaround of destroy and recreate works for me, too. Can I help in solving this issue?

Thanks, FWieP

This workaround doesn't work for me using the same bootstrap-select, bootstrap and jQuery versions.

@loupid
Copy link

loupid commented May 17, 2022

jayb611 commented good solution #2712 (comment)

@pedrorivotti
Copy link

Workarounds that seem to work for me:

  • .selectpicker('destroy') followed by .selectpicker() (this fixes the JSFiddle)

One caveat is that events hooked into the select are lost with.selectpicker('destroy'). For example, in https://jsfiddle.net/u53japht/ the event that logs "Triggered" into the console upon selection stops working after hitting "Load" or "Empty".
Yet another workaround could be to re-add the events, as in https://jsfiddle.net/3gbcusar/.

@datadimension
Copy link

any update on this bug fix, it seems quite a basic requirement to change the options dynamically (remove/add without duplication)

@scr4bble
Copy link

scr4bble commented Nov 28, 2023

EDIT: my issue is a duplicate of #2445
Seems to be already fixed in v1.13.16

I am having similar issue - when dynamically replacing all options && preselecting one option in the new list $option.prop('selected', true), then calling .selectpicker('refresh') and getting this result:

  • Text in the button changed to "Nothing selected" && color of this text changed from gray to black
  • when dropdown is opened, I see the preselected option correctly highlighted

The issue isn't present in 1.13.12
Issue appeared after increasing version to 1.13.14

Could be related to this issue because it's also problem with "refresh".
Calling refresh twice or calling refresh & then render solves the issue but I rather switched back to version 1.13.12

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

No branches or pull requests

10 participants