Home › Forums › Backend Issues (wp-admin) › Dynamically disabling/enabling some select2 options
Hi,
I’ve used the same select2 version 3.x which ACF uses in the backend in my theme and I can just update the actual <option> tags inside <select> tags with prop(‘disable’, true) on the <option> tags and then trigger(‘update’) on the <select> tag, that makes select2 update its view and show the disabled options correctly.
But the same does not work for the ACF fields which uses select2, any idea why? and how can I make it work?
Hi @sil3ntone
I’m afraid you can’t disable the options. But you can always modify the returned data by using the acf/load_field
hook. That way you can remove the options you want. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
I hope this helps 🙂
I’m afraid that wont work for me, I need to limit options of selectbox 2 based on the choice selected in selectbox 1.
What you have mentioned above would require updating the post. I got several dependent fields like this, forcing a page refresh on updating a field value would not be a good experience to the person doing the data entry.
Hi @sil3ntone
In that case, I think you can try this tutorial: https://www.bobz.co/dynamically-populate-select-fields-choice-in-advanced-custom-fields/.
It’s an old tutorial, but I believe you still can use it.
Hope this helps 🙂
I noticed there is a load over AJAX option. Does it in some way allow me to hook in at javascript level to send an extra parameter? That way I’ll be able to access it in wordpress filter.
Update 1 :
select2_args might be usable
Source: https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
Thanks for the link but that tutorial is using native select boxes, with multiselect enables those can be a problem (accidental clicks, without pressing ctrl etc, just really annoying) 😛
I’ll give the select2_args idea I had a try and update here if it works as expected.
The topic ‘Dynamically disabling/enabling some select2 options’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.