0

I am trying to set an attribute of a row in django-tables2 to id of a current record.

To do so I have modified one row in table.html template in such a way:

<tr row_id="{{ record.id }}" class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}">

But this gives me empty row_id="" in html result. What is wrong?

Leo
  • 420
  • 3
  • 18

2 Answers2

1

The tables2 row object is not the record.. but the record is within it. So you can do:

<tr id="row_{{ row.record.id }}" class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}">

Or using a data property

<tr data-record-id="row_{{ row.record.id }}" class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}">

This definitely works.. I am using it.

Also look at the (last) answer here: django-tables2 specify different properties for different rows which suggests using a custom render method to add some html to a cell within each row. That way you don't have to maintain a custom tables2 template. Then with help from jquery it's easy enough to find the element containing the id.. if the user clicks on the row, or whatever it is that you'd like to do...

Community
  • 1
  • 1
little_birdie
  • 5,600
  • 3
  • 23
  • 28
0

The default name for the for variable for django-tables2 table.html (as we see at https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/templates/django_tables2/table.html#L25) is row (and not record which is used for TemplateColumn).

You've not shared your table.html template however I think that this probably why you see an empty row_id. So can you try changing row_id="{{ record.id }}" to row_id="{{ row.id }}" ?

Serafeim
  • 14,962
  • 14
  • 91
  • 133