Save and recall expansion state of tree

- Save choices to sessionStorage
This commit is contained in:
Oliver Walters 2019-05-09 00:13:50 +10:00
parent 206720849c
commit cc7fb31d6d
2 changed files with 30 additions and 1 deletions

View File

@ -21,7 +21,11 @@ from rest_framework import views
class TreeSerializer(views.APIView):
""" JSON View for serializing a Tree object.
""" JSON View for serializing a Tree object.
Turns a 'tree' model into a JSON object compatible with bootstrap-treview.
Ref: https://github.com/jonmiles/bootstrap-treeview
"""
def itemToJson(self, item):

View File

@ -11,6 +11,29 @@ function loadTree(url, tree, data) {
data: response.tree,
enableLinks: true
});
var saved_exp = sessionStorage.getItem('inventree-sidenav-expanded-items').split(",");
// Automatically expand the desired notes
for (var q = 0; q < saved_exp.length; q++) {
$(tree).treeview('expandNode', parseInt(saved_exp[q]));
}
// Setup a callback whenever a node is toggled
$(tree).on('nodeExpanded nodeCollapsed', function(event, data) {
// Record the entire list of expanded items
var expanded = $(tree).treeview('getExpanded');
var exp = [];
for (var i = 0; i < expanded.length; i++) {
exp.push(expanded[i].nodeId);
}
// Save the expanded nodes
sessionStorage.setItem('inventree-sidenav-expanded-items', exp);
});
}
},
error: function (xhr, ajaxOptions, thrownError) {
@ -24,6 +47,8 @@ function openSideNav() {
document.getElementById("inventree-content").style.marginLeft = "270px";
sessionStorage.setItem('inventree-sidenav-state', 'open');
}
function closeSideNav() {