- Author:
- maguspk
- Posted:
- June 23, 2010
- Language:
- Python
- Version:
- 1.2
- Tags:
- dropdown jquery select widget jqueryui
- Score:
- 0 (after 0 ratings)
This widget can be imported in your forms.py file and used like so:
formfield = forms.ModelChoiceField(widget=SelectDropdownWidget, queryset=Model.objects.all())
The javascript is provided by filament group's Scott and his jQueryUI Selectmenu. In addition to the javascript you will need jQuery (the latest release is fine) and jQuery UI. The convenient thing is that this widget will style to match whichever jQuery UI you've 'rolled' or selected, making it highly customizable!
Hopefully this helps others who wanted a nice widget with jQuery and were sad to find there were no nice default options, enjoy! :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class SelectDropdownWidget(forms.Select):
class Media:
css = {
'all': (
iasettings.MEDIA_URL + 'js/selectmenu/ui.selectmenu.css',
)
}
js = (
iasettings.MEDIA_URL + 'js/selectmenu/ui.selectmenu.js',
)
def __init__(self, language=None, attrs=None):
self.language = language or settings.LANGUAGE_CODE[:2]
super(SelectDropdownWidget, self).__init__(attrs=attrs)
def render(self, name, value, attrs=None):
rendered = super(SelectDropdownWidget, self).render(name, value, attrs)
return rendered + mark_safe(u'''<script type="text/javascript">
$(document).ready(function afterReady() {
var elem = $('#id_%(name)s');
elem.selectmenu();
});
</script>''' % {'name':name})
|
More like this
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 8 months, 1 week ago
- Crispy Form by sourabhsinha396 9 months ago
- ReadOnlySelect by mkoistinen 9 months, 2 weeks ago
- Verify events sent to your webhook endpoints by santos22 10 months, 2 weeks ago
- Django Language Middleware by agusmakmun 10 months, 3 weeks ago
Comments
Please login first before commenting.