django-tables2 specify different properties for different rows

眉间皱痕 提交于 2019-12-05 04:30:15
osa

Well, let me post my own solution.

I have copied the standard template table.html and edited it. I only changed one line:

<tbody>
    {% for row in table.page.object_list|default:table.rows %} {# support pagination #}
    {% block table.tbody.row %}
    <tr class="{{ row.tr_class }}">  <!-- CLASS FOR EACH ROW -->

instead of

    <tr class="{% cycle "odd" "even" %}">

This way you can set a different class for each row in the table. It remains to add an invisible column to your table class:

class MyTable(tables.Table):
  tr_class=tables.Column(visible=False)
  ... # other columns

After that, whenever you are creating a table, you can set any CSS classes for any particular rows. Remember to use the modified template:

{% render_table div_table "modifiedtable.html" %}  

Of course, you can also change the original table.html.

Can anybody suggest a more graceful solution?

Overall, I have a feeling that django_tables2 is still missing many important features, so I have to reinvent the wheel every time I am trying to do something nontrivial.

Define tr_class

To use this, you must use custom rendering. For example:

class MyTable(tables.Table):
 tr_class=tables.Column(visible=False, empty_values=())
 def render_tr_class(self, value):
   if value.chosen == True:
     return 'highlight'

And the tr would be given the class highlight.

Paritosh Gupta

I have a very easy workaround for this

class MyTable(tables.Table):
    source = tables.Column()

    def render_source(self, value):
        if value == 'some_value':
            return mark_safe("<span class='highlight_this_row'>%s</span>" % (escape(value)))
        else:
            return value

Then instead of creating a complete custom HTML page for custom rendering, you could just use jQuery to actually highlight the row.

$('.highlight_this_row').parent().parent().addClass('highlight');

If you don't have a class "highlight" defined, you can define it as:

<style>
    .highlight{
        background-color: black
    }
</style>

There's another way (also not very beautiful), but here you don't have to define a fake column.

First, you extend django_tables2.rows.BoundRows:

class ColoredBoundRows(BoundRows):
    def __iter__(self):
        for record in self.data:
            row = BoundRow(record, table=self.table)
            row.style = 'some_class'
            yield row

    def __getitem__(self, key):
        container = ColoredBoundRows if isinstance(key, slice) else BoundRow
        return container(self.data[key], table=self.table)

Then make your Table use it:

class YourTable(Table):
    def __init__(self, *args, **kwargs):
        super(YourTable, self).__init__(*args, **kwargs)
        self.rows = ColoredBoundRows(data=self.data, table=self)

Then define a new template (note that you only have to overwrite one block):

{% extends "django_tables2/table.html" %}

{% block table.tbody.row %}
<tr class="{% cycle "odd" "even" %} {{ row.style }}">
  {% for column, cell in row.items %}
    <td {{ column.attrs.td.as_html }}>{{ cell }}</td>
  {% endfor %}
</tr>
{% endblock table.tbody.row %}

I actually couldn't get the accepted answer to work for this, perhaps the latest version of django_tables2 is different. Here is my solution, which doesn't require modifying the tables.html template and is based on the documentation.

First define row_attrs as part of the class Meta

class MyTable(tables.Table):
    my_column = tables.Column()

    class Meta:
        row_attrs = {'class': my_custom_row_attrs}

You can then define a function outside the scope of MyTable to work out your row attributes depending on the values in the individual row record

def my_custom_row_attrs(**kwargs):
    '''My function to generate custom row attributes
    '''

    record = kwargs.get('record', None)
    tr_class = ''

    if record:
        # Only do comparison if pat_date is present
        if record.my_column.value == True:
            tr_class = 'highlight'

    return tr_class

I would rather keep all this functionality within the class, but this seemed the best way to do it to avoid modifying anything else.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!