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

Dynamically Change the Options in Advanced Select #183

Closed
more-malekpour opened this issue Nov 27, 2023 · 14 comments
Closed

Dynamically Change the Options in Advanced Select #183

more-malekpour opened this issue Nov 27, 2023 · 14 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@more-malekpour
Copy link

Hello,

First of all, thanks for this awesome library. I'm trying to dynamically change (add and remove) the options of the advanced select but have had no luck so far with the HSSelect methods, such as buildOption(). I can easily create/remove options for vanilla HTML with JS, but not working for the Advanced Select plugin.

Thanks in advance for your assistance.

@kshitijmehta
Copy link

Was just looking for the same thing.
I'm having issues even to render dynamic options for Advanced Select.

My code is pretty simple.

          <select
            data-hs-select='{// all the config from the Country selector from the example }'
            className="hidden">
           // loading the options dynamically. 
            {
              namespaces.map((namespace) => {
                return (
                  <option value={namespace.metadata.uuid} key={namespace.metadata.uuid}>
                    {namespace.metadata.name}
                  </option>
                )
              })
            }
            
          </select>

Nothing gets rendered

@sebastianbilling
Copy link

sebastianbilling commented Nov 28, 2023

I'm able to render dynamic options for Advanced Select:
<select className="hidden" > <option value={""}> Choose </option> {items(item => { return ( <option key={item.id} value={item.id}> {item.val} </option> ) })} </select>

However, I'm unable to use the onChange to set the state, any know know why?

Are you sure you imported the PrelineScript according to the documentation?

@more-malekpour
Copy link
Author

@sebastianbilling For the first render, I can render options with a similar code to yours. The problem is updating (adding or removing) options with jQuery or vanilla JS. Let's say I want to update the options of a select based on data retrieved from the server through JSON object.

@olegpix
Copy link
Collaborator

olegpix commented Dec 1, 2023

@sebastianbilling For the first render, I can render options with a similar code to yours. The problem is updating (adding or removing) options with jQuery or vanilla JS. Let's say I want to update the options of a select based on data retrieved from the server through JSON object.

Are you using any library or framework like Vue or React?
Have you tried updating your select using a helper function? E.g.

const [options, setOptions] = useState(['one', 'two']);

// yours options update code

useEffect(() => {
    HSStaticMethods.autoInit(['select']);
}, [options]);

@more-malekpour
Copy link
Author

@sebastianbilling For the first render, I can render options with a similar code to yours. The problem is updating (adding or removing) options with jQuery or vanilla JS. Let's say I want to update the options of a select based on data retrieved from the server through JSON object.

Are you using any library or framework like Vue or React? Have you tried updating your select using a helper function? E.g.

const [options, setOptions] = useState(['one', 'two']);

// yours options update code

useEffect(() => {
    HSStaticMethods.autoInit(['select']);
}, [options]);

@olegpix Thanks for your answer. I'm using Django (Python), thus I have to stick with jQuery and vanilla JS for updating the options. I tried to replicate your suggested code but unfortunately couldn't translate it to a working jQuery code.

@olegpix
Copy link
Collaborator

olegpix commented Dec 1, 2023

@sebastianbilling For the first render, I can render options with a similar code to yours. The problem is updating (adding or removing) options with jQuery or vanilla JS. Let's say I want to update the options of a select based on data retrieved from the server through JSON object.

Are you using any library or framework like Vue or React? Have you tried updating your select using a helper function? E.g.

const [options, setOptions] = useState(['one', 'two']);

// yours options update code

useEffect(() => {
    HSStaticMethods.autoInit(['select']);
}, [options]);

@olegpix Thanks for your answer. I'm using Django (Python), thus I have to stick with jQuery and vanilla JS for updating the options. I tried to replicate your suggested code but unfortunately couldn't translate it to a working jQuery code.

Hmmm... The code is very similar to React, which is why I thought you were using it. In any case, if you are using vanilla code (without a library), this means that you need to reinitialize the select using this method HSStaticMethods.autoInit(['select']); after you update options , perhaps it will be a callback to the fetch function.
The basic idea here is that you need to reinitialize the select after some update.

const data = await fetch('https://api.com/items').then(() => {
    HSStaticMethods.autoInit(['select']);
});

or

const data = await fetch('https://api.com/items').then(() => {
    setTimeout(() => {
        HSStaticMethods.autoInit(['select']);
    }, 100);
});

Hope it helps.

@more-malekpour
Copy link
Author

I really appreciate your help. I did the following steps but still nothing happened in the frontend:

  1. Replaced the old options with the new options with:
$("select").empty().append("<option value='test'>test</option>")
  1. In the next step, I used HSStaticMethods.autoInit(["select"])

Thanks in advance.

@jahaganiev jahaganiev added the help wanted Extra attention is needed label Dec 2, 2023
@shanke-pvex
Copy link

I have the same problem. Adding options dynamically to the advanced select, nothing happens in frontend.
No success with HSStaticMethods.autoInit(["select"]).
If i add options to a normal select via reactive data in vue3 it works.
Is it possible to rerender the advanced select?

@shanke-pvex
Copy link

If i call this: new HSSelect(document.querySelector("#module_type")); before my axios request and once in success handler i get the following ui.
Bildschirmfoto 2023-12-04 um 22 11 58
The select seems to bo twice initalized.

I hope someone can help with this problem.

@umaraziz0
Copy link

umaraziz0 commented Dec 24, 2023

Having the same problem and unable to update the options dynamically. One workaround is to not render the component until the options data is fetched, which isn't very ideal.

Hope there will be a fix for this soon.

@more-malekpour
Copy link
Author

more-malekpour commented Dec 24, 2023

It seems that this issue is a bug rather than help wanted, could you please reconsider the label? @jahaganiev Thank you

@jahaganiev jahaganiev added bug Something isn't working enhancement New feature or request and removed help wanted Extra attention is needed labels Dec 25, 2023
@jahaganiev
Copy link
Member

Hey @more-malekpour, updated the label - our team is looking into it. Thanks!

@jahaganiev
Copy link
Member

Hey everyone, we've just released v2.0.3 update with dynamic validation states, the ability to add/remove options and more. Check out some sample demos at https://preline.co/docs/advanced-select.html#dynamic-validation-states

After updating, if you are still having any issues, please open a new issue with more details. Thanks!

@marconi
Copy link

marconi commented Jan 22, 2024

I have the same issue even after updating to latest, the select plugin just doesn't detect changes on the select element's options. I had to call destroy() first then re-instantiate the plugin with new HSSelect(...). The component ends up flickering which looks really bad but its the only way for me for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants