I want to be able to expand and collapse by Type level.
@using Syncfusion.Blazor.Navigations
@using Syncfusion.Blazor.Buttons
@using System.Collections.ObjectModel
@using System.Diagnostics
<SfTreeView TValue="Peoples" @ref="tree" EnablePersistence="true" ShowCheckBox="true">
<TreeViewFieldsSettings TValue="Peoples" Id="Id" DataSource="@MyData" Text="Name" ParentID="ParentId" HasChildren="HasSubFolders" Expanded="Expanded"></TreeViewFieldsSettings>
</SfTreeView>
<SfButton Content="Expand Level B" @onclick="ExpandLevelB"></SfButton>
<SfButton Content=" Collapse Level B" @onclick="CollapseLevelB"></SfButton>
@code{
SfTreeView<Peoples> tree;
public class Peoples
{
public string Id { get; set; }
public string ParentId { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public bool Expanded { get; set; }
public bool HasSubFolders { get; set; }
}
ObservableCollection<Peoples> MyData = new ObservableCollection<Peoples>();
protected override void OnInitialized()
{
base.OnInitialized();
MyData.Add(new Peoples
{
Id = "1",
Name = "Main",
HasSubFolders = true,
Type = "A",
Expanded = false
});
MyData.Add(new Peoples
{
Id = "2",
ParentId = "1",
Name = "AM1",
Type = "B",
HasSubFolders = true,
Expanded = false
});
MyData.Add(new Peoples
{
Id = "3",
ParentId = "1",
Name = "AM2",
Type = "B",
HasSubFolders = true,
Expanded = false
});
MyData.Add(new Peoples
{
Id = "4",
ParentId = "1",
Name = "AM3",
Type = "B",
HasSubFolders = true,
Expanded = false
});
MyData.Add(new Peoples
{
Id = "5",
ParentId = "2",
Name = "AF1",
Type = "C",
HasSubFolders = true,
Expanded = false
});
MyData.Add(new Peoples
{
Id = "6",
ParentId = "3",
Name = "AF2",
Type = "C",
HasSubFolders = true,
Expanded = false
});
MyData.Add(new Peoples
{
Id = "7",
ParentId = "4",
Name = "AF3",
Type = "C",
HasSubFolders = true,
Expanded = false
});
MyData.Add(new Peoples
{
Id = "8",
ParentId = "5",
Name = "Member 1",
Type = "D",
Expanded = false
});
MyData.Add(new Peoples
{
Id = "9",
ParentId = "6",
Name = "Member 2",
Type = "D",
Expanded = false
});
MyData.Add(new Peoples
{
Id = "10",
ParentId = "7",
Name = "Member 3",
Type = "D",
Expanded = false
});
}
public List<string> array = new List<string>();
public String[] str;
public async Task ExpandLevelB()
{
int x = MyData.Count;
int y = 0;
while (y < x)
{
if (MyData.ElementAt(y).Type == "B")
{
array.Add(MyData.ElementAt(y).Id.ToString());
}
y = y + 1;
}
str = array.ToArray();
await tree.ExpandAll(str);
}
public async Task CollapseLevelB()
{
int x = MyData.Count;
int y = 0;
while (y < x)
{
if (MyData.ElementAt(y).Type == "B")
{
array.Add(MyData.ElementAt(y).Id.ToString());
}
y = y + 1;
}
str = array.ToArray();
await tree.CollapseAll(str);
}