Skip to content

Commit

Permalink
Tree TreeItemRender event added
Browse files Browse the repository at this point in the history
  • Loading branch information
enchev committed Mar 28, 2024
1 parent 5d25fec commit 3f8cd14
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
45 changes: 45 additions & 0 deletions Radzen.Blazor/Common.cs
Expand Up @@ -2639,6 +2639,51 @@ public class TreeItemSettings
public RenderFragment<RadzenTreeItem> Template { get; set; }
}

/// <summary>
/// Supplies information about a <see cref="RadzenTree" /> item render event that is being raised.
/// </summary>
public class TreeItemRenderEventArgs
{
/// <summary>
/// Gets or sets the item HTML attributes.
/// </summary>
public IDictionary<string, object> Attributes { get; private set; } = new Dictionary<string, object>();

bool _checkedSet;
internal bool CheckedSet()
{
return _checkedSet;
}

bool? _checked;
/// <summary>
/// Gets or sets a value indicating whether this item is checked.
/// </summary>
/// <value><c>true</c> if expanded; otherwise, <c>false</c>.</value>
public bool? Checked
{
get
{
return _checked;
}
set
{
_checkedSet = true;
_checked = value;
}
}

/// <summary>
/// Gets tree item.
/// </summary>
public object Value { get; internal set; }

/// <summary>
/// Gets child items.
/// </summary>
public IEnumerable Data { get; internal set; }
}

/// <summary>
/// Supplies information about a <see cref="RadzenLogin.Login" /> event that is being raised.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions Radzen.Blazor/RadzenTree.razor.cs
Expand Up @@ -114,6 +114,24 @@ protected override string GetComponentCssClass()
[Parameter]
public EventCallback<TreeEventArgs> Collapse { get; set; }

/// <summary>
/// A callback that will be invoked when item is rendered.
/// </summary>
[Parameter]
public Action<TreeItemRenderEventArgs> ItemRender { get; set; }

internal Tuple<Radzen.TreeItemRenderEventArgs, IReadOnlyDictionary<string, object>> ItemAttributes(RadzenTreeItem item)
{
var args = new TreeItemRenderEventArgs() { Data = item.GetAllChildValues(), Value = item.Value };

if (ItemRender != null)
{
ItemRender(args);
}

return new Tuple<TreeItemRenderEventArgs, IReadOnlyDictionary<string, object>>(args, new System.Collections.ObjectModel.ReadOnlyDictionary<string, object>(args.Attributes));
}

/// <summary>
/// Gets or sets the child content.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions Radzen.Blazor/RadzenTreeItem.razor
@@ -1,15 +1,16 @@
@using Radzen
@using Radzen.Blazor.Rendering
@implements IDisposable
<li class="rz-treenode" @attributes="Attributes">
@{var itemArgs = Tree?.ItemAttributes(this); }
<li class="rz-treenode" @attributes="@(itemArgs.Item1.Attributes.Any() ? itemArgs.Item1.Attributes : Attributes)">
<div class=@ContentClassList @onclick="@Select">
@if (ChildContent != null || HasChildren)
{
<span class=@IconClassList @onclick="@Toggle" @onclick:stopPropagation></span>
}
@if(Tree != null && Tree.AllowCheckBoxes)
{
<RadzenCheckBox TabIndex=-1 Name="@($"{GetHashCode()}")" TValue="bool?" Value="@IsChecked()" Change="@CheckedChange" InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", Tree.SelectItemAriaLabel }})" />
<RadzenCheckBox TabIndex=-1 Name="@($"{GetHashCode()}")" TValue="bool?" Value="@(itemArgs.Item1.CheckedSet() ? itemArgs.Item1.Checked : IsChecked())" Change="@CheckedChange" InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", Tree.SelectItemAriaLabel }})" />
}
@if (Template != null)
{
Expand Down
2 changes: 1 addition & 1 deletion Radzen.Blazor/RadzenTreeItem.razor.cs
Expand Up @@ -361,7 +361,7 @@ IEnumerable<object> GetCheckedValues()
return Tree.CheckedValues != null ? Tree.CheckedValues : Enumerable.Empty<object>();
}

IEnumerable<object> GetAllChildValues(Func<object, bool> predicate = null)
internal IEnumerable<object> GetAllChildValues(Func<object, bool> predicate = null)
{
var children = items.Concat(items.SelectManyRecursive(i => i.items)).Select(i => i.Value);

Expand Down
13 changes: 12 additions & 1 deletion RadzenBlazorDemos/Pages/TreeCheckBoxes.razor
Expand Up @@ -7,7 +7,8 @@
<div class="row my-5">
<div class="col-lg-6 offset-lg-3">
<RadzenCard>
<RadzenTree AllowCheckBoxes="true" @bind-CheckedValues=@CheckedValues Style="width: 100%; height: 300px" Data=@categories>
<RadzenTree AllowCheckBoxes="true" @bind-CheckedValues=@CheckedValues Style="width: 100%; height: 300px" Data=@categories
ItemRender="@TreeItemRender">
<RadzenTreeLevel TextProperty="CategoryName" ChildrenProperty="Products" />
<RadzenTreeLevel TextProperty="ProductName" HasChildren=@(product => false) />
</RadzenTree>
Expand Down Expand Up @@ -57,5 +58,15 @@
await base.OnInitializedAsync();

categories = dbContext.Categories.Include(c => c.Products);

checkedValues = dbContext.Products.Where(p => p.ProductName.StartsWith("C"));
}

void TreeItemRender(TreeItemRenderEventArgs args)
{
if (args.Value == categories.FirstOrDefault() && !args.Data.Cast<object>().Any())
{
args.Checked = null;
}
}
}

0 comments on commit 3f8cd14

Please sign in to comment.