Last active
September 24, 2023 20:15
-
-
Save alfonsodguez/7ee979de9f1a32014507388558c398a8 to your computer and use it in GitHub Desktop.
Blazor treeview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inject NavigationManager _navigated | |
@foreach (var category in categories.Where(subcatg => subcatg.CategoryParentId == ParentId)) | |
{ | |
bool isCollapse = true; | |
string node = category.CategoryName; | |
bool hasSubcategory = categories.Where(subcatg => subcatg.CategoryParentId == category.CategoryId).Any() | |
bool canExpand = isCollapse == @isCollapseNavMenu && node == @nodeParent && hasSubcategory | |
<li class="nav-item px-2"> | |
<NavLink @onclick="@(() => ToggleNavMenu(category))" class="nav-link">@category.CategoryName.Split(";")[0]</NavLink> | |
@if (canExpand) | |
{ | |
<ul class="nav flex-column"> | |
<RecursiveUI ParentId="category.CategoryId"></RecursiveUI> | |
</ul> | |
} | |
</li> | |
} | |
@code { | |
[Parameter] | |
public int ParentId { get; set; } | |
[CascadingParameter] | |
public List<Category> categories { get; set; } | |
private bool isCollapseNavMenu = false; | |
private String nodeParent = null; | |
private void ToggleNavMenu(Category category) | |
{ | |
isCollapseNavMenu = !isCollapseNavMenu; | |
this.nodeParent = category.CategoryName; | |
StateHasChanged(); | |
this._navigated.NavigateTo($@"/Shop/Products/{category.CategoryName.Split(";")[1]}"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inject IRestService _restService | |
@inject NavigationManager _navigated | |
<CascadingValue Value="categories"> | |
<ul class="nav flex-column"> | |
<RecursiveUI ParentId=0></RecursiveUI> | |
</ul> | |
</CascadingValue> | |
@code { | |
List<Category> categories = new List<Category>(); | |
protected override async Task OnInitializedAsync() | |
{ | |
try | |
{ | |
this.categories = await this._restService.GetCategories(); | |
} | |
catch (Exception) | |
{ | |
this._navigated.NavigateTo("/"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment