Display count badges in sidenav tree

Uses the 'tags' parameter as according to the docs - https://github.com/jonmiles/bootstrap-treeview

- Part
- Stock
This commit is contained in:
Oliver Walters 2019-05-09 20:30:23 +10:00
parent 2c5bb6b126
commit 45223fb607
5 changed files with 35 additions and 7 deletions

View File

@ -41,6 +41,17 @@ class InvenTreeTree(models.Model):
null=True,
related_name='children')
@property
def item_count(self):
""" Return the number of items which exist *under* this node in the tree.
Here an 'item' is considered to be the 'leaf' at the end of each branch,
and the exact nature here will depend on the class implementation.
The default implementation returns zero
"""
return 0
def getUniqueParents(self, unique=None):
""" Return a flat set of all parent items that exist above this node.
If any parents are repeated (which would be very bad!), the process is halted

View File

@ -33,6 +33,7 @@ class TreeSerializer(views.APIView):
data = {
'text': item.name,
'href': item.get_absolute_url(),
'tags': [item.item_count],
}
if item.has_children:
@ -51,12 +52,16 @@ class TreeSerializer(views.APIView):
nodes = []
top_count = 0
for item in top_items:
nodes.append(self.itemToJson(item))
top_count += item.item_count
top = {
'text': self.title,
'nodes': nodes,
'tags': [top_count],
}
response = {

View File

@ -47,18 +47,17 @@ class PartCategory(InvenTreeTree):
verbose_name = "Part Category"
verbose_name_plural = "Part Categories"
@property
def item_count(self):
return self.partcount
@property
def partcount(self):
""" Return the total part count under this category
(including children of child categories)
"""
count = self.parts.count()
for child in self.children.all():
count += child.partcount
return count
return len(Part.objects.filter(category__in=self.getUniqueChildren()))
@property
def has_parts(self):

View File

@ -9,7 +9,8 @@ function loadTree(url, tree, data) {
if (response.tree) {
$(tree).treeview({
data: response.tree,
enableLinks: true
enableLinks: true,
showTags: true,
});
var saved_exp = sessionStorage.getItem('inventree-sidenav-expanded-items').split(",");

View File

@ -48,6 +48,18 @@ class StockLocation(InvenTreeTree):
}
)
@property
def stock_item_count(self):
""" Return the number of StockItem objects which live in or under this category
"""
return len(StockItem.objects.filter(location__in=self.getUniqueChildren()))
@property
def item_count(self):
return self.stock_item_count
@receiver(pre_delete, sender=StockLocation, dispatch_uid='stocklocation_delete_log')
def before_delete_stock_location(sender, instance, using, **kwargs):