Replies: 1 comment 2 replies
-
Hi @rez0n, How are you? Excellent ticket as always, but I am not 100% sure, I am getting the use case right. Let me try to paraphrase: You are trying to disable single options in your results. Or pass other options and render the options differently. At my previous company, we used that to select articles (n=10^8). We would render the options with images and manufacturer name, EAN and other information. I believe my preferred way is to use a custom view and a custom template. You will need to:
That's it. It sounds like much in the beginning, but once you have figured it out, it's truly powerful. Please reach out to me if you need anything. Cheers! |
Beta Was this translation helpful? Give feedback.
-
Hi, sometimes I need pass additional html (data) attribute to the Select2 field options, for example to disable some options from selecting or do other restrictions on the page based on the selected option.
It is big problem.
What solutions I "invented" over the last year or two.
Solution 1
Creating custom widget on top of the
s2forms.Select2Widget
This is most flexible and stable solution, but it works only with choices, which is means it will work super slow with queryset which contains 10K options (which I have and need to use).
Here is example, where I annotated value
is_disabled
by some conditions and passed it to the Select2 option attrs to disable this such option in list.Solution 2
This solution is for cases when I can't build choices list because queryset contains thousands options.
But it have very serious consequence, it works only in case when you doing search in the Select2 field, after saving and reloading page - selected option will lost appended attribute.
(I tried to modify
ModelSelect2Widget
to append attribures to the options, lost a few days without result, seems it require to re-implement model choices generator, but it became super slow on any modification)I will very thankfully if someone have any ideas and suggestion how to append additional attrs to the options without pain and also do this for
ModelSelect2Widget
.Beta Was this translation helpful? Give feedback.
All reactions