Mokgadi Rasekgala
Mokgadi Rasekgala

Reputation: 111

Using extra_columns in django_tables2.tables.Table

Trying to use extra_colums and get no error but the table does not show up. I've use the documentation from here. I am trying to add a column which will have checkboxes into the table. I have already predefined the table and can exclude some fields but with using the documentation I cannot figure out how to add a new column. I must be missing something

The implementation can be seen below. Would appreciate any assistance. Thanks

IN TABLES.PY

import django_tables2 as tables
from project.models import Project

class ProjectTable(tables.Table):
   project_name = tables.TemplateColumn("""
        {%if record.department == "TRACER"%}
                <a href=" {% url 'project_detail' record.id %}">
                    {{ value }}
                </a>
        {%else%}
           {{value}}
        {%endif%}

        """, orderable=True, accessor='name', verbose_name="Project 
        name")

   project_type = tables.TemplateColumn("""
        {% if value == 'Yes' %}Special{% else %}Normal{% endif %}
       """, orderable=True, accessor='is_special', 
       verbose_name="Project type")
   project_code = tables.Column(accessor='code', verbose_name="Project 
          code")

   project_status = tables.Column(accessor='status', 
      verbose_name="Project status")

   department = tables.Column(accessor='department', 
      verbose_name="Department")



   class Meta:
      model = Project
      attrs = {'class': 'table table-striped table-hover'}
      sequence = (
        'project_name', 'project_type', 'project_code',
         'project_status',)

IN THE VIEW

from project.tables import ProjectTable
from django_tables2.columns import CheckBoxColumn

class AllocationChangeView(PagedFilteredTableView):
   display_message = "You need to be logged in to view this page"
   table_class = ProjectTable
   queryset = Project.objects.all()
   template_name = 'matter_allocation/change_project.html'
   paginate_by = ITEMS_PER_PAGE
   formhelper_class = ProjectFilterFormHelper
   filter_class = ProjectFilter

def get_context_data(self, **kwargs):
    context = super(AllocationChangeView,
                    self).get_context_data(**kwargs)
    table = context['table']
    table.exclude = ('project_status','department')
    table.extra_columns =(('Change',CheckBoxColumn(checked=False)),)
    context['table'] = table
    return context

Upvotes: 0

Views: 2086

Answers (1)

Jieter
Jieter

Reputation: 4229

You are setting an attribute on the table instance, not passing extra_columns as an argument to the table constructor. Using the extra_columns should look something like this:

class MyTable(tables.Table):
    name = tables.Column()

table = MyTable(data, order_by='-country', extra_columns=[
    ('country', tables.Column(verbose_name=_('country')))
])

In your case, using the class based views, the table is created in the get_table method of your view. The get_table_kwargs method allows adding kwargs to the table creation call, so this should do the trick:

class AllocationChangeView(PagedFilteredTableView):
   display_message = "You need to be logged in to view this page"
   table_class = ProjectTable
   queryset = Project.objects.all()
   template_name = 'matter_allocation/change_project.html'
   paginate_by = ITEMS_PER_PAGE
   formhelper_class = ProjectFilterFormHelper
   filter_class = ProjectFilter

   def get_table_kwargs(self):
       return {
           'extra_columns': (('Change', CheckBoxColumn(checked=False)),),
           'exclude': ('project_status', 'department')
       }

Upvotes: 4

Related Questions