OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

django-tables2 change headers for export

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

Guest
I'm trying to create different column header text when exporting a table. The problem (I created) is when I render the table as a page, I wanted to append an icon to the column text to indicate it's sortable and the current direction of the sort. My solution was to create an AbstractTable as follows:

Code:
class AbstractTable(Table):
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

    for i in range(len(self.columns)):
        if is_blank(self.columns[i].column.verbose_name):
            my_label = self.columns[i].name.replace("_", " ").strip()
        else:
            my_label = self.columns[i].column.verbose_name.strip()

        arrow = '⇵' if self.columns[i].orderable else ""

        self.columns[i].column.verbose_name = mark_safe(f'{my_label} {arrow}')
    
def before_render(self, request):
    pprint(vars(request))
    if "sort" in request.GET:
        which_column = request.GET["sort"]
        reverse_sort = True if which_column[0] == "-" else False
        if reverse_sort:
            which_column = which_column[1:]
            arrow = '<i class="fa-solid fa-arrow-down-z-a"></i>'
        else:
            arrow = '<i class="fa-solid fa-arrow-down-a-z"></i>'
        i = [j for j in self.columns if j.name == which_column]

        new_name = i[0].verbose_name.replace(' &#x21f5;', '')

        i[0].column.verbose_name = mark_safe(f'{new_name} {arrow}')

then I do:

Code:
class StudentTable(AbstractTable)

and voila, it all works nicely - although I'm sure it can be improved.

Now the problem, when I export the table, my column headers in the csv or xls have the html code attached. For example, if name is sortable, the export will have name &#x21f5; in the first row instead of just name.

What I can't figure out is how to tell if the request includes an export (or not) so I could modify the name appropriately.
<p>I'm trying to create different column header text when exporting a table. The problem (I created) is when I render the table as a page, I wanted to append an icon to the column text to indicate it's sortable and the current direction of the sort. My solution was to create an AbstractTable as follows:</p>
<pre><code>class AbstractTable(Table):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

for i in range(len(self.columns)):
if is_blank(self.columns.column.verbose_name):
my_label = self.columns.name.replace("_", " ").strip()
else:
my_label = self.columns.column.verbose_name.strip()

arrow = '&#x21f5;' if self.columns.orderable else ""

self.columns.column.verbose_name = mark_safe(f'{my_label} {arrow}')

def before_render(self, request):
pprint(vars(request))
if "sort" in request.GET:
which_column = request.GET["sort"]
reverse_sort = True if which_column[0] == "-" else False
if reverse_sort:
which_column = which_column[1:]
arrow = '<i class="fa-solid fa-arrow-down-z-a"></i>'
else:
arrow = '<i class="fa-solid fa-arrow-down-a-z"></i>'
i = [j for j in self.columns if j.name == which_column]

new_name = i[0].verbose_name.replace(' &#x21f5;', '')

i[0].column.verbose_name = mark_safe(f'{new_name} {arrow}')
</code></pre>
<p>then I do:</p>
<pre><code>class StudentTable(AbstractTable)
</code></pre>
<p>and voila, it all works nicely - although I'm sure it can be improved.</p>
<p>Now the problem, when I export the table, my column headers in the csv or xls have the <code>html code</code> attached. For example, if <code>name</code> is sortable, the export will have <code>name &#x21f5;</code> in the first row instead of just <code>name</code>.</p>
<p>What I can't figure out is how to tell if the request includes an export (or not) so I could modify the name appropriately.</p>
 

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top