Completed
Last Updated: 03 Feb 2021 17:22 by ADMIN
De
Created on: 28 Jan 2021 02:50
Category: UI for Blazor
Type: Feature Request
0
Hierarchy Blazor grid: Pass user defined data in when adding new row

I would like to pass a defined piece of data in when I add a new row in a hierarchical Blazor grid.  in the example below, I have a hierarchy of Agency > District > School.  When I add a new school within a district, I would like to pass the District ID so that the user does not have to select it or type it in. 

I have a district ID column in the schools grid, but (a) it would be better if the user did not see this and (b) when I click "Add school" that field is always passed as empty.  Is there any way to pass the 

districtSchools.Dist.DISTRICT_ID

variable when I add a row?


<TelerikGrid Data="@Agencies" Sortable="true" Reorderable="true" OnUpdate="@UpdateHandlerAgency">
                <DetailTemplate Context="granteeAgency">
                    @{
                        var leadAgency = granteeAgency as Agency;
                        <TelerikGrid Data="leadAgency.Districts" OnUpdate="@UpdateHandlerDistrict" OnRowRender="@OnRowRenderHandlerDistrict">
                            <GridColumns>
                                <GridColumn Field="@(nameof(District.Dist.DistrictName))" Editable="false">
                                    <Template>
                                        @((context as District).Dist.DistrictName.ToString())
                                    </Template>
                                </GridColumn>
                                <GridColumn Field="@(nameof(District.DistAlloc))">
                                    <Template>
                                        @((context as District).DistAlloc.ToString("C"))
                                    </Template>
                                </GridColumn>
                                <GridCommandColumn>
                                    <GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
                                    <GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
                                    <GridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</GridCommandButton>
                                </GridCommandColumn>
                            </GridColumns>
                            <DetailTemplate Context="districtSchools">
                                    <TelerikGrid Data="districtSchools.Schools" OnCreate="@CreateHandlerSchool" OnDelete="@DeleteHandlerSchool" OnUpdate="@UpdateHandlerSchool" OnRowRender="@OnRowRenderHandlerSchool">
                                        <GridToolBar>
                                            <GridCommandButton Command="Add" Icon="add">Add School</GridCommandButton>
                                        </GridToolBar>
                                        <GridColumns>
                                            <GridColumn Field="@(nameof(School.SchoolName))">
                                                <Template>
                                                    @((context as School).SchoolName.ToString())
                                                </Template>
                                            </GridColumn>
                                            <GridColumn Field="@(nameof(School.SchoolAlloc))">
                                                <Template>
                                                    @((context as School).SchoolAlloc.ToString("C"))
                                                </Template>
                                            </GridColumn>
                                            <GridColumn Field="" Visible="true">
                                                <Template>
                                                    @districtSchools.Dist.DISTRICT_ID
                                                </Template>
                                                <EditorTemplate>
                                                    @districtSchools.Dist.DISTRICT_ID
                                                </EditorTemplate>
                                            </GridColumn>
                                            <GridCommandColumn>
                                                <GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
                                                <GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
                                                <GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton>
                                                <GridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</GridCommandButton>
                                            </GridCommandColumn>
                                        </GridColumns>
                                    </TelerikGrid>
                            </DetailTemplate>
                        </TelerikGrid>
                    }
                </DetailTemplate>
                <GridColumns>
                    <GridColumn Field="@(nameof(Agency.AgencyName))" Editable="false">
                        <Template>
                            @((context as Agency).AgencyName.ToString())
                        </Template>
                    </GridColumn>
                    <GridColumn Field="@(nameof(Agency.AgencyAlloc))">
                        <Template>
                            @((context as Agency).AgencyAlloc.ToString("C"))
                        </Template>
                    </GridColumn>
                    <GridCommandColumn>
                        <GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
                        <GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
                        <GridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</GridCommandButton>
                    </GridCommandColumn>
                </GridColumns>
            </TelerikGrid>

1 comment
ADMIN
Nadezhda Tacheva
Posted on: 03 Feb 2021 17:22

Hi De,

There are a couple of ways to proceed here as per this knowledge base article and this one.

Include the parent Id in the child model

One approach could be to include the DistrictID as field in the model of the Schools. This way on inserting/editing the Child element, in your case the School, you will be able to provide information for the District ID.

Since you don't want the user to see the DistrictID, you may not initialize a column in the Grid for it, it will be still part of the data.

For inserting a new item (School) - make sure to provide the DistrictID from parent to the newly created School.

To better illustrate how to achieve the desired behavior, I've created an example project with 2-level hierarchical Grid (see attached). The CUD operations are only enabled for the child Grid for brevity.

Use the grid State

You can also use the Grid State to define the desired default values for the Inserted/Edited item. You may also check this part of the article for reference.

Regards,
Nadezhda Tacheva
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Attached Files: