I would like to create a table with django-tables2 such that 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 of the rows?
Similarly, if I have a CheckBoxColumn and I specify some data for this column, it goes into the value:
<input type="checkbox" name="col" value="123"/>
This is great for figuring out which checkbox was checked. However, how can I set some checkboxes as checked when the table is created?
My scenario: the user picks some rows from a large table. For example, the table has
The user picks aaple 5 and cucumber 7.
Then I would like to display all apples and all cucumbers, since the user picked at least one apple and at least one cucumber. This allows the user to see other relevant entries:
However, I would like to highlight the entries actually picked by the user by using css and/or by displaying a checked checkbox:
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With