blazor-405000-components-treelist-get-started-with-treelist.md
This tutorial describes how to create a simple Blazor application that uses a DevExpress TreeList component.
The following example contains full source code for this tutorial: Blazor TreeList - Getting Started.
Create an application as described in the following topic: Get Started With DevExpress Components for Blazor.
Blazor TreeList supports static render mode to display static data in a single page. For other features, you need to enable interactivity on a Razor page and allow the TreeList component to execute scripts and display data.
@rendermode InteractiveServer
Add the EmployeeTask class to your project:
Add the EmployeeTaskService class to your project:
Register the EmployeeTaskService in the Program.cs file.
Add <DxTreeList></DxTreeList> tags to the Index.razor page.
Inject the EmployeeTaskService to the page.
In the @code block, declare a list of EmployeeTask objects. Populate this list in the OnInitialized lifecycle method.
Bind the Data property to the list.
Specify KeyFieldName and ParentKeyFieldName properties to build a tree structure.
Refer to the following topic for additional information: Bind Blazor TreeList to Data.
<DxTreeList Data="@Data" KeyFieldName="Id" ParentKeyFieldName="ParentId">
<Columns>
<DxTreeListDataColumn FieldName="Name" Caption="Task" Width="40%"/>
<DxTreeListDataColumn FieldName="EmployeeName" />
<DxTreeListDataColumn FieldName="StartDate" MinWidth="100" />
<DxTreeListDataColumn FieldName="DueDate" MinWidth="100" />
<DxTreeListDataColumn FieldName="Progress" DisplayFormat="{0}%" />
</Columns>
</DxTreeList>
The TreeList component allows users to sort data by multiple columns. Specify SortOrder and SortIndex properties to configure initial sort settings.
<DxTreeListDataColumn FieldName="DueDate"
MinWidth="100"
SortOrder="TreeListColumnSortOrder.Ascending"
SortIndex="0" />
<DxTreeListDataColumn FieldName="Progress"
SortOrder="TreeListColumnSortOrder.Descending"
SortIndex="1" />
Set the ShowFilterRow property to true to display the filter row that allows users to filter data.
Use the FilterTreeMode property to specify how the TreeList component displays filtered nodes:
Specify FilterRowValue and FilterRowOperatorType properties to configure initial filter row values.
<DxTreeList Data="@Data"
KeyFieldName="Id"
ParentKeyFieldName="ParentId"
ShowFilterRow="true"
FilterTreeMode="TreeListFilterTreeMode.EntireBranch">
<Columns>
<DxTreeListDataColumn FieldName="EmployeeName"
FilterRowValue='"John"'
FilterRowOperatorType="TreeListFilterRowOperatorType.Contains" />
<!-- ... -->
</Columns>
</DxTreeList>
Follow the steps below to display a total summary in the TreeList component:
<DxTreeList Data="@Data" ... >
<TotalSummary>
<DxTreeListSummaryItem FieldName="Name" SummaryType="TreeListSummaryItemType.Count" />
<DxTreeListSummaryItem FieldName="Progress" SummaryType="TreeListSummaryItemType.Avg" />
</TotalSummary>
</DxTreeList>
Follow the steps below to enable data editing:
Columns collection. The command column displays buttons that allow users to add, edit, and delete rows.<DxTreeList Data="@Data"
KeyFieldName="Id"
ParentKeyFieldName="ParentId"
EditModelSaving="TreeList_EditModelSaving"
DataItemDeleting="TreeList_DataItemDeleting"
CustomizeEditModel="TreeList_CustomizeEditModel" ...>
<Columns>
<DxTreeListCommandColumn Width="200px"/>
@* ... *@
</Columns>
</DxTreeList>
@code {
List<EmployeeTask> Data { get; set; }
void TreeList_CustomizeEditModel(TreeListCustomizeEditModelEventArgs e) {
if(e.IsNew) {
var newTask = (EmployeeTask)e.EditModel;
newTask.Id = Data.Max(x => x.Id) + 1;
if(e.ParentDataItem != null)
newTask.ParentId = ((EmployeeTask)e.ParentDataItem).Id;
}
}
async Task TreeList_EditModelSaving(TreeListEditModelSavingEventArgs e) {
if(e.IsNew)
Data.Add((EmployeeTask)e.EditModel);
else
e.CopyChangesToDataItem();
}
async Task TreeList_DataItemDeleting(TreeListDataItemDeletingEventArgs e) {
Data.Remove((EmployeeTask)e.DataItem);
}
protected override void OnInitialized() {
Data = EmployeeTaskService.GenerateData();
}
}
Refer to the following topic for additional information: Editing and Validation in Blazor TreeList.
For additional information about TreeList features, refer to the following root topic: Blazor TreeList.