Skip to content

Commit

Permalink
Pass records to CheckboxColumn's attrs too
Browse files Browse the repository at this point in the history
fixes: #762
  • Loading branch information
jieter committed Oct 15, 2020
1 parent 29ebdfa commit a780c23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions django_tables2/columns/checkboxcolumn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.utils.safestring import mark_safe

from django_tables2.utils import Accessor, AttributeDict
from django_tables2.utils import Accessor, AttributeDict, computed_values

from .base import Column, library

Expand Down Expand Up @@ -65,8 +65,10 @@ def render(self, value, bound_column, record):

general = self.attrs.get("input")
specific = self.attrs.get("td__input")
attrs = AttributeDict(default, **(specific or general or {}))
return mark_safe("<input %s/>" % attrs.as_html())

attrs = dict(default, **(specific or general or {}))
attrs = computed_values(attrs, kwargs={"record": record, "value": value})
return mark_safe("<input %s/>" % AttributeDict(attrs).as_html())

def is_checked(self, value, record):
"""
Expand Down
14 changes: 14 additions & 0 deletions tests/columns/test_checkboxcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ class TestTable(tables.Table):
"value": "2",
"name": "col",
}

def test_column_callable_attrs(self):
class TestTable(tables.Table):
col = tables.CheckBoxColumn(
attrs={"input": {"data-source": lambda record: record["col"]}}
)

table = TestTable([{"col": "1"}])
assert attrs(table.rows[0].get_cell("col")) == {
"type": "checkbox",
"value": "1",
"name": "col",
"data-source": "1",
}

0 comments on commit a780c23

Please sign in to comment.