1 from django import forms
2 from django.conf import settings
3 from django.utils.safestring import mark_safe
4 from django.utils.translation import ugettext_lazy as _
6 from sponsors import models
9 class OrderedSelectMultiple(forms.TextInput):
11 A SelectMultiple with a JavaScript interface.
15 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
16 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js',
17 settings.MEDIA_URL + 'js/ordered_select_multiple.js',
20 def render(self, name, value, attrs=None, choices=()):
21 output = [super(OrderedSelectMultiple, self).render(name, value, attrs)]
22 choices = [(unicode(obj), obj.pk) for obj in models.Sponsor.objects.all()]
23 choices_js = ', '.join('{name: "%s", id: %d}' % choice for choice in choices)
24 output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
25 # TODO: "id_" is hard-coded here. This should instead use the correct
26 # API to determine the ID dynamically.
27 output.append(u'$("#id_%s").orderedSelectMultiple({choices: [%s]}); });</script>\n' %
29 return mark_safe(u''.join(output))