Skip to content

Commit

Permalink
Pass record/value to LinkColumn's attrs callables too (jieter#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsldankers authored and Alirezaja1384 committed Dec 27, 2022
1 parent df3030f commit 04907bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
7 changes: 6 additions & 1 deletion django_tables2/columns/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ def __init__(self, url=None, accessor=None, attrs=None, reverse_args=None):
accessor (Accessor): if supplied, the accessor will be used to decide on which object
``get_absolute_url()`` is called.
attrs (dict): Customize attributes for the ``<a>`` tag.
Values of the dict can be either static text or a
callable. The callable can optionally declare any subset
of the following keyword arguments: value, record, column,
bound_column, bound_row, table. These arguments will then
be passed automatically.
reverse_args (dict, tuple): Arguments to ``django.urls.reverse()``. If dict, the arguments
are assumed to be keyword arguments to ``reverse()``, if tuple, a ``(viewname, args)``
or ``(viewname, kwargs)``
Expand Down Expand Up @@ -143,7 +148,7 @@ def resolve_if_accessor(val):
return reverse(**params)

def get_attrs(self, **kwargs):
attrs = AttributeDict(self.attrs or {})
attrs = AttributeDict(computed_values(self.attrs or {}, kwargs=kwargs))
attrs["href"] = self.compose_url(**kwargs)

return attrs
Expand Down
15 changes: 9 additions & 6 deletions tests/columns/test_linkcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,22 @@ class PersonTable(tables.Table):

def test_a_attrs_should_be_supported(self):
class TestTable(tables.Table):
col = tables.LinkColumn(
"occupation", kwargs={"pk": A("col")}, attrs={"a": {"title": "Occupation Title"}}
)
attrs = {"a": {"title": "Occupation Title", "id": lambda record: str(record["id"])}}
col = tables.LinkColumn("occupation", kwargs={"pk": A("col")}, attrs=attrs)
col_linkify = tables.Column(
accessor="col",
attrs={"a": {"title": "Occupation Title"}},
attrs=attrs,
linkify=("occupation", {"pk": A("col")}),
)

table = TestTable([{"col": 0}])
table = TestTable([{"col": 0, "id": 1}])
self.assertEqual(
attrs(table.rows[0].get_cell("col")),
{"href": reverse("occupation", kwargs={"pk": 0}), "title": "Occupation Title"},
{
"href": reverse("occupation", kwargs={"pk": 0}),
"title": "Occupation Title",
"id": "1",
},
)
self.assertEqual(table.rows[0].get_cell("col"), table.rows[0].get_cell("col_linkify"))

Expand Down

0 comments on commit 04907bd

Please sign in to comment.