I have a data from a single self-referencing table (idboss may references to idcounty).
class County
{
public int Idcounty
public string Fullname
public int? Idboss
}
And I would like to select some node using loading on demand. I manually add data to tree-data variable but tree hierarchy brokens. All nodes are out of place.
Here's my code
<RadzenTree Data="@counties" Expand="@OnExpand" @bind-Value=@selected>
<RadzenTreeLevel TextProperty="Fullname" />
</RadzenTree>
@code {
IEnumerable<County> counties;
sausozuContext context;
object selected;
protected override async System.Threading.Tasks.Task OnInitializedAsync()
{
context = DbFactory.CreateDbContext();
var result = await ProtectedSessionStore.GetAsync<int>("idcounty");
var idcounty = result.Success ? result.Value : -1;
if (context.Counties.FirstOrDefault(county => county.Idcounty == idcounty) != null)
{
var searchcounty = context.Counties.FirstOrDefault(county => county.Idcounty == idcounty);
List<County> tempcounties = new List<County>();
int? idboss = searchcounty.Idboss;
while (idboss != null)
{
tempcounties.AddRange(context.Counties.Where(c1 => c1.Idboss == idboss).ToList());
idboss = context.Counties.FirstOrDefault(c1 => c1.Idcounty == idboss)?.Idboss;
}
tempcounties.AddRange(context.Counties.Where(c1 => c1.Idboss == null).ToList());
counties = tempcounties.OrderBy(c => c.Idboss).ThenBy(c => c.Fullname).ToList();
selected = counties.First(c => c.Idcounty == idcounty);
}
else
{
var idboss = context.Counties.OrderBy(c => c.Idboss).First().Idboss;
counties = context.Counties.Where(c => c.Idboss == idboss).OrderBy(c => c.Idboss).ThenBy(c => c.Fullname).ToList();
}
}
void OnExpand(TreeExpandEventArgs args)
{
var county = args.Value as County;
if (county != null)
{
args.Children.Data = context.Counties.Where(c => c.Idboss == county.Idcounty).OrderBy(c=>c.Fullname);
args.Children.TextProperty = "Fullname";
args.Children.HasChildren = (county) => context.Counties.Any(c => c.Idboss == (county as County).Idcounty);
}
}
What I want:
And what I receives