applegrew / django-select2

This is a Django integration for Select2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to specify if widget should be automatically initialized

fgblomqvist opened this issue · comments

Is your feature request related to a problem? Please describe.
Sometimes it can be useful/necessary to initialize the select2 later, not immediately on page load. One example is when the select2 resides in an element that will be cloned. The cloner could save the original element on page load (with an uninitialized select2) and then initialize the select2 on every copy it appends into the DOM. That way you don't have to deal with destroying select, etc.

Describe the solution you'd like
Modify django_select2.js to only initialize select2's that have a certain class, such as init.
Example:
$('.django-select2.init').djangoSelect2()
Then add a config var to the Django widget that will cause that class to be added to the element (and that var could default to True to retain current behavior).

Describe alternatives you've considered
Modify django_select2.js myself to comment out the default initialization. Not as flexible as per-widget-config and also requires me to keep my own copy of django_select2.js.

Hi @fgblomqvist thank you for reaching out. In deed, you make a very valid point. It can be confusing for some, to find a good way to extend the JS functionality of this library.
What I would recommend, is to change the class name. We initialize based on a class based CSS selector. Changing the CSS class will render the selector useless and you can go ahead and write your own script that initializes Select2 with all the additional options one might desire.

I guess they way that is done could be improved and it should be documented. Currently, you would need to overwrite the build_attrs method. I wrote a how-to, in another comment recently here:
#561 (comment)

So I believe two things need to be done.

  1. We need to set the CSS class based on a instance attribute, over here:
    if 'class' in attrs:
    attrs['class'] += ' django-select2'
    else:
    attrs['class'] = 'django-select2'
    return attrs
    def __init__(self, *args, **kwargs):
        self.css_class = kwargs.pop('css_class', 'django-select2')
        super().__init__(*args, **kwargs)

    def build_attrs(self, base_attrs, extra_attrs=None):
        # ...
        if 'class' in attrs:
            attrs['class'] += self.css_class
        else:
            attrs['class'] = self.css_class
        return attrs
  1. We need to a documentation – probably a whole section – dedicated on how to customize the library. In fact there are also fancy NPM/Yarn ways to do this.

@fgblomqvist maybe you would do the honors of submitting a pull-request that improves this issue. I am happy to review and get it releases asap :)

Sounds good, I'll see if I can find any time this week! :)

This repo has moved, please address your issue at https://github.com/codingjoe/django-select2 Thanks!