New to Telerik UI for BlazorStart a free 30-day trial

Blazor TreeList CRUD Operations

The Telerik TreeList for Blazor supports create, update, and delete operations (CRUD) with different modes and user experience. The component also supports built-in DataAnnotations or custom validation. This page describes:

Basics

The TreeList CRUD operations rely on the following algorithm:

  1. Users execute TreeList commands (Edit, Save, Delete, and more) with the mouse or keyboard.
  2. The TreeList fires events (OnCreate, OnDelete, OnUpdate and more), which provide information what the user did or how the component data changed.
  3. The application applies the changes to the TreeList data source in the above event handlers.
  4. The TreeList rebinds to display the latest data.

Model Requirements

Adding or editing rows in the TreeList sets the following requirements on the TreeList model:

Edit Modes

The TreeList offers several ways to add and edit rows with a different user experience:

  • In-Cell—users modify the TreeList content cell by cell.
  • Inline—users modify the TreeList content row by row.
  • Popup—users modify the TreeList content row by row in a modal popup form.

To allow users to add or edit values in the TreeList:

  1. Set the EditMode parameter to a member of the TreeListEditMode enum. The default EditMode parameter value is None and the built-in Add and Edit commands don't work.
  2. Define the required command buttons and events for the selected edit mode and operations.

Set up TreeList popup edit mode

RAZOR
<TelerikTreeList EditMode="@TreeListEditMode.Popup"
                 OnUpdate="@OnTreeListUpdate">
    <TreeListColumns>
        <TreeListCommandColumn>
            <TreeListCommandButton Command="Edit">Edit</TreeListCommandButton>
        </TreeListCommandColumn>
    </TreeListColumns>
</TelerikTreeList>

See the TreeList add and edit operations in action in the complete examples for TreeList inline, in-cell, and popup editing.

Editing multiple rows at the same time is not supported. You can render editors in all TreeList data cells through column <Template>s as an alternative.

Delete Operations

Delete operations provide the same user experience in all TreeList edit modes and require the same configuration:

  • Delete command button.
  • OnDelete event.
  • Optional ConfirmDelete TreeList parameter. It determines if the component will show a Dialog before firing OnDelete, so that users can abort the operation.

Delete operations can work even if the TreeList EditMode parameter value is None.

See the delete operations in action in the complete examples for TreeList inline, in-cell and popup editing. Also check how to customize the Delete Confirmation Dialog.

Commands

The TreeList provides the following built-in commands, which enable users to add, delete, and edit rows:

  • Add—adds a new row and puts it in edit mode. Fires the OnAdd event. If the command button is in the TreeList toolbar, the new row is created at root level. If the command button is in the command column, the new row is created as a child of the current row.
  • Cancel—cancels the row or cell changes and exits edit mode. Fires the OnCancel event.
  • Delete—deletes a row. Fires the OnDelete event.
  • Edit—puts a TreeList row or cell in edit mode. Fires the OnEdit event.
  • Save—confirms the row or cell changes and exits edit mode, if the user input is valid. Fires the OnUpdate event.

Users execute commands in the following ways:

Command buttons can only reside in a TreeList Command Column or the TreeList ToolBar. You can also trigger add and edit operations programmatically from anywhere on the web page through the TreeList State.

Events

The following table describes the TreeList events, which are related to adding, deleting, and editing items. Also check the sections about item instances and event arguments below.

EventRequiredDescriptionItem InstanceIf Cancelled
OnAddNoFires on Add command button click, before the TreeList enters add mode. This event preceeds OnCreate or OnCancel.NewTreeList remains in read mode.
OnCancelNoFires on Cancel command invocation.New or clonedTreeList remains in add or edit mode.
OnCreateTo add new items.Fires on Save command invocation for new items. This event succeeds OnAdd.NewTreeList remains in add mode.
OnDeleteTo delete items.Fires on Delete command button click.OriginalTreeList won't rebind. Deletion depends on the app itself.
OnEditNoFires on Edit command invocation, before the TreeList actually enters edit mode. This event preceeds OnCreate or OnCancel.OriginalTreeList remains in read mode.
OnModelInitDepends on the TreeList model typeFires when the TreeList requires a new model instance, which is immediately before OnAdd or immediately after OnEdit.
Use this event when the TreeList model type is an interface, abstract class, or has no parameterless constructor.
No event argumentsNot cancellable
OnUpdateTo edit existing items.Fires on Save command invocation for existing items. This event succeeds OnEdit.ClonedTreeList remains in edit mode.

The following considerations apply for the TreeList CRUD events:

  • Most events provide a TreeListCommandEventArgs argument in the handler. OnModelInit has no event argument.
  • All events, except OnModelInit, are cancellable and the user action can be prevented. Cancelling OnDelete does not automatically prevent item deletion from the TreeList data source. This depends entirely on the executed application code.
  • The OnCreate, OnDelete, and OnUpdate events are required when using add, delete, and edit operations, respectively. The app must use these events to modify the TreeList data source. The TreeList does not modify its data directly.

Some events always fire in the same sequence, based on the user actions. In the list below, the value input and validation occur between the second and third event:

  • OnModelInit, OnAdd, and OnCreate for add operations
  • OnEdit, OnModelInit, and OnUpdate for edit operations

Both user workflows can end with the OnCancel event instead of OnCreate or OnUpdate.

Use async Task instead of async void for event handlers that execute awaitable operations. Otherwise the TreeList may show incorrect data, or the app may throw exceptions related to disposed objects or concurrency.

Item Instances

The TreeList does not expose or modify its data items directly in add or edit mode. Instead, the component creates a new item instance or clones an existing one. The user is always changing the values of the separate item instance. The TreeList uses Activator.CreateInstance<TItem>() and PropertyInfo.SetValue() to create and populate items for add and edit mode. This approach:

TreeListCommandEventArgs

All events in the Events table, except OnModelInit, provide a TreeListCommandEventArgs event argument. It exposes the following properties:

Property NameTypeDescription
FieldstringThe column Field name. Applicable only for in-cell edit mode.
IsCancelledboolDefines if the user action should be prevented. See the Comparison table below for details.
IsNewboolDefines if Item is a newly added row or an existing row.
ItemobjectThe data item, which the user is adding, deleting, or editing. Cast it to the TreeList model type.
ParentItemobject?The parent of the data item, which the user is adding. Available only in the OnCreate event. Cast the object to the TreeList model type.
ValueobjectThe data item value, which the user is editing. You can cast it to the correct type, based on the Field. Applicable only for in-cell edit mode.

Column Editors

You can customize the column editors through the column EditorType parameter, or by using an editor template. The EditorType parameter accepts a member of the TreeListEditorType enum:

Column Field TypeValid TreeListEditorType Enum Members
boolCheckBox (default)
Switch
DateTimeDatePicker (default)
DateTimePicker
TimePicker
stringTextArea
TextBox (default)

Setting column editor type

RAZOR
<TreeListColumn Field="@nameof(Race.StartDateTime)"
                EditorType="@TreeListEditorType.DateTimePicker" />

Rebinding After Data Changes

During add, edit, and delete operations, the TreeList expects the application to make changes to the data source (database) and then provide the latest data to the component.

In the OnCreate, OnDelete, and OnUpdate event handler, the application must do one of the following:

Integration with Other Features

Updated rows comply with the current filter, search, and sort settings, just like all other rows. As a result, an updated row may render at a different place or disappear.

When editing a row with child items, it will collapse unless you override the Equals() method of the TreeList model class.

Learn more integration details for the inline and in-cell edit modes.

Examples

See TreeList CRUD operations in action at:

See Also