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

django-tables2 export does not respect selected columns #12

Closed pvanek closed 5 years ago

pvanek commented 6 years ago

I'm evaluating this cool addon to tables2. Is it possible to extend the export tool of tables2 to take handle only selected columns from column-shifter?

https://django-tables2.readthedocs.io/en/latest/pages/export.html

Something with querystring probably?

https://django-tables2.readthedocs.io/en/latest/pages/template-tags.html#querystring

This is my playground template.

<!doctype html>
<html>
        <head>
                <title>List of workflows</title>
        </head>

{% load static %}
{% load render_table from django_tables2 %}
{% load querystring from django_tables2 %}
{% load bootstrap4 %}
{% bootstrap_css %}
{% bootstrap_javascript jquery='full' %}
    <script type="text/javascript" src="{% static "django_tables2_column_shifter/js/django_tables2_column_shifter.min.js" %}"></script>

<body>
{% render_table table %}

<a href="{% querystring '_export'='xlsx' %}">XLSX</a>
<a href="{% querystring '_export'='csv' %}">CSV</a>
<a href="{% querystring '_export'='xls' %}">XLS</a>

</body>
</html>
spapas commented 6 years ago

Hello @pvanek because of how django-tables2-column-shifter works (storing selected invisible columns to the localStorage using javascript) you'll need to also use javascript to modify the query so that the excluded columns would be passed to Django. I don't think it is possible to implement this using querystring.

For this you can use the $.django_tables2_column_shifter_hidden() API call to retrieve the hidden column names (see #11), modify the query to pass these columns along with the _export=format to the backend and finally capture the column names in the backend and pass them to the Exporter. Take a look at my comment on #9 to see a more thorough explanation.

When I'll find the time I'll provide a complete template + backend for this functionality.