Django-tables2 define different properties for different rows

I would like to create a table with django-tables2, so different rows have different properties.

By default, I get either

<tr class="odd">

or

<tr class="even">

How can I specify my own class for some lines?

Similarly, if I have a CheckBoxColumn, and I specify some data for this column, it goes into a value:

<input type="checkbox" name="col" value="123"/>

This is great for figuring out which check box is selected. However, how can I set some of the checkboxes that were checked when creating the table?

My scenario: the user selects several rows from a large table. For example, a table has

  • orange 1
  • orange 2
  • apple 5
  • orange 3
  • apple 4
  • cucumber 7
  • aaple 1

The user selects applet 5 and cucumber 7.

, . :

  • apple 5
  • apple 4
  • 7

, , css / :

  • apple 5
  • apple 4
  • 7
+5
3

, .

table.html . :

<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 -->

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

. :

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

, , CSS . :

{% render_table div_table "modifiedtable.html" %}  

, table.html.

?

, , django_tables2 , , - .

tr_class

, . :

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

tr highlight.

+8

( ), .

-, 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)

:

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

( , ):

{% 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 %}
+4

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 full custom HTML page for custom rendering, you can simply use jQuery to actually highlight the line.

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

If you do not have a dedicated class, you can define it as:

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

All Articles