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

Add virtualization to improve performance with many items #40

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
conditional Virtualize
stefanloerwald committed Oct 8, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit dbbc7a6ff274e7730cdf96785b737fbdb694270e
1 change: 1 addition & 0 deletions TreeView/__Internal/OrderedListAsyncBase.cs
Original file line number Diff line number Diff line change
@@ -13,5 +13,6 @@ public class OrderedListAsyncBase<T> : ComponentBase
protected string Class => TreeViewAsync?.ListClass;
[Parameter] public int Level { get; set; }
[Parameter] public bool Collapsed { get; set; }
[Parameter] public bool Virtualize { get; set; }
}
}
1 change: 1 addition & 0 deletions TreeView/__Internal/OrderedListBase.cs
Original file line number Diff line number Diff line change
@@ -13,5 +13,6 @@ public class OrderedListBase<T> : ComponentBase
protected string Class => TreeView?.ListClass;
[Parameter] public int Level { get; set; }
[Parameter] public bool Collapsed { get; set; }
[Parameter] public bool Virtualize { get; set; }
}
}
43 changes: 29 additions & 14 deletions TreeView/__Internal/OrderedListByGetChildren.razor
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
@typeparam T
@inherits OrderedListBase<T>

@{
RenderFragment<RenderFragment<T>> container = Virtualize
? (child) =>
@<Virtualize Items=SortBy(Parental).ToList() Context="item" TItem="T" ItemSize=TreeView.ItemSize>
@child(item)
</Virtualize>
: (child) =>
@<text>
@foreach (var item in SortBy(Parental))
{
@child(item)
}
</text>;
}
<OrderedList Collapsed="Collapsed" Class="@Class">
<Virtualize Items=SortBy(Parental).ToList() Context="item" TItem="T" ItemSize=TreeView.ItemSize>
<ListItem @key="item" Context="ChildCollapsed" Item="item" Level="Level" HasChildren="TreeView.HasChildren(item)">
@{
var sub_items = GetChildren(item);
sub_items = sub_items == null ? null : FilterBy(sub_items).ToList();
if (sub_items != null)
@container((item) =>
@<ListItem @key="item" Context="ChildCollapsed" Item="item" Level="Level" HasChildren="TreeView.HasChildren(item)">
@{
var sub_items = GetChildren(item);
sub_items = sub_items == null ? null : FilterBy(sub_items).ToList();
if (sub_items != null)
{
if (!TreeView.IsLazyLoading || !ChildCollapsed)
{
if (!TreeView.IsLazyLoading || !ChildCollapsed)
{
<OrderedListByGetChildren Level="Level + 1"
Collapsed="ChildCollapsed"
Parental="sub_items" GetChildren="GetChildren" />
}
<OrderedListByGetChildren
Level="Level + 1"
Collapsed="ChildCollapsed"
Parental="sub_items"
GetChildren="GetChildren" />
}
}
</ListItem>
</Virtualize>
}
</ListItem>)
</OrderedList>
@code {
[Parameter] public List<T> Parental { get; set; }
34 changes: 24 additions & 10 deletions TreeView/__Internal/OrderedListByGetChildrenAsync.razor
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
@typeparam T
@inherits OrderedListAsyncBase<T>

@{
RenderFragment<RenderFragment<ChildItem>> container = Virtualize
? (child) =>
@<Virtualize Items=childItems Context="item" ItemSize=TreeViewAsync.ItemSize>
@child(item)
</Virtualize>
: (child) =>
@<text>
@foreach (ChildItem item in childItems)
{
@child(item)
}
</text>;
}

<OrderedList Collapsed="Collapsed" Class="@Class">
<Virtualize Items=childItems Context="item" ItemSize=TreeViewAsync.ItemSize>
<ListItem @key="item.Value" Context="ChildCollapsed" Item="item.Value" Level="Level" LoadingChild="item.LoadingChildren" HasChildren="item.HasChildren">
@if (!ChildCollapsed)
{
<OrderedListByGetChildrenAsync Level="Level + 1"
Collapsed="ChildCollapsed"
Parental="item.Value" GetChildrenAsync="GetChildrenAsync" Loading="(loading) => item.LoadingChildren = loading"/>
}
</ListItem>
</Virtualize>
@container((item) =>
@<ListItem @key="item.Value" Context="ChildCollapsed" Item="item.Value" Level="Level" LoadingChild="item.LoadingChildren" HasChildren="item.HasChildren">
@if (!ChildCollapsed)
{
<OrderedListByGetChildrenAsync Level="Level + 1"
Collapsed="ChildCollapsed"
Parental="item.Value" GetChildrenAsync="GetChildrenAsync" Loading="(loading) => item.LoadingChildren = loading"/>
}
</ListItem>)
</OrderedList>

@code {
47 changes: 33 additions & 14 deletions TreeView/__Internal/OrderedListByGetParent.razor
Original file line number Diff line number Diff line change
@@ -2,20 +2,39 @@
@inherits OrderedListBase<T>

<OrderedList Collapsed="Collapsed" Class="@Class">
<Virtualize Items=SortBy(Parental).ToList() Context="item" ItemSize=TreeView.ItemSize>
@{var sub_items = FilterBy(Remaining.Where(i => GetParent(i)?.Equals(item) == true)).ToList();}
<ListItem @key="item" Context="ChildCollapsed" Item="item" Level="Level" HasChildren="sub_items.Any()">
@if (sub_items.Any())
{
<OrderedListByGetParent Level="Level + 1"
Collapsed="ChildCollapsed"
Parental="sub_items" Remaining="Remaining.Where(i => GetParent(i)?.Equals(item) != true).ToList()" GetParent="GetParent" />
}
</ListItem>
</Virtualize>
@{
RenderFragment<RenderFragment<T>> container = Virtualize
? (child) =>
@<Virtualize Items=SortBy(Parental).ToList() Context="item" ItemSize=TreeView.ItemSize>
@child(item)
</Virtualize>
: (child) =>
@<text>
@foreach (var item in SortBy(Parental))
{
@child(item)
}
</text>;
}
@container((item) =>
{
var sub_items = FilterBy(Remaining.Where(i => GetParent(i)?.Equals(item) == true)).ToList();
return
@<ListItem @key="item" Context="ChildCollapsed" Item="item" Level="Level" HasChildren="sub_items.Any()">
@if (sub_items.Any())
{
<OrderedListByGetParent
Level="Level + 1"
Collapsed="ChildCollapsed"
Parental="sub_items"
Remaining="Remaining.Where(i => GetParent(i)?.Equals(item) != true).ToList()"
GetParent="GetParent" />
}
</ListItem>;
})
</OrderedList>
@code {
[Parameter] public List<T> Parental { get; set; }
[Parameter] public List<T> Remaining { get; set; }
[Parameter] public Func<T, T> GetParent { get; set; }
[Parameter] public List<T> Parental { get; set; }
[Parameter] public List<T> Remaining { get; set; }
[Parameter] public Func<T, T> GetParent { get; set; }
}