views:

34

answers:

2

Overlap in terminology makes search for answers difficult for this one.

I'm looking for advice on the best way to implement a multiple-column display of my QuerySet that fills each column top to bottom over X columns. Meaning that the number of items in each column equals the QuerySet count divided by X (number of columns).

Using Offset doesn't work for this because I would like my data to grow into 4 columns without updating the offset manually. CSS floats work visually, but leave the data out of order.

+1  A: 

Something like that should work for you, pass the number of columns as columns to the template:

{% for item in items %}
    {% if forloop.first %}<div style="float:left;">{% endif %}
    {{ item }}
    {% if forloop.counter|divisbleby:columns %}
        </div><div style="float:left">
    {% endif %}
    {% if forloop.last %}</div>{% endif %}    
{% endfor %}
<div style="clear:both;"></div>
lazerscience
A: 

It seems like lazerscience's answer is on the right track - but I think the OP wants the data to alphabetically sort the data down the column and then start back at the top of the next column. Using divisibleby: works to break the line after 'X' items, but I think it'll take a more complex template to do what the OP wants.

Not sure if it's possible, but in the view could you: Get item count, divide by 'columns' and used that # in Divisibleby to break into the next DIV column (the visual flow will be CSS)

As Lazers's example is now you're constructing Rows and then breaking it into columns, leaving the sort order across and then down.

Sorry if I missed something.

-K

Kim
My Example sorts the items down the columns, and THEN start the next column, it would firstly construct a row f you would put every single item into its own floating div!
lazerscience
I see, you are correct. But you are still defining how many items are in the column, not how many columns their are. For display use, this isn't very useful, as a list grows from 10 to 200 items, the column count gets absurd unless you manually update the divisibleby:#. Instead, it would be useful to say I can fit 3 columns in my design, split the dataset into three chunks (1-10, 11-20, 21-30 for a 30 item list) and calculate the count of each column on the fly (1/3rd of the query set)
Kim