Home > Net >  Highlighting the first repeating loop element in django
Highlighting the first repeating loop element in django

Time:08-16

Please tell me how to select only the first repeating element of the cycle in Jinja django?

<table id="{{el.slug}}_table"  cellspacing="0" width="100%" >
                <thead>
                  <tr>
                    <th>expand</th>
                    <th>Тип алерта </th>
                    <th>hostname</th>
                    <th>username</th>
                    <th>message</th>
                    <th>Actions</th>
                  </tr>
                  
                </thead>
                <tbody>
                  
                  {% regroup alerts|dictsort:"username" by username as alrt %}
                  {% for all in alrt %}
                  
                    {% for ell in all.list %}
                     
                      {% if el.title == ell.title  %}
                        {% if forloop.first %}
                        <tr >
                          <td > </td>
                          <td >{{ell.title}}</td>
                          <td>{{ell.hostname}}</td>
                          <td>{{ell.username}}</td>
                          <td>{{ell.message}}</td>
                          <td><a href="#"><i  aria-hidden="true"></i></a> <a href="#"><i  aria-hidden="true"></i></a> <a href="#"><i  aria-hidden="true"></i></a> <a href="#"><i  aria-hidden="true"></i></a> <a href="#"><i  aria-hidden="true"></i></i></a></td>
                        </tr>
                        {% else %}
                        <tr >
                          <td > </td>
                          <td >{{ell.title}}</td>
                          <td>{{ell.hostname}}</td>
                          <td>{{ell.username}}</td>
                          <td>{{ell.message}}</td>
                          <td><a href="#"><i  aria-hidden="true"></i></a> <a href="#"><i  aria-hidden="true"></i></a> <a href="#"><i  aria-hidden="true"></i></a> <a href="#"><i  aria-hidden="true"></i></a> <a href="#"><i  aria-hidden="true"></i></i></a></td>
                        </tr>
                        {% endif %}
                      {% endif %}
                    {% endfor %}
                  {% endfor %}
                </tbody>
              </table>

At the moment, it turned out to select only the first element in the loop, and you need the first repeating one

CodePudding user response:

at first. all what you need {% ifchanged %} - in documentation, here: https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#ifchanged

at second. i am not really shure, what you want to do.

at third. i add {% cycle %} to made you code a little bit better.

<tbody>
...... some staff here              
    {% for all in alrt %}
        {% for ell in all.list %}
            {% ifchanged el.title %}
                <tr >
                   <td > </td>
                   ...... some staff here
                </tr>
            {% endif %}
        {% endfor %}
    {% endfor %}
</tbody>

By the way, if you use russian in your tmplates, try to wrap it in trans, it can be better in future:

<th>{% trans 'Тип алерта ' %}</th>

CodePudding user response:

Thank you for your answer and I apologize for not phrasing the question correctly) I'm going to achieve to group all the duplicate elements in the output and make them into a drop-down list. So that by clicking all the elements that have the same "username" drop out under the element we clicked on.

enter image description here

  • Related