Displays as an ordinary selectbox with an additional text-input for filtering the options. An adaption of the example at [1] for use with Django.
The code assumes the java script from [2] is downloaded into your media folder. Remember to output your form's media, for instance like: "{{form.media|safe}}", somewhere above your form.
License: [2] by Mr Patrick Fitzgerald is licensed under GPL and the python code written by myself is GPL as well.
References:
- http://www.barelyfitz.com/projects/filterlist/index.php/
- http://www.barelyfitz.com/projects/filterlist/filterlist.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class FilteredSelect(forms.widgets.Select):
"""
Displays as an ordinary selectbox with an additional text-input for filtering the options.
Requires: http://www.barelyfitz.com/projects/filterlist/filterlist.js
Author: Konrad Giæver Beiske
Company: Linpro
"""
class Media:
js = ('filterlist.js', )
pre_html = """
Filter: <INPUT NAME=regexp onKeyUp="%s_filter.set(this.value)"/>
<INPUT TYPE=checkbox NAME="toLowerCase"
onClick="%s_filter.set_ignore_case(!this.checked)"
/> Case-sensitive <br>
"""
post_html = """
<SCRIPT TYPE="text/javascript">
<!--
var %s_filter = new filterlist(document.getElementById("id_%s"));
//-->
</SCRIPT>
"""
def render(self, name, value, attrs=None, choices=()):
super_res = super(FilteredSelect, self).render(name, value, attrs=attrs, choices=choices)
return (self.pre_html%(name, name)) + super_res + (self.post_html%(name, name))
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.