Last active
August 29, 2015 14:15
-
-
Save alexanderjulo/f59c25ad2ae34d87f23d to your computer and use it in GitHub Desktop.
Revisions
-
Alexander Jung-Loddenkemper revised this gist
Feb 8, 2015 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ from wtforms import Form from wtforms.fields import Select class ExampleForm(Form): example = Select("Example", choices=[("1", "1"), ("2", "2")], widget=ChosenSelect()) -
Alexander Jung-Loddenkemper created this gist
Feb 8, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ import json from wtforms.widgets import Select, HTMLString class ChosenSelect(Select): def __init__(self, multiple=False, renderer=None, options={}): """ Initiate the widget. This offers you two general options. First off it allows you to configure the ChosenSelect to allow multiple options and it allows you to pass options to the chosen select (this will produce a json object) that chosen will get passed as configuration. :param multiple: whether this is a multiple select (default to `False`) :param renderer: If you do not want to use the default select renderer, you can pass a function that will get the field and options as arguments so that you can customize the rendering. :param options: a dictionary of options that will influence the chosen behavior. If no options are given `width: 100%` will be set. """ super(ChosenSelect, self).__init__(multiple=multiple) self.renderer = renderer options.setdefault('width', '100%') self.options = options def __call__(self, field, **kwargs): """ Render the actual select. :param field: the field to render :param **kwargs: options to pass to the rendering (i.e. class, data-* and so on) This will render the select as is and attach a chosen initiator script for the given id afterwards considering the options set up in the beginning. """ kwargs.setdefault('id', field.id) # currently chosen does not reflect the readonly attribute # we compensate for that by automatically setting disabled, # if readonly if given # https://github.com/harvesthq/chosen/issues/67 if kwargs.get("readonly"): kwargs['disabled'] = 'disabled' html = [] # render the select if self.renderer: html.append(self.renderer(self, field, **kwargs)) else: html.append(super(ChosenSelect, self).__call__(field, **kwargs)) # attach the chosen initiation with options html.append( '<script>$("#%s").chosen(%s);</script>\n' % (kwargs['id'], json.dumps(self.options)) ) # return the HTML (as safe markup) return HTMLString('\n'.join(html))