【问题标题】:django-tables2 CheckBoxColumn - only header checkbox is generateddjango-tables2 CheckBoxColumn - 仅生成标题复选框
【发布时间】:2017-04-05 03:09:10
【问题描述】:
我有一个要根据对象列表生成的表,我想再显示一个列 (checkboxcolumn),其值将根据我的对象中的某些数据设置。我尝试使用 django-tables2 附带的 CheckBoxColumn,但它只在我的标题中生成一个复选框,并且所有数据行都显示“-”(带空格的减号)。 Internet 上有关该特定列类型的信息很少,因此我找不到任何解决此问题的方法。这是我的表格代码:
class MyTable(ScheduleTable):
checkbox_column = CheckBoxColumn()
class Meta:
order_by = "-end"
我错过了什么?我尝试添加一些属性(如 td_input、输入或检查参数),但没有任何效果。
丹尼尔
【问题讨论】:
标签:
python
django
checkbox
django-tables2
【解决方案1】:
我在使用 CheckBoxColumn 但未传入任何参数时也遇到了这个问题。我的表格元素没有任何价值,只有<td>-</td>。为我解决的问题是传入访问器参数。
class MyTable(ScheduleTable):
checkbox_column = CheckBoxColumn(accessor='pk')
【解决方案2】:
您不应该为您的表使用模型来放置数据吗?
至于将传递给表的值,您可以使用 args
from django_tables2.utils import A
class MyTable(ScheduleTable):
checkbox_column = CheckBoxColumn(args=[A('pk')])
class Meta:
model = MyModel
order_by = "-end"
【解决方案3】:
这也发生在我身上。当我检查单元格中丢失的框时,很明显它得到了正确的 html:
<input type="checkbox" name="select_box" value="59">
所以我在 Chrome 开发工具中使用 css,发现由于高度和宽度,它不可见(我不知道为什么 - 不是 css 专家)。但是,一旦我将高度和宽度设置为 auto 或特定的 px 量,我就可以看到这些框。
在 tables.py 中,我向 td__input 属性添加了一个类。来自文档:
参数:
attrs (dict):除了 ~.Column 支持的 attrs 键,
以下是可用的:
- ``input`` -- ``<input>`` elements in both ``<td>`` and ``<th>``.
- ``th__input`` -- Replaces ``input`` attrs in header cells.
- ``td__input`` -- Replaces ``input`` attrs in body cells.
所以我添加了这样的属性
select_box = CustomCheckboxColumn(
accessor ='index',
attrs={
'td__input': {'class':'checkbox_local'},
},
)