Skip to content

Commit

Permalink
Pass record/value to CheckboxColumn's attrs callables too (#774)
Browse files Browse the repository at this point in the history
fixes: #762
  • Loading branch information
jieter authored Oct 29, 2020
1 parent 60d0bb5 commit 654e82f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
with:
python-version: 3.8
- uses: actions/checkout@v1
- run: python -m pip install tox
- run: tox -e isort
- run: python -m pip install -r requirements/common.pip isort==5.6.4
- run: isort --diff --check django_tables2 test

tests:
runs-on: ubuntu-latest
Expand Down
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",
}
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ commands = black --check .
basepython = python3.8
deps =
-r requirements/common.pip
isort==4.3.21
commands = isort --diff --check --recursive {toxinidir}
isort==5.6.4
commands = isort --diff --check django_tables2 test

[isort]
multi_line_output = 3
Expand Down

0 comments on commit 654e82f

Please sign in to comment.