grantlee icon indicating copy to clipboard operation
grantlee copied to clipboard

Splitting a list into "N" chunks

Open asmaloney opened this issue 7 years ago • 0 comments

I'm trying to take a list of objects (QList<QObject *>), split it into "N" chunks, and build a separate (HTML) table for each chunk.

I'm doing something like:

  {% range objectList.count as startNum %}
    {% if startNum|divisibleby:16 %}
      {% include "table.html.django" %}
    {% endif %}
  {% endrange %}

I've tried this for table.html.django:

<table>
     <thead><tr><th>Name</th></tr></thead>
     <tbody>
       {% range startNum startNum|add:16 as num %}
       <tr><td>{{ num }} {{ objectList.num.name }}</td></tr>
       {% endrange %}
   </tbody>
 </table>

But that doesn't reference the list item properly and I don't see how to access a specific item in a list when I have a variable ("num"). Is there a way to do this?

I tried using slice, but looking at SliceFilter::doFilter() it seems like it only works on lists of strings. Is that correct?

Any suggestions on how to approach this?

(Also posted on StackOverflow, but no grantlee tag, so I'm guessing not too many people are using it over there.)

asmaloney avatar Aug 14 '17 20:08 asmaloney