Back to Devexpress

DxTreeList.HasChildrenFieldName Property

blazor-devexpress-dot-blazor-dot-dxtreelist-21058258.md

latest4.4 KB
Original Source

DxTreeList.HasChildrenFieldName Property

Specifies the field name that defines whether a node has children.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
[DefaultValue(null)]
[Parameter]
public string HasChildrenFieldName { get; set; }

Property Value

TypeDefaultDescription
Stringnull

The field name that defines whether a node has children.

|

Remarks

The TreeList component can initially load only root nodes. It can retrieve child nodes on demand when a user expands a parent node for the first time. Enable this behavior in one of the following ways:

In on-demand mode, the TreeList component uses the HasChildrenFieldName property to determine whether a node has children and requires an expand button. If you do not specify the HasChildrenFieldName property, the component throws an exception.

Read Tutorial: Bind Blazor TreeList to Data Run Demo: Large Dataset Run Demo: Load Data on Demand

The following code sample uses the Entity Framework Core data access technology to bind the TreeList component to the GridDevExtremeDataSource<T>:

razor
@inject CitiesService CitiesService

<DxTreeList Data="@Data"
            KeyFieldName="ID"
            ParentKeyFieldName="ParentID" 
            HasChildrenFieldName="HasChildren">
    <Columns>
        <DxTreeListDataColumn Caption="Location" FieldName="Name" />
        <DxTreeListDataColumn FieldName="CityType" />
        <DxTreeListDataColumn FieldName="Year" DisplayFormat="d"/>
        <DxTreeListDataColumn FieldName="RecordType" />
        <DxTreeListDataColumn FieldName="Population" />
    </Columns>
</DxTreeList> 

@code {
    object Data { get; set; }

    protected override async Task OnInitializedAsync() {
        var cities = await CitiesService.GetCitiesAsync();
        Data = new GridDevExtremeDataSource<Location>(cities.AsQueryable());
    }
}
csharp
public class Location {
    public string? Name { get; set; }
    public string? City { get; set; }
    public string? Country { get; set; }
    public decimal Population { get; set; }
    public int ID { get; set; }
    public int ParentID { get; set; }
    public string? CityType { get; set; }
    public string? RecordType { get; set; }
    public int? Year { get; set; }
    public bool HasChildren { get; set; }
}
csharp
public class CitiesService {
    CitiesContext _context;
    public CitiesService(IDbContextFactory<CitiesContext> contextFactory) {
        _context = contextFactory.CreateDbContext();
    }
    public void SeedData() {
        _context.Database.EnsureCreated();
        if (_context.Locations.Any())
            return;
        var stream = File.OpenRead(@"cities.json");
        var cities = System.Text.Json.JsonSerializer.Deserialize<Location[]>(stream);
        if (cities == null)
            return;
        foreach (var city in cities) {
            city.ID++;
            city.ParentID++;
        }
        _context.Locations.AddRange(cities);
        _context.SaveChanges();
    }

    public async Task<Location[]?> GetCitiesAsync() {
        await _context.Locations.LoadAsync();
        return _context.Locations.Local.ToArray();
    }
}

Implements

HasChildrenFieldName

See Also

DxTreeList Class

DxTreeList Members

DevExpress.Blazor Namespace