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

Correctly render selected choice for ListSelect2 grouped fields #1299

Open
j-bernard opened this issue Jun 21, 2022 · 2 comments
Open

Correctly render selected choice for ListSelect2 grouped fields #1299

j-bernard opened this issue Jun 21, 2022 · 2 comments

Comments

@j-bernard
Copy link

ListSelect2 does not correctly render the selected choices when used with groups.

This snippet works for groups but I did not make a PR as I'll let the way to detect or declare grouped vs non-grouped to you.

class LgrGroupedListSelect2(autocomplete.ListSelect2):

    def filter_choices_to_render(self, selected_choices):
        # dal.widget.WidgetMixin.filter_choices_to_render does not handle correctly grouped lists
        ch = []
        for group in self.choices:
            c = [c for c in group[1] if str(c[0]) in selected_choices]
            ch.extend(c)
        self.choices = ch
@jpic
Copy link
Member

jpic commented Sep 9, 2022

Dois it work better with Select2GroupListView ?

https://django-autocomplete-light.readthedocs.io/en/master/api.html#dal_select2.views.

@j-bernard
Copy link
Author

I'm using Select2GroupListView to get the list of values to display but the problem is with the field widget.

My form field is as follow:

autocomplete.Select2ListChoiceField(label='',
                                    required=True,
                                    widget=LgrGroupedListSelect2(url='autocomplete-url',
                                                                 attrs={'data-allow-clear': 'false'}))

and autocomplete-url is a Select2GroupListView.
If I replace LgrGroupedListSelect2 with autocomplete.ListSelect2 it will not display the groups correctly.

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

No branches or pull requests

2 participants