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 to initialize table anytime #27

Closed spapas closed 1 year ago

spapas commented 1 year ago

Hello @djk2 I'd like to propose a small change to the JS code of the project. Right now, the table initialization is performed by an anonymous function that is called when document is ready. See line 3 of django_tables2_column_shifter.js. This function does various stuff but everything is inside the anonymous function so it isn't visible outside.

My proposal would be to create a named function (maybe named something like $.django_tables2_column_shifter_init) that would contain all the stuff that this anonymous function does. This named function will be added to the API of this project so other client code can call it (similar to $.django_tables2_column_shifter_hidden). The $(document).ready will only call this named function.

So nothing really will change beyond the addition of 1 more function to the API of the js of this project.

Why is it needed? If we want to use frameworks like unpoly (https://unpoly.com/) that transition between pages without full page loads, the $(document).ready event is not fired when doing page transitions. That results to the table not initializing. To fix that, unpoly has a way to run arbitrary javascript when the DOM "sees" a specific element. So in this case, when the DOM sees the .column-shifter-container element it will run the $.django_tables2_column_shifter_init() function I mentioned above and the table will initialize.

I'd be happy to provide a PR if you agree with this approach (or you can do it yourself if you want to name/refactor things your way).

Kind regards, Serafeim

spapas commented 1 year ago

Hey friends, any love for this issue? I'd be happy to provide the PR if you like!

djk2 commented 1 year ago

Hi. Sorry for delay in response, unfortunately from a very long time I don't have enough time to maintain my projects on GitHub. I think that your suggestion regarding refactoring structure of function for JS is right. I'm not a expert in JS, so if you could prepare a PR for that I will be appreciate.