3

I've got a table to generate based on a list of objects and I'd like to show one more column (checkboxcolumn) whose value would be set based on some data in my objects. I tried to use CheckBoxColumn shipped with django-tables2 but it only generates a checkbox in my header and all data rows display '- ' (minus sign with space). There is very little information about that particular column type on the Internet so I couldn't find any solution to this problem. Here is my code for the table:

class MyTable(ScheduleTable):
    checkbox_column = CheckBoxColumn()

    class Meta:
        order_by = "-end"

What I am missing? I tried adding some attributes (like td_input, input or checked parameter) but nothing worked.

Daniel

Aranha
  • 2,903
  • 3
  • 14
  • 29

3 Answers3

2

I ran into this issue as well when using a CheckBoxColumn but not passing in any arguments. There was no value on my table elements, just <td>-</td>. What solved it for me was passing in the accessor argument.

class MyTable(ScheduleTable):
    checkbox_column = CheckBoxColumn(accessor='pk')
0

Shouldn't you be using a model for your table to put data from ? as for the value that will be passed to the table, you can use args

from django_tables2.utils import A

class MyTable(ScheduleTable):
  checkbox_column = CheckBoxColumn(args=[A('pk')])

  class Meta:
      model = MyModel
      order_by = "-end"
iOReK
  • 1
  • 4
0

This was happening to me too. When I inspected the missing box in the cell it was clear that it was getting the correct html:

<input type="checkbox" name="select_box" value="59">

So I monkeyed with the css in Chrome dev tools and found that it wasn't visible due to height & width (I'm not sure why - not a css expert). But once I put height and width to auto or a specific px amount I could see the boxes.

In tables.py I added a class to the td__input attr. From the docs:

Arguments: attrs (dict): In addition to attrs keys supported by ~.Column, the following are available:

         - ``input``     -- ``<input>`` elements in both ``<td>`` and ``<th>``.
         - ``th__input`` -- Replaces ``input`` attrs in header cells.
         - ``td__input`` -- Replaces ``input`` attrs in body cells.

So I added attrs like so

select_box = CustomCheckboxColumn(
    accessor ='index',
    attrs={
        'td__input': {'class':'checkbox_local'},
        },
)
Liam Hanninen
  • 1,525
  • 2
  • 19
  • 37