Skip to content

Commit

Permalink
Merge pull request #30 from spapas/add_init_api
Browse files Browse the repository at this point in the history
Add js init api (fixes #27)
  • Loading branch information
djk2 committed Apr 7, 2023
2 parents ac496b2 + 280e334 commit e80b34d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
===========

v. 2.1.1
--------

* Add the ``$.django_tables2_column_shifter_init`` api to initialize the column shifter
for tables that are added to the DOM after the page has loaded (@spapas)

v. 2.1.0
--------

Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ To use app, you must inherit your table class from ``django_tables2_column_shift

JS API:
-------

This library is initialized automatically on the page ready event. In case you are using a framework
like htmx, unpoly or turbo that does not trigger the ready event, you can initialize it manually by calling
``$.django_tables2_column_shifter_init()`` on your framework's initialize callback.

To retrieve the invisible columns you can use the ``$.django_tables2_column_shifter_hidden()`` js API.
You can either pass the 0-based index of the table in the page (i.e use ``$.django_tables2_column_shifter_hidden(1)``
to get the hidden columns for the 2nd table in the page) or just use it without parameters to retrieve the hidden columns
Expand Down
2 changes: 1 addition & 1 deletion django_tables2_column_shifter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VERSION = (2, 1, 0)
VERSION = (2, 1, 1)
__version__ = ".".join(str(i) for i in VERSION)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Author: Grzegorz Tężycki

$(document).ready(function(){

// API function for initializing column shifter tables
$.django_tables2_column_shifter_init = function() {

// In web storage is saved structure like that:
// localStorage['django_tables2_column_shifter'] = {
Expand Down Expand Up @@ -185,19 +187,22 @@ $(document).ready(function(){

// show or hide columns based on data from web storage
shift_columns();

// Add API method for retrieving non-visible cols for table
// Pass the 0-based index of the table or leave the parameter
// empty to return the hidden cols for the 1st table found
$.django_tables2_column_shifter_hidden = function(idx) {
if(idx==undefined) {
idx = 0;
}
return $('.table-container').eq(idx).find('.btn-shift-column').filter(function(z) {
return $(this).data('state')=='off'
}).map(function(z) {
return $(this).data('td-class')
}).toArray();
}

});
}


// Add API method for retrieving non-visible cols for table
// Pass the 0-based index of the table or leave the parameter
// empty to return the hidden cols for the 1st table found
$.django_tables2_column_shifter_hidden = function(idx) {
if(idx==undefined) {
idx = 0;
}
return $('.table-container').eq(idx).find('.btn-shift-column').filter(function(z) {
return $(this).data('state')=='off'
}).map(function(z) {
return $(this).data('td-class')
}).toArray();
}

// Initialize column shifter on page ready event
$(document).ready($.django_tables2_column_shifter_init);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e80b34d

Please sign in to comment.