Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(treeview): update the rename node kb #1793

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 187 additions & 8 deletions knowledge-base/treeview-rename-node.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: How to Rename a TreeView Node
description: How to edit the name (text) of a treeview node
description: Learn how to edit the name (text) of a Treeview node and explore the example that demonstrates a possible approach with an ItemTemplate.
type: how-to
page_title: How to rename a TreeView node?
page_title: How to Rename a TreeView Node?
slug: treeview-kb-rename-node
position:
tags: edit, rename, tree, treeview, note, item
ticketid: 1525532
tags: edit, rename, tree, treeview, node, item
YanushevGeorgi marked this conversation as resolved.
Show resolved Hide resolved
ticketid: 1525532, 1540469, 1629878, 1602035
res_type: kb
---

Expand All @@ -22,11 +22,190 @@ res_type: kb


## Description
Is there any way I can edit the node text within a Blazor TreeView?
This KB article answers the following questions:
- Is there any way I can edit the node text within a Blazor TreeView?
- How can I let the user rename a node?

I want to let the user rename a node.

## Solution
You can use the `ItemTemplate` to define how the node text will render and the desired UI and UX for editing. Here is a sample project that demonstrates a possible implementation. It uses a custom component `EditableTreeNode` that renders buttons and a textbox to update the TreeView item.
Use the `ItemTemplate` to determine the node text's rendering and customize the editing UI. For example, you can define a button that initiates editing, a TextBox that modifies the node's name, and another button that saves the changes.

https://github.com/telerik/blazor-ui/tree/master/treeview/rename-node
````CSHTML
<TelerikTreeView Data="@FlatData">
<TreeViewBindings>
<TreeViewBinding>
<ItemTemplate>
@{
Item = context as TreeItem;

if (Item.Icon != null)
{
<TelerikSvgIcon Icon="@Item.Icon"></TelerikSvgIcon>
}
@if (!IsEditing)
{
<span style="margin-right: 1em;">@Item.Text</span>
<TelerikButton Icon="SvgIcon.Pencil" OnClick="@Edit" FillMode="flat"></TelerikButton>
}
else
{
<span @onclick:stopPropagation="true">
@* Stop the treenode from taking focus when you click in the textbox *@
<TelerikTextBox @bind-Value="@Item.Text" @ref="@TextBoxRef"></TelerikTextBox>
</span>
<TelerikButton Icon="SvgIcon.Save" OnClick="@Save" FillMode="flat"></TelerikButton>
}
}
</ItemTemplate>
</TreeViewBinding>
</TreeViewBindings>
</TelerikTreeView>

@code {
private TelerikTextBox TextBoxRef { get; set; }

private List<TreeItem> FlatData { get; set; }
YanushevGeorgi marked this conversation as resolved.
Show resolved Hide resolved

private TreeItem Item { get; set; }

private TreeDataService DataService = new TreeDataService();

private bool IsEditing { get; set; }

private async Task Edit()
{
IsEditing = true;

//give rendering time to put the markup in and populate the reference
await InvokeAsync(StateHasChanged);
await Task.Delay(20);

if (TextBoxRef != null)
{
await TextBoxRef.FocusAsync();
}
}

private async Task Save()
{
IsEditing = false;
await DataService.UpdateNode(Item);
}

protected override async Task OnInitializedAsync()
{
FlatData = await DataService.GetData();
}

public class TreeDataService
{
List<TreeItem> items = new List<TreeItem>();

public async Task<List<TreeItem>> GetData()
{
EnsureData();
return await Task.FromResult(new List<TreeItem>(items));
}

public async Task UpdateNode(TreeItem itemToUpdate)
{
int itmIndex = items.FindIndex(itm => itm.Id == itemToUpdate.Id);
if (itmIndex > -1)
{
items[itmIndex] = itemToUpdate;
}
}

private void EnsureData()
{
if (items == null || !items.Any())
{
GenerateData();
}
}

private void GenerateData()
{
items = new List<TreeItem>();

items.Add(new TreeItem()
{
Id = 1,
Text = "Project",
ParentId = null,
HasChildren = true,
Icon = SvgIcon.Folder,
Expanded = true
});

items.Add(new TreeItem()
{
Id = 2,
Text = "Design",
ParentId = 1,
HasChildren = true,
Icon = SvgIcon.Brush,
Expanded = true
});
items.Add(new TreeItem()
{
Id = 3,
Text = "Implementation",
ParentId = 1,
HasChildren = true,
Icon = SvgIcon.Folder,
Expanded = true
});

items.Add(new TreeItem()
{
Id = 4,
Text = "site.psd",
ParentId = 2,
HasChildren = false,
Icon = SvgIcon.FilePsd,
Expanded = true
});
items.Add(new TreeItem()
{
Id = 5,
Text = "index.js",
ParentId = 3,
HasChildren = false,
Icon = SvgIcon.Js
});
items.Add(new TreeItem()
{
Id = 6,
Text = "index.html",
ParentId = 3,
HasChildren = false,
Icon = SvgIcon.Html5
});
items.Add(new TreeItem()
{
Id = 7,
Text = "styles.css",
ParentId = 3,
HasChildren = false,
Icon = SvgIcon.Css
});
}
}

public class TreeItem
{
public int Id { get; set; }
public string Text { get; set; }
public int? ParentId { get; set; }
public bool HasChildren { get; set; }
public ISvgIcon Icon { get; set; }
public bool Expanded { get; set; }
}
}
````


## See Also

* [TreeView Item Template]({%slug components/treeview/templates%})