Thursday, August 21, 2014

Re: Change color of table cell based in another cell value django-tables2

I have never used "django-tables2" package but traditionally you'd do something like class="color_{{item.reception_status}}" which will render class="color_3" or class="color_1" and then use CSS to style it. What does cell render? Can you access reception_status from it as in {{ cell.reception_status }} and then use an if statement like this {%if cell.reception_status == 3%}<div class="green">{{cell}}</div>{%endif%}

On 22/08/2014 6:19 am, "Jose Elvir" <jlvirgo@gmail.com> wrote:
I have a html table (django-tables2) based on a model

class MasterTable(tables.Table):
   
class Meta:
       model
= Master
       fields
= ("name","reception_date","reception_status","amount_files")


I need to change the color of some cell based in the value of others cell.
Example :
name will be red if reception_status is 3
name will be green if reception_status is 1
amount_files will be red if the value is less than 10
amount_files will be green if the value is greater than 10

This is my code to render the table in the template:

<table class="table table-compact table-bordered"{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
        {% block table.thead %}
           
<thead>
           
<tr>
                {% for column in table.columns %}
                       
<th {{ column.attrs.th.as_html }}><a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a></th>
                {% endfor %}
           
</tr>
           
</thead>
        {% endblock table.thead %}
        {% block table.tbody %}
           
<tbody>
            {% for row in table.page.object_list|default:table.rows %} {# support pagination #}
                {% block table.tbody.row %}
                   
<tr class="{% cycle "odd" "even" %}">
                        {% for column, cell in row.items %}
                           
<td {{ column.attrs.td.as_html }}>{{ cell }}</td>
                        {% endfor %}
                   
</tr>
                {% endblock table.tbody.row %}
            {% empty %}
                {% if table.empty_text %}
                    {% block table.tbody.empty_text %}
                       
<tr><td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td></tr>
                    {% endblock table.tbody.empty_text %}
                {% endif %}
            {% endfor %}
           
</tbody>
   
</table>

I don't know how to evaluate the value of the cell to change the color of the others cells.
Help please.
Thanks in advance



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3b48345a-e929-4844-b845-313659a1d687%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHqTbj%3DpgPpgLS3ck%2ByPDfBWWgitaXLCYJL9Dd7KnWW8M7-Xgg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment