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

Add API function for retrieving hidden cols (issue #9) #11

Closed spapas closed 6 years ago

spapas commented 6 years ago

I've added a $.django_tables2_column_shifter_hidden() method which can be used to retrieve an array with the names of the columns that are hidden for each table. This method takes the (0-based) index of the table in the page for which you want to retrieve the hidden cols in case there are more than one tables in the same page; if there's only one (or you want the hidden cols of the 1st table) just call it without parameters (or pass 0 to the method like $.django_tables2_column_shifter_hidden(0)).

This method uses some jquery trickery to identify which fields have a data-state == off attribute; my previous idea of reading from the local storage isn't working that well because the state of the columns is written to the local storage only after it's been changed (so initially the local storage will be empty).

The proper way to handle multiple tables in the same page would be to use the prefix of each table (https://django-tables2.readthedocs.io/en/latest/pages/query-string-fields.html?highlight=prefix) however I couldn't find a reliable way to get it and didn't want to use hacky solutions; using the index of each table in the page seems more straight forward.

Notice that I've compressed the source using uglify-js with the following command: uglifyjs --compress --mangle -- django_tables2_column_shifter.js > django_tables2_column_shifter.min.js; if you are using a different util then I recommend re-compressing it.

The above should resolve #9.

spapas commented 6 years ago

Hello @djk2 thanks for merging!

I think that a small description on the README would also be useful to describe the API. I could do it if you like.

djk2 commented 6 years ago

New release was added to PyPi: django-tables2-column-shifter 0.5.1. Thanks for PR. Could you modify README ??

spapas commented 6 years ago

Done in #14.

Best regards, Serafeim

AyubKa4a commented 2 years ago

okay the function is working when i execute it in the console , but how do i retrieve the array of hidden elements in the backend

spapas commented 2 years ago

Hey @AyubKa4a you need to pass them to the backend somehow; see my comment on #9 on how I use it to pass the selected columns on my export view.

Also see this in the readme https://github.com/djk2/django-tables2-column-shifter#js-api