5.6 KiB
Staff Hierarchy Page Fix
Problem Identified
The staff hierarchy page was not displaying properly because the organization has 17 separate hierarchy trees (17 top-level managers) instead of a single unified hierarchy.
D3.js tree visualizations require a single root node to render correctly. When the API returned multiple disconnected root nodes, the visualization failed to display any content.
Data Statistics
- Total Staff: 1,968
- Top-Level Managers (Root Nodes): 17
- Issue: 17 disconnected trees cannot be rendered by D3.js without a virtual root
Solution Implemented
1. API Fix (apps/organizations/views.py)
Modified the hierarchy action in StaffViewSet to:
- Detect multiple root nodes: Identify when there are multiple top-level managers
- Create virtual root: When multiple roots exist, create a virtual "Organization" node
- Wrap hierarchies: Place all root nodes as children under the virtual root
- Return single tree: API always returns a single tree structure that D3.js can render
Key Changes:
# If there are multiple root nodes, wrap them in a virtual "Organization" node
if len(root_nodes) > 1:
hierarchy = [{
'id': None, # Virtual root has no real ID
'name': 'Organization',
'is_virtual_root': True # Flag to identify this is a virtual node
'children': root_nodes
}]
2. Template Fix (templates/organizations/staff_hierarchy_d3.html)
Updated the D3.js visualization to:
- Handle virtual root: Recognize and style the virtual root node differently
- Prevent navigation: Disable double-click navigation to virtual root (no staff detail page)
- Visual distinction: Make virtual root larger and use different colors
Key Changes:
- Virtual root node radius: 20px (vs 10px for regular nodes)
- Virtual root color: Gray (#666) to distinguish from real staff
- Cursor style: Default (not clickable) for virtual root
- Navigation check: Prevent double-click navigation to
/organizations/staff/None/
Files Modified
-
apps/organizations/views.py- Modified
StaffViewSet.hierarchy()action - Added virtual root node logic for multiple hierarchies
- Modified
-
templates/organizations/staff_hierarchy_d3.html- Updated node styling for virtual root
- Modified double-click handler to prevent navigation to virtual root
- Enhanced node update transitions
Testing the Fix
Verify the Fix Works
-
Start the server (if not running):
python manage.py runserver -
Login to the application with your credentials
-
Navigate to the hierarchy page:
- Go to Organizations > Staff > Hierarchy
- Or visit:
http://localhost:8000/organizations/staff/hierarchy/
-
Expected behavior:
- You should see a single organizational chart
- Top-level "Organization" node (virtual root, gray color, larger)
- 17 top-level managers as children of the virtual root
- All 1,968 staff members displayed in the hierarchy
- Click on nodes to expand/collapse
- Double-click on staff nodes (not virtual root) to view details
Check the API Response
If you want to verify the API is returning the correct structure:
python manage.py shell << 'EOF'
from django.test import RequestFactory
from apps.organizations.views import StaffViewSet
from apps.accounts.models import User
# Create a mock request
factory = RequestFactory()
request = factory.get('/organizations/api/staff/hierarchy/')
# Create a mock user (PX Admin)
request.user = User.objects.filter(is_px_admin=True).first()
# Call the viewset action
viewset = StaffViewSet()
viewset.request = request
viewset.format_kwarg = None
response = viewset.hierarchy(request)
# Check response
import json
data = json.loads(response.content)
print(f"Total staff: {data['statistics']['total_staff']}")
print(f"Top managers: {data['statistics']['top_managers']}")
print(f"Virtual root created: {data['hierarchy'][0].get('is_virtual_root', False)}")
print(f"Children of virtual root: {len(data['hierarchy'][0].get('children', []))}")
EOF
Benefits of This Fix
- Single Unified View: All staff hierarchies are now visible in one cohesive visualization
- No Data Loss: All 1,968 staff members are displayed
- Better UX: Users can see the entire organizational structure at a glance
- Flexible: Works with any number of hierarchies (1, 17, or more)
- Backward Compatible: Single hierarchies still work without virtual root
Virtual Root Node Details
The virtual root node has these characteristics:
- Name: "Organization"
- ID:
None(no real database ID) - is_virtual_root:
true(flag for identification) - color: Gray (#666) to distinguish from real staff
- size: 20px radius (larger than regular 10px nodes)
- cursor: Default (not clickable)
- navigation: Disabled (double-click does nothing)
Future Enhancements
Potential improvements for the hierarchy visualization:
- Hospital Filtering: Add dropdown to filter by hospital
- Department Filtering: Add dropdown to filter by department
- Export Options: Add ability to export hierarchy as PDF or image
- Search Enhancement: Highlight search results in the tree
- Organization Grouping: Group hierarchies by hospital under virtual root
- Collapsible Virtual Root: Allow hiding the virtual root label
Related Documentation
docs/STAFF_HIERARCHY_INTEGRATION_SUMMARY.md- Original integration documentationdocs/D3_HIERARCHY_INTEGRATION.md- D3.js implementation detailsdocs/STAFF_HIERARCHY_IMPORT_GUIDE.md- Staff data import guide