Skip to content

Fix #128, add documentation on how to configure select2 options #215

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

Merged
merged 1 commit into from
May 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions docs/django_select2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,47 @@ plugin. It will handle both normal and heavy fields. Simply call

$('.django-select2').djangoSelect2();

Please replace all your ``.select2`` invocations with the here provided
``.djangoSelect2``.

You can pass see `Select2 options <https://select2.github.io/options.html>`_ if needed::

$('.django-select2').djangoSelect2({placeholder: 'Select an option'});
Configuring Select2
-------------------

Select2 options can be configured either directly from Javascript or from within Django
using widget attributes. `(List of options in the Select2 docs) <https://select2.org/configuration/options-api>`_.

To pass options in javascript

.. code-block:: javascript

$('.django-select2').djangoSelect2({
minimumInputLength: 0,
placeholder: 'Select an option',
});

From Django, you can use ``data-`` attributes using the same names in camel-case and
passing them to your widget. Select2 will then pick these up. For example when
initialising a widget in a form, you could do:

.. code-block:: python

class MyForm(forms.Form):
my_field = forms.ModelMultipleChoiceField(
widget=ModelSelect2MultipleWidget(
model=MyModel
search_fields=['another_field']
attrs={
"data-minimum-input-length": 0,
"data-placeholder": "Select an option",
"data-close-on-select": "false",
}
)
)

(If you do not want to initialize the widget, you could add the attributes by overriding
a widget method and adding them in a super call, e.g. `get_context() <https://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.get_context>`_)

Please replace all your ``.select2`` invocations with the here provided
``.djangoSelect2``.

Security & Authentication
-------------------------
Expand Down