3

I've created my table output with django-tables2, but I want a specific name in the table column for the check-boxes.

How can I do that ?

Here is my class for the table drawing, I've modified the sequence of the columns so that my check-box is the first one.

class SimpleTable(tables.Table):
    amend = tables.CheckBoxColumn(verbose_name=('Amend'), accessor='pk')

    class Meta:
        model = SysimpReleaseTracker
        attrs = {"class": "paleblue"}
        listAttrs = list()
        listAttr = list()
        listAttrs = SysimpReleaseTracker._meta.fields

        listAttr.append('amend')
        for x in listAttrs:
            listAttr.append('%s' %x.name)
        sequence = listAttr
César
  • 9,939
  • 6
  • 53
  • 74
Bogdan
  • 349
  • 1
  • 4
  • 15
  • To make `amend` the first column, all you have to do is `sequence = ("amend", "...")`. Here's the doc for [sequence](http://django-tables2.readthedocs.org/en/latest/#Table.Meta.sequence) – SunnySydeUp Jun 09 '14 at 12:41
  • The sequence is modified and all the existing columns are added directly from the model because there are more than 20 and also if someone will add a new column to the model, it will be displayed using the TABLE._meta.fields that it's inserted into the list. My problem is with the check box column, that one doesn't have a name but a simple check-box in the header as well :( – Bogdan Jun 09 '14 at 13:08

1 Answers1

8

CheckBoxColumn has it's own unique rendering for the header and so it will not show the verbose_name unlike all the other columns. You could subclass CheckBoxColumn to change it's behavior though.

class CheckBoxColumnWithName(tables.CheckBoxColumn):
    @property
    def header(self):
        return self.verbose_name

class SimpleTable(tables.Table):  
    amend = CheckBoxColumnWithName(verbose_name="Amend", accessor="pk")

Alternatively, you could use a TemplateColumn.

class SimpleTable(tables.Table):  
    amend = TemplateColumn('<input type="checkbox" value="{{ record.pk }}" />', verbose_name="Amend")
SunnySydeUp
  • 6,680
  • 4
  • 28
  • 32