Back to Devexpress

DxTreeListDataColumn.CellDisplayTemplate Property

blazor-devexpress-dot-blazor-dot-dxtreelistdatacolumn.md

latest18.7 KB
Original Source

DxTreeListDataColumn.CellDisplayTemplate Property

Specifies a template used to display column cells.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
[Parameter]
public RenderFragment<TreeListDataColumnCellDisplayTemplateContext> CellDisplayTemplate { get; set; }

Property Value

TypeDescription
RenderFragment<TreeListDataColumnCellDisplayTemplateContext>

The column cell template.

|

Remarks

The CellDisplayTemplate property allows you to specify custom content and change the appearance of cells in individual columns. To define a common template for cells of all TreeList columns, use the DxTreeList.DataColumnCellDisplayTemplate.

The CellDisplayTemplate property accepts a TreeListDataColumnCellDisplayTemplateContext object as the context parameter. You can use the parameter’s members to get information about a column cell (DataColumn, Value, DisplayText, VisibleIndex). You can also access the TreeList object and use its members to obtain additional information about the TreeList.

The following code snippet displays links to Wikipedia pages:

razor
@inject SpaceObjectDataProvider SpaceObjectDataProvider

<DxTreeList Data="TreeListData" ChildrenFieldName="Satellites">
    <Columns>
        <DxTreeListDataColumn FieldName="Name" />
        <DxTreeListDataColumn FieldName="TypeOfObject" Caption="Type" />
        <DxTreeListDataColumn FieldName="Mass10pow21kg" Caption="Mass, kg" DisplayFormat="N2">
            <HeaderCaptionTemplate>Mass, 10<sup>21</sup> &#215; kg</HeaderCaptionTemplate>
        </DxTreeListDataColumn>
        <DxTreeListDataColumn FieldName="MeanRadiusInKM" Caption="Radius, km" DisplayFormat="N2" />
        <DxTreeListDataColumn FieldName="Volume10pow9KM3" DisplayFormat="N2">
            <HeaderCaptionTemplate>Volume, 10<sup>9</sup> &#215; km<sup>3</sup></HeaderCaptionTemplate>
        </DxTreeListDataColumn>
        <DxTreeListDataColumn FieldName="SurfaceGravity" DisplayFormat="N2">
            <HeaderCaptionTemplate>Gravity, m/s<sup>2</sup></HeaderCaptionTemplate>
        </DxTreeListDataColumn>
        <DxTreeListDataColumn FieldName="WikiPage" AllowSort="false" Width="90px">
            <CellDisplayTemplate><a href="@context.Value">Open Wikipedia</a></CellDisplayTemplate>
        </DxTreeListDataColumn>
    </Columns>
</DxTreeList>

@code {
    object TreeListData { get; set; }

    protected override async Task OnInitializedAsync() {
        TreeListData = SpaceObjectDataProvider.GenerateData();
    }
}
csharp
using System.Collections.Generic;

public class SpaceObject {
    public string Name { get; set; }
    public string WikiPage { get; set; }
    public double MeanRadiusInKM { get; set; }
    public double MeanRadiusByEarth { get; set; }
    public double Volume10pow9KM3 { get; set; }
    public double Mass10pow21kg { get; set; }
    public double Density { get; set; }
    public double SurfaceGravity { get; set; }
    public string TypeOfObject { get; set; }
    public List<SpaceObject> Satellites { get; set; }
    public SpaceObject(
        string name,
        string wikiPage,
        double meanRadiusInKM,
        double meanRadiusByEarth,
        double volume10pow9KM3,
        double mass10pow21kg,
        double density,
        double surfaceGravity,
        string typeOfObject,
        List<SpaceObject> satellites = null
    )
    {
        Name = name;
        WikiPage = wikiPage;
        MeanRadiusInKM = meanRadiusInKM;
        MeanRadiusByEarth = meanRadiusByEarth;
        Volume10pow9KM3 = volume10pow9KM3;
        Mass10pow21kg = mass10pow21kg;
        Density = density;
        SurfaceGravity = surfaceGravity;
        TypeOfObject = typeOfObject;
        Satellites = satellites ?? new List<SpaceObject>();
    }
}
csharp
public class SpaceObjectDataProvider {
    public List<SpaceObject> GenerateData() {
        return new List<SpaceObject>() {
            new SpaceObject("Sun", "https://en.wikipedia.org/wiki/Sun", 696000, 109.25, 1412000000, 1989100000, 1.409, 274.0, "Star", new List<SpaceObject>() {
                new SpaceObject("Mercury", "https://en.wikipedia.org/wiki/Mercury_(planet)", 2439.7, 0.383, 60.83, 330.2, 5.43, 3.7, "Planet"),
                new SpaceObject("Venus", "https://en.wikipedia.org/wiki/Venus", 6051.8, 0.950, 928.43, 4868.5, 5.24, 8.872, "Planet"),
                new SpaceObject("Earth", "https://en.wikipedia.org/wiki/Earth", 6371.0, 1, 1083.21, 5973.6, 5.515, 9.78033, "Planet", new List<SpaceObject> () {
                    new SpaceObject("Moon", "https://en.wikipedia.org/wiki/Moon", 1737.1, 0.273, 21.958, 73.5, 3.3464, 1.625, "Satellite")
                }),
                new SpaceObject("Mars", "https://en.wikipedia.org/wiki/Mars", 3390.0, 0.532, 163.18, 641.85, 3.94, 3.7, "Planet"),
                new SpaceObject("Jupiter", "https://en.wikipedia.org/wiki/Jupiter", 69911, 10.97, 1431280, 1898600, 1.33, 24.79, "Planet", new List<SpaceObject>() {
                    new SpaceObject("Ganymede", "https://en.wikipedia.org/wiki/Ganymede_(moon)", 2631.2, 0.413, 76.30, 148.2, 1.936, 1.428, "Satellite"),
                    new SpaceObject("Callisto", "https://en.wikipedia.org/wiki/Callisto_(moon)", 2410.3, 0.378, 58.65, 107.6, 1.83, 1.23603, "Satellite"),
                    new SpaceObject("Io", "https://en.wikipedia.org/wiki/Io_(moon)", 1821.5, 0.286, 25.32, 89.3, 3.528, 1.797, "Satellite"),
                    new SpaceObject("Europa", "https://en.wikipedia.org/wiki/Europa_(moon)", 1561, 0.245, 15.93, 48, 3.01, 1.316, "Satellite"),
                }),
                new SpaceObject("Saturn", "https://en.wikipedia.org/wiki/Saturn", 58232, 9.14, 827130, 568460, 0.70, 10.445, "Planet", new List<SpaceObject>() {
                    new SpaceObject("Titan", "https://en.wikipedia.org/wiki/Titan_(moon)", 2576, 0.404, 71.52, 134.5, 1.88, 1.354, "Satellite"),
                    new SpaceObject("Rhea", "https://en.wikipedia.org/wiki/Rhea_(moon)", 764.4, 0.12, 1.87, 2.3166, 1.23, 0.26, "Satellite"),
                    new SpaceObject("Iapetus", "https://en.wikipedia.org/wiki/Iapetus_(moon)", 736, 0.113, 1.55, 1.9739, 1.08, 0.223, "Satellite"),
                    new SpaceObject("Dione", "https://en.wikipedia.org/wiki/Dione_(moon)", 561.6, 0.088, 0.73, 1.096, 1.48, 0.232, "Satellite"),
                    new SpaceObject("Tethys", "https://en.wikipedia.org/wiki/Tethys_(moon)", 533, 0.083, 0.624, 0.6173, 1.15, 0.145, "Satellite"),
                    new SpaceObject("Enceladus", "https://en.wikipedia.org/wiki/Enceladus_(moon)", 252.1, 0.039, 0.067, 0.108, 1.61, 0.111, "Satellite"),
                    new SpaceObject("Mimas", "https://en.wikipedia.org/wiki/Mimas_(moon)", 198.3, 0.031, 0.033, 0.03749, 1.15, 0.06363, "Satellite")
                }),
                new SpaceObject("Uranus", "https://en.wikipedia.org/wiki/Uranus", 25362, 3.98, 68340, 86832, 1.30, 8.87, "Planet", new List<SpaceObject>() {
                    new SpaceObject("Titania", "https://en.wikipedia.org/wiki/Titania_(moon)", 788.9, 0.124, 2.06, 3.526, 1.72, 0.378, "Satellite"),
                    new SpaceObject("Oberon", "https://en.wikipedia.org/wiki/Oberon_(moon)", 761.4, 0.12, 1.85, 3.014, 1.63, 0.347, "Satellite"),
                    new SpaceObject("Umbriel", "https://en.wikipedia.org/wiki/Umbriel_(moon)", 584.7, 0.092, 0.84, 1.2, 1.4, 0.234, "Satellite"),
                    new SpaceObject("Ariel", "https://en.wikipedia.org/wiki/Ariel_(moon)", 578.9, 0.091, 0.81, 1.35, 1.67, 0.269, "Satellite"),
                    new SpaceObject("Miranda", "https://en.wikipedia.org/wiki/Miranda_(moon)", 235.8, 0.037, 0.055, 0.0659, 1.20, 0.07910375, "Satellite"),
                }),
                new SpaceObject("Neptune", "https://en.wikipedia.org/wiki/Neptune", 24622, 3.87, 62540, 102430, 1.76, 11.15, "Planet", new List<SpaceObject>() {
                    new SpaceObject("Triton", "https://en.wikipedia.org/wiki/Triton_(moon)", 1353.4, 0.212, 10.38, 21.5, 2.061, 0.782, "Satellite"),
                    new SpaceObject("Proteus", "https://en.wikipedia.org/wiki/Proteus_(moon)", 210, 0.033, 0.038, 0.050, 1.3, 0.0666, "Satellite"),
                }),
                new SpaceObject("Eris", "https://en.wikipedia.org/wiki/Eris_(dwarf_planet)", 1170, 0.182, 7, 16.7, 2.25, 0.662, "Dwarf planet"),
                new SpaceObject("Pluto", "https://en.wikipedia.org/wiki/Pluto", 1153, 0.181, 7.15, 13.105, 2.0, 0.61, "Dwarf planet"),
                new SpaceObject("Makemake", "https://en.wikipedia.org/wiki/Makemake_(dwarf_planet)", 710, 0.126, 1.8, 3, 2.0, 0.4, "Dwarf planet"),
                new SpaceObject("Haumea", "https://en.wikipedia.org/wiki/Haumea_(dwarf_planet)", 575, 0.117, 1.3, 4.006, 3, 0.44, "Dwarf planet"),
                new SpaceObject("Ceres", "https://en.wikipedia.org/wiki/Ceres_(dwarf_planet)", 475, 0.076, 0.437, 0.95, 2.08, 0.27, "Dwarf planet"),
                new SpaceObject("Pallas", "https://en.wikipedia.org/wiki/2_Pallas", 266, 0.0042, 0.078, 0.211, 2.8, 0.2, "Asteroid"),
                new SpaceObject("Vesta", "https://en.wikipedia.org/wiki/4_Vesta", 264.6, 0.0042, 0.078, 0.262, 3.42, 0.251, "Asteroid")
            })
        };
    }
}
csharp
// ...
builder.Services.AddSingleton<SpaceObjectDataProvider>();

For additional information about templates in the TreeList component, refer to the following topic: Templates in Blazor TreeList.

Cell Editing Specifics

During edit operations, the TreeList applies user changes only to the edit model and keeps the data item unchanged. DataItem, DisplayText, and Value context parameters return information about the data item, not the edit model. As a result, values of these properties do not reflect unsaved changes.

When the EditMode is set to CellEdit, only one TreeList cell can be in edit mode while others are in display mode. The CellDisplayTemplate affects contents of cells in display mode, including cells with modified values. To display unsaved changes in the CellDisplayTemplate, use the edit model’s field values instead of properties of the template’s context parameter:

razor
@inject EmployeeTaskService EmployeeTaskService

<DxTreeList Data="TreeListData"
            KeyFieldName="Id"
            ParentKeyFieldName="ParentId"
            EditMode="TreeListEditMode.EditCell"
            EditModelSaving="TreeList_EditModelSaving"
            DataItemDeleting="TreeList_DataItemDeleting"
            CustomizeEditModel="TreeList_CustomizeEditModel">
    <Columns>
        <DxTreeListCommandColumn EditButtonVisible="false"
                                 CancelButtonVisible="false"
                                 SaveButtonVisible="false" />
        <DxTreeListDataColumn FieldName="Name" Caption="Task">
            <CellDisplayTemplate Context="displayContext">
                @{
                    string value;
                    if (displayContext.TreeList.IsEditingRow(displayContext.VisibleIndex)) {
                        var editModel = (EmployeeTask)displayContext.TreeList.GetEditContext().Model;
                        value = editModel.Name;
                    }
                    else
                        value = displayContext.DisplayText;
                }
                <span>@value</span>
            </CellDisplayTemplate>
        </DxTreeListDataColumn>
        <DxTreeListDataColumn FieldName="EmployeeName" />
        <DxTreeListDataColumn FieldName="StartDate" />
        <DxTreeListDataColumn FieldName="DueDate" />
    </Columns>
</DxTreeList>

@code {
    List<EmployeeTask> TreeListData { get; set; }

    protected override void OnInitialized() {
        TreeListData = EmployeeTaskService.GenerateData();
    }
    void TreeList_CustomizeEditModel(TreeListCustomizeEditModelEventArgs e) {
        if(e.IsNew) {
            var newTask = (EmployeeTask)e.EditModel;
            newTask.Id = TreeListData.Max(x => x.Id) + 1;
            if(e.ParentDataItem != null)
                newTask.ParentId = ((EmployeeTask)e.ParentDataItem).Id;
        }
    }
    async Task TreeList_EditModelSaving(TreeListEditModelSavingEventArgs e) {
        if(e.IsNew)
            TreeListData.Add((EmployeeTask)e.EditModel);
        else
            e.CopyChangesToDataItem();
    }
    async Task TreeList_DataItemDeleting(TreeListDataItemDeletingEventArgs e) {
        TreeListData.Remove((EmployeeTask)e.DataItem);
    }
}
csharp
public class EmployeeTask{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
    public string EmployeeName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime DueDate { get; set; }

    public EmployeeTask(
        int id,
        int parentId,
        string name,
        string employeeName,
        DateTime startDate,
        DateTime dueDate
    )
    {
        Id = id;
        ParentId = parentId;
        Name = name;
        EmployeeName = employeeName;
        StartDate = startDate;
        DueDate = dueDate;
    }
}
csharp
public class EmployeeTaskService {
    public List<EmployeeTask> GenerateData() {
        return new List<EmployeeTask>() {
            new EmployeeTask(1, 0, "Simplify & Clarify Product Messaging", "John Heart", new DateTime(2018, 4, 3), new DateTime(2018, 4, 14)),
            new EmployeeTask(2, 1, "Prepare Financial Reports", "Samantha Bright", new DateTime(2018, 4, 3), new DateTime(2018, 4, 7)),
            new EmployeeTask(3, 1, "Prepare Marketing Plan", "Arthur Miller", new DateTime(2018, 4, 7), new DateTime(2018, 4, 14)),
            new EmployeeTask(4, 0, "Create Action Plan To Improve Customer Engagement", "Robert Reagan", new DateTime(2017, 8, 8), new DateTime(2018, 4, 8)),
            new EmployeeTask(5, 4, "Update Personnel Files", "Greta Sims", new DateTime(2017, 8, 8), new DateTime(2017, 10, 18)),
            new EmployeeTask(6, 4, "Review Health Insurance Options ", "Brett Wade", new DateTime(2017, 9, 27), new DateTime(2017, 11, 10)),
            new EmployeeTask(7, 4, "Choose Between PPO And HMO Health Plan", "Sandra Johnson", new DateTime(2017, 12, 13), new DateTime(2018, 3, 23)),
            new EmployeeTask(8, 4, "Update Google Adwords Strategy", "Ed Holmes", new DateTime(2017, 8, 23), new DateTime(2017, 12, 23)),
            new EmployeeTask(9, 4, "Create New Brochure Design", "Barb Banks", new DateTime(2018, 1, 3), new DateTime(2018, 3, 14)),
            new EmployeeTask(10, 4, "Obtain Price Quote For New Brochure", "Kevin Carter", new DateTime(2018, 2, 1), new DateTime(2018, 3, 15)),
            new EmployeeTask(11, 4, "Brochure Design Review", "Cindy Stanwick", new DateTime(2017, 8, 22), new DateTime(2017, 10, 28)),
            new EmployeeTask(12, 4, "Review Website Re-Design Strategy", "Sammy Hill", new DateTime(2017, 9, 16), new DateTime(2018, 3, 6)),
            new EmployeeTask(13, 4, "Rollout New Website", "Davey Jones", new DateTime(2017, 11, 7), new DateTime(2018, 2, 6)),
            new EmployeeTask(14, 4, "Update Sales/Marketing Strategy", "Victor Norris", new DateTime(2017, 12, 13), new DateTime(2018, 4, 2)),
            new EmployeeTask(15, 4, "Update Sales/Revenue Report", "Mary Stern", new DateTime(2017, 12, 25), new DateTime(2018, 4, 2)),
            new EmployeeTask(16, 4, "Direct Vs Online Sales Comparison Report", "Robin Cosworth", new DateTime(2018, 1, 2), new DateTime(2018, 3, 20)),
            new EmployeeTask(17, 4, "Review Sales Report and Approve Modifications", "Kelly Rodriguez", new DateTime(2017, 9, 4), new DateTime(2017, 10, 30)),
            new EmployeeTask(18, 4, "Update R&D Strategy", "James Anderson", new DateTime(2017, 11, 13), new DateTime(2017, 12, 4)),
            new EmployeeTask(19, 4, "Discuss Updated R&D Strategy", "Antony Remmen", new DateTime(2017, 10, 29), new DateTime(2017, 12, 31)),
            new EmployeeTask(20, 4, "Update QA Strategy", "Olivia Peyton", new DateTime(2017, 10, 31), new DateTime(2017, 11, 2)),
            new EmployeeTask(21, 4, "Schedule Training Events", "Taylor Riley", new DateTime(2017, 11, 19), new DateTime(2018, 4, 7)),
            new EmployeeTask(22, 4, "Approve The Hiring Of John Jeffers", "Amelia Harper", new DateTime(2018, 1, 7), new DateTime(2018, 4, 8)),
            new EmployeeTask(23, 0, "Increase Average Subscription Price ", "Wally Hobbs", new DateTime(2017, 8, 9), new DateTime(2017, 9, 13)),
            new EmployeeTask(24, 23, "Update Non-Compete Agreements", "Brad Jameson", new DateTime(2017, 8, 9), new DateTime(2017, 9, 3)),
            new EmployeeTask(25, 23, "Update Employee Records With New NDA", "Karen Goodson", new DateTime(2017, 8, 23), new DateTime(2018, 9, 10))
        };
    }
}
csharp
// ...
builder.Services.AddSingleton<EmployeeTaskService>();

Implements

CellDisplayTemplate

See Also

DxTreeListDataColumn Class

DxTreeListDataColumn Members

DevExpress.Blazor Namespace