Skip to content

Commit

Permalink
Preserve sort order when going Back to the page (Closes #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed Nov 14, 2015
1 parent 965f0c6 commit b94e115
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions mojo-app/public/js/main.js
Expand Up @@ -3,9 +3,9 @@ $(function(){
});

function setup_table() {
var filter_container, filter;
var el = $('#dists'), filter_container, filter, table_plugin, sort_order;

$('#dists').DataTable({
table_plugin = el.DataTable({
paging: false,
autoWidth: false,
scrollX: false,
Expand All @@ -22,6 +22,19 @@ function setup_table() {
]
});

// This lets us restore correct sort order if the user uses "Back"
// button in their browser
sort_order = window.location.hash.match(/sort-([^-]+)-([^-])+/);
if ( sort_order ) {
table_plugin.order([
sort_order[1], sort_order[2] == 'a' ? 'asc' : 'desc'
]).draw();
}
el.find('th').click(function(){
window.location.hash = 'sort-' + $(this).index() + '-'
+ $(this).attr('aria-sort').substr(0,1);
});

// Mess around with markup to accomodate the table plugin and marry
// it nicely with both our JS-less version of the site and Bootstrap
$('#dists_wrapper').addClass('table-responsive').unwrap();
Expand Down

0 comments on commit b94e115

Please sign in to comment.