Created
November 27, 2013 21:21
-
-
Save czpython/7683425 to your computer and use it in GitHub Desktop.
Django Readonly Select2 widget https://github.com/applegrew/django-select2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from django_select2.widgets import Select2MultipleWidget | |
class ReadonlySelect2MultipleWidget(Select2MultipleWidget): | |
def render_inner_js_code(self, id_, *args): | |
""" | |
Renders all the JS code required for this widget. | |
:return: The rendered JS code which will be later enclosed inside ``<script>`` block. | |
:rtype: :py:obj:`unicode` | |
""" | |
options = dict(self.get_options()) | |
options = self.render_select2_options_code(options, id_) | |
# I know how this looks, but setting the "readonly" option to true is not enough :( | |
js = u""" | |
$("#{id}").select2({options}); | |
$("#{id}").select2("readonly", true); | |
""" | |
return js.format(id=id_, options=options) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment