djk2 / django-tables2-column-shifter

Simple extension for django-tables2 can dynamically show or hide columns. Using JQuery, Bootstrap 3, Bootstrap 4, Bootstrap 5 and Django >=1.9
BSD 3-Clause "New" or "Revised" License
21 stars 8 forks source link

Is it possible to "respect" django-tables2 template ? #17

Closed spapas closed 3 years ago

spapas commented 4 years ago

A django-tables2 package has the template_name option to choose the template used:

For example something like

class CPVTable(tables.Table):
    class Meta:
        model = models.CPV
        fields = ("id", "code", "name", "name_en")
        attrs = {"class": "table table-sm table-stripped"}
        template_name = "cpv-table.html"

The problem is that when my table inherits from ColumnShiftTable (instead of Table) this attribute is completely ignored. Whould it be possible somehow to respect that parameter? I understand that it may not be possible because the template must follow a particular layout in order to be compatible with django-tables2-column-shifter.

Thank you

djk2 commented 3 years ago

Why don't you override a shifter_template attr from ColumnShiftTable? This attribute finally override template_name attr from origin Table class.

class ColumnShiftTable(tables.Table):
    ....
    shifter_template = "django_tables2_column_shifter/table.html"
    ....
    def __init__(self, *args, **kwargs):
        ....
        if hasattr(self, "template_name"):
            self.template_name = self.shifter_template
        else:
            self.template = self.shifter_template
        ....

You can create your own template for shifter and override shifter_template attribute in ColumnShiftTable class. Your custom template can also inherit from django-tables2-column-shifter.templates.tables for example Custom Templase: {% extends "django_tables2_column_shifter/bootstrap4.html" %}

spapas commented 3 years ago

I'll try it thank you!