|
| 1 | +@page "/" |
| 2 | +@using Syncfusion.Blazor |
| 3 | +@using Syncfusion.Blazor.Navigations |
| 4 | +@using Syncfusion.Blazor.Data |
| 5 | +@using MyBlazorApp.Data; |
| 6 | +@using Newtonsoft.Json; |
| 7 | +@inject HttpClient Http |
| 8 | + |
| 9 | + |
| 10 | +@*<SfTreeView TValue="OrganizationDetails"> |
| 11 | + <TreeViewFieldsSettings TValue="OrganizationDetails" |
| 12 | + Id="Id" Text="Name" ParentID="ParentId" HasChildren="HasTeam" Expanded="IsExpanded"> |
| 13 | + <SfDataManager Url="api/Organization" Adaptor="Adaptors.WebApiAdaptor" CrossDomain="true"></SfDataManager> |
| 14 | + </TreeViewFieldsSettings> |
| 15 | +</SfTreeView>*@ |
| 16 | + |
| 17 | +<div id="treeview"> |
| 18 | + <SfTreeView @ref="tree" TValue="OrganizationDetails"> |
| 19 | + <TreeViewFieldsSettings TValue="OrganizationDetails" |
| 20 | + Id="Id" Text="Name" ParentID="ParentId" HasChildren="HasTeam" Expanded="IsExpanded"> |
| 21 | + <SfDataManager Url="api/Organization" Adaptor="Adaptors.WebApiAdaptor" CrossDomain="true"></SfDataManager> |
| 22 | + </TreeViewFieldsSettings> |
| 23 | + <TreeViewEvents TValue="OrganizationDetails" NodeClicked="TreeViewNodeClicked"></TreeViewEvents> |
| 24 | + </SfTreeView> |
| 25 | +</div> |
| 26 | +<SfContextMenu TValue="ContextMenuItem" Target="#treeview"> |
| 27 | + <ContextMenuItems> |
| 28 | + <ContextMenuItem Text="Edit"></ContextMenuItem> |
| 29 | + <ContextMenuItem Text="Add"></ContextMenuItem> |
| 30 | + <ContextMenuItem Text="Remove"></ContextMenuItem> |
| 31 | + </ContextMenuItems> |
| 32 | + <ContextMenuEvents TValue="ContextMenuItem" ItemSelected="ContextMenuItemSelected"></ContextMenuEvents> |
| 33 | +</SfContextMenu> |
| 34 | + |
| 35 | +@code{ |
| 36 | + |
| 37 | + SfTreeView<OrganizationDetails> tree; |
| 38 | + string selectedId; |
| 39 | + int index; |
| 40 | + |
| 41 | + public void ContextMenuItemSelected(MenuEventArgs<ContextMenuItem> args) |
| 42 | + { |
| 43 | + switch (args.Item.Text) |
| 44 | + { |
| 45 | + case "Edit": |
| 46 | + RenameNode(); |
| 47 | + break; |
| 48 | + case "Add": |
| 49 | + AddNode(); |
| 50 | + break; |
| 51 | + case "Remove": |
| 52 | + RemoveNode(); |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + void RemoveNode() |
| 58 | + { |
| 59 | + string[] removeNode = new string[] { this.selectedId }; |
| 60 | + this.tree.RemoveNodes(removeNode); |
| 61 | + } |
| 62 | + |
| 63 | + protected override async Task OnInitializedAsync() |
| 64 | + { |
| 65 | + // To get the last item index from the db |
| 66 | + int count = await Http.GetJsonAsync<int>("api/Organization/Index"); |
| 67 | + this.index = count +1; |
| 68 | + } |
| 69 | + |
| 70 | + void AddNode() |
| 71 | + { |
| 72 | + List<object> treeData = new List<object>(); |
| 73 | + treeData.Add(new |
| 74 | + { |
| 75 | + Id = this.index, |
| 76 | + Name = "New Entry", |
| 77 | + ParentId = this.selectedId |
| 78 | + }); |
| 79 | + this.tree.AddNodes(treeData, this.selectedId, null, false); |
| 80 | + this.index += 1; |
| 81 | + } |
| 82 | + |
| 83 | + async void RenameNode() |
| 84 | + { |
| 85 | + await tree.BeginEdit(this.selectedId); |
| 86 | + } |
| 87 | + |
| 88 | + public async void TreeViewNodeClicked(NodeClickEventArgs args) |
| 89 | + { |
| 90 | + this.selectedId = null; |
| 91 | + string eventString = JsonConvert.SerializeObject(args.Event); |
| 92 | + Dictionary<string, dynamic> eventParameters = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(eventString); |
| 93 | + if ((eventParameters["which"]).ToString() == "3") |
| 94 | + { |
| 95 | + // To get the selected node id upon context menu click |
| 96 | + this.selectedId = await args.Node.GetAttribute<string>("data-uid"); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments