Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass record/value to CheckboxColumn's attrs callables too #774

Merged
merged 2 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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