Skip to content

Commit

Permalink
improve permalinks with search on sortable html table
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrammer committed Jan 8, 2025
1 parent 3c5b6be commit c016dd2
Showing 1 changed file with 62 additions and 11 deletions.
73 changes: 62 additions & 11 deletions grizli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11226,8 +11226,9 @@ def write_sortable_html(
var i;
var filter_url = "";
// Search text
if ($('input[type="search"]').val() != "") {{
filter_url += '&search=' + $('input[type="search"]').val();
var search_text = $('input[type="search"]').val();
if ((search_text != "") & (search_text != null)) {{
filter_url += '&search=' + search_text;
}}
// Table columns
Expand All @@ -11245,6 +11246,8 @@ def write_sortable_html(
if (filter_url != "") {{
var filtered_url = window.location.href.split('?')[0] + '?' + filter_url;
window.history.pushState('', '', filtered_url);
}} else {{
window.history.pushState('', '', window.location.href.split('?')[0]);
}}
}}
Expand Down Expand Up @@ -11303,7 +11306,11 @@ def write_sortable_html(
)

for il, line in enumerate(lines):
if "} ); </script>" in line:
if "dataTable(" in line:
lines[il] = " var table = {0}\n".format(
lines[il].strip().replace("dataTable", "DataTable")
)
elif "} ); </script>" in line:
break

lines.insert(il, listener)
Expand All @@ -11315,14 +11322,14 @@ def write_sortable_html(
if toggle:
lines = open(output).readlines()

# Change call to DataTable
for il, line in enumerate(lines):
if "dataTable(" in line:
break

lines[il] = " var table = {0}\n".format(
lines[il].strip().replace("dataTable", "DataTable")
)
# # Change call to DataTable
# for il, line in enumerate(lines):
# if "dataTable(" in line:
# break
#
# lines[il] = " var table = {0}\n".format(
# lines[il].strip().replace("dataTable", "DataTable")
# )

# Add function
for il, line in enumerate(lines):
Expand Down Expand Up @@ -11360,6 +11367,50 @@ def write_sortable_html(
)

lines.insert(il + 2, toggle_div)
else:
# Without filter columns
# Input listener
listener = """
// Parse location bar
$.urlParam = function(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results==null){
return null;
}
else{
return decodeURI(results[1]) || 0;
}
};
// Initialize search from address bar
var url_search = $.urlParam('search');
if ((url_search != "") & (url_search != null)) {
table.search(url_search).draw();
};
// Listener to update address bar on search text
$('input[type="search"]').keyup( function() {
// Search text
var search_text = $('input[type="search"]').val();
if ((search_text != "") & (search_text != null)) {
window.history.pushState(
'',
'',
window.location.href.split('?')[0] + '?search=' + search_text
);
} else {
window.history.pushState('', '', window.location.href.split('?')[0]);
}
} );\n"""

for il, line in enumerate(lines):
if "dataTable(" in line:
lines[il] = " var table = {0}\n".format(
lines[il].strip().replace("dataTable", "DataTable")
)
elif "} ); </script>" in line:
lines.insert(il, listener)
break

# Insert timestamp
if timestamp:
Expand Down

0 comments on commit c016dd2

Please sign in to comment.