Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Select Multiple Widget Horizontal

I have option groups, somethings like this:

Choices = (('Group1',(('Option1','Option1'),('Option2','Option2'))))

Is there anyway to display the options horizontally instead of vertically using the SelectMultiple Widget? My problem is that I have too many Groups, so if i could have something like the following it would be better.

Group 1 - Option1, Option2, ...
Group 2 - Option1, Option2, ...

Update - Found this but it only "works" for CheckboxSelectMultiple. How should I change it in order to work with SelectMultiple?

from django.utils.safestring import mark_safe

class HorizWidget(forms.SelectMultiple):

    def render(self, *args, **kwargs):
        output = super(HorizWidget, self).render(*args,**kwargs) 
        return mark_safe(output.replace(u'<ul>', u'').replace(u'</ul>', u'').replace(u'<li>', u'<p>').replace(u'</li>', u'</p>'))
like image 993
Pedro Avatar asked Feb 21 '26 22:02

Pedro


1 Answers

Are you using the Django admin? If so, the filter_horizontal and filter_vertical options can make multiple selects much more usable (I must admit, I haven't tried them before with grouped choices).

If that doesn't help, then you can subclass the SelectMultiple widget and override the render methods as you wish. I personally prefer to use javascript widget plugins where possible (e.g. jquery-ui-multiselect). They can often work with just a few lines of javascript in your template header, which I find a lot less painful than writing custom widget html.

like image 97
Alasdair Avatar answered Feb 24 '26 14:02

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!