Contains all the Django widgets for Select2.
Bases: object
The base mixin of all Select2 widgets.
This mixin is responsible for rendering the necessary Javascript and CSS codes which turns normal <select> markups into Select2 choice list.
The following Select2 otions are added by this mixin:-
- minimumResultsForSearch: 6
- placeholder: ''
- allowClear: True
- multiple: False
- closeOnSelect: False
Note
Many of them would be removed by sub-classes depending on requirements.
Constructor of the class.
The following additional kwarg is allowed:-
| Parameters: | select2_options (dict or None) – This is similar to standard Django way to pass extra attributes to widgets. This is meant to override values of existing options. Example: class MyForm(ModelForm):
class Meta:
model = MyModel
widgets = {
'name': Select2WidgetName(select2_options={
'minimumResultsForSearch': 10,
'closeOnSelect': True,
})
}
Tip You cannot introduce new options using this. For that you should sub-class and overried init_options(). The reason for this is, few options are not compatible with each other or are not applicable in some scenarios. For example, when Select2 is attached to <select> tag, it can get if it is multiple or single valued from that tag itself. In this case if you specify multiple option then not only it is useless but an error in Select2 JS’ point of view. There are other such intricacies, based on which some options are removed. By enforcing this restriction we make sure to not break the code by passing some wrong concotion of options. |
|---|
The options listed in this are rendered as JS map and passed to Select2 JS code. The complete description of theses options are available in Select2 JS’ site.
Sub-classes can use this to pass additional options to Select2 JS library.
Example:
def init_options(self):
self.options['createSearchChoice'] = JSFunction('Your_js_function')
In the above example we are setting Your_js_function as Select2’s createSearchChoice function.
Tip
If you want to run Your_js_function in the context of the Select2 DOM element, i.e. this inside your JS function should point to the component instead of window, then use JSFunctionInContext instead of JSFunction.
Placeholder is a value which Select2 JS library shows when nothing is selected. This should be string.
| Returns: | None |
|---|
Renders options for Select2 JS.
| Returns: | The rendered JS code. |
|---|---|
| Return type: | unicode |
Renders the <script> block which contains the JS code for this widget.
| Returns: | The rendered JS code enclosed inside <script> block. |
|---|---|
| Return type: | unicode |
Bases: django_select2.widgets.Select2Mixin, django.forms.widgets.Select
Drop-in Select2 replacement for forms.Select.
Following Select2 option from Select2Mixin.options is removed:-
- multiple
Bases: django_select2.widgets.Select2Mixin, django.forms.widgets.SelectMultiple
Drop-in Select2 replacement for forms.SelectMultiple.
Following Select2 options from Select2Mixin.options are removed:-
- multiple
- allowClear
- minimumResultsForSearch
Bases: django.forms.widgets.TextInput
Multiple hidden input for Select2.
This is a specialized multiple Hidden Input widget. This includes a special JS component which renders multiple Hidden Input boxes as there are values. So, if user suppose chooses values 1, 4 and 9 then Select2 would would write them to the primary hidden input. The JS component of this widget will read that value and will render three more hidden input boxes each with values 1, 4 and 9 respectively. They will all share the name of this field, and the name of the primary source hidden input would be removed. This way, when submitted all the selected values would be available as list.
Bases: django_select2.widgets.Select2Mixin
The base mixin of all Heavy Select2 widgets. It sub-classes Select2Mixin.
This mixin adds more Select2 options to Select2Mixin.options. These are:-
minimumInputLength: 2
initSelection: JSFunction('django_select2.onInit')
- ajax:
- dataType: 'json'
- quietMillis: 100
- data: JSFunctionInContext('django_select2.get_url_params')
- results: JSFunctionInContext('django_select2.process_results')
Tip
You can override these options by passing select2_options kwarg to __init__().
Constructor of the class.
The following kwargs are allowed:-
| Parameters: |
|
|---|
Tip
When data_view is provided then it is converted into Url using reverse().
Warning
Either of data_view or data_url must be specified, else ValueError would be raised.
| Parameters: |
|
|---|
Renders a JS array with labels for the selected_choices.
| Parameters: | |
|---|---|
| Returns: | The rendered JS array code. |
| Return type: |
Renders the JS code which sets the txt attribute on the field. It gets the array of lables from render_texts().
| Parameters: | |
|---|---|
| Returns: | JS code which sets the txt attribute. |
| Return type: |
Bases: django_select2.widgets.HeavySelect2Mixin, django.forms.widgets.TextInput
Single selection heavy widget.
Following Select2 option from Select2Mixin.options is added or set:-
- multiple: False
Bases: django_select2.widgets.HeavySelect2Mixin, django_select2.widgets.MultipleSelect2HiddenInput
Multiple selection heavy widget.
Following Select2 options from Select2Mixin.options are removed:-
- allowClear
- minimumResultsForSearch
Following Select2 options from Select2Mixin.options are added or set:-
- multiple: False
- separator: JSVar('django_select2.MULTISEPARATOR')
Renders the JS code which sets the txt attribute on the field. It gets the array of lables from render_texts().
| Parameters: |
|
|---|---|
| Returns: | JS code which sets the txt attribute. |
| Return type: |
Bases: object
This mixin is needed for Auto heavy fields.
This mxin adds extra JS code to notify the field’s DOM object of the generated id. The generated id is not the same as the id attribute of the field’s Html markup. This id is generated by register_field() when the Auto field is registered. The client side (DOM) sends this id along with the Ajax request, so that the central view can identify which field should be used to serve the request.
Bases: django_select2.widgets.AutoHeavySelect2Mixin, django_select2.widgets.HeavySelect2Widget
Auto version of HeavySelect2Widget
Bases: django_select2.widgets.AutoHeavySelect2Mixin, django_select2.widgets.HeavySelect2MultipleWidget
Auto version of HeavySelect2MultipleWidget