Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KB] Add Grid Inline Form KB #2110

Merged
merged 21 commits into from
May 22, 2024
Merged
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 213 additions & 0 deletions knowledge-base/grid-inline-edit-form.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
---
title: Add inline Telerik Form to the the Grid Rows
svdimitr marked this conversation as resolved.
Show resolved Hide resolved
description: How to add inline Telerik Form to the Grid Rows.
svdimitr marked this conversation as resolved.
Show resolved Hide resolved
type: how-to
page_title: Add inline Telerik Form to the the Grid Rows
slug: grid-kb-inline-form
position:
tags: telerik,blazor,grid,inline,form,rows
svdimitr marked this conversation as resolved.
Show resolved Hide resolved
res_type: kb
svdimitr marked this conversation as resolved.
Show resolved Hide resolved
---

## Environment

<table>
<tbody>
<tr>
<td>Product</td>
<td>Grid for Blazor</td>
</tr>
</tbody>
</table>


## Description

I want to add an inline Telerik Form to the Grid rows when I enter edit mode.
svdimitr marked this conversation as resolved.
Show resolved Hide resolved


## Solution

Add an inline [Telerik Form]({%slug form-overview%}) to the Grid rows by following these steps:
svdimitr marked this conversation as resolved.
Show resolved Hide resolved

* Define a [DetailTemplate]({%slug components/grid/features/hierarchy%}).
svdimitr marked this conversation as resolved.
Show resolved Hide resolved
svdimitr marked this conversation as resolved.
Show resolved Hide resolved
* Hide the hierarchy expand column with CSS.
* Use the [SetStateAsync() method]({%slug grid-state%}#setstateasync-examples) to enter and exit edit mode programmatically.
svdimitr marked this conversation as resolved.
Show resolved Hide resolved

>tip The Telerik Form works with a cloned instance of the edited/added item to support cancellation. If you cancel the addition or update of an item, you need to delete it from the Grid Data collection.
svdimitr marked this conversation as resolved.
Show resolved Hide resolved

````CSHTML
@using System.ComponentModel.DataAnnotations

<TelerikGrid @ref="@GridRef"
Data="@GridData"
Class="no-expand-column">
<GridToolBarTemplate>
<GridCommandButton OnClick="@OnGridAddButtonClick" Icon="@SvgIcon.Plus">Add</GridCommandButton>
</GridToolBarTemplate>
<GridColumns>
<GridColumn Field="@nameof(SampleModel.Id)" />
<GridColumn Field="@nameof(SampleModel.Name)" />
<GridColumn Field="@nameof(SampleModel.Price)" DisplayFormat="{0:c2}" />
<GridColumn Field="@nameof(SampleModel.Quantity)" />
<GridColumn Field="@nameof(SampleModel.StartDate)" DisplayFormat="{0:d}" />
<GridCommandColumn>
@{
var item = (SampleModel)context;
}
<GridCommandButton OnClick="@( () => OnGridEditButtonClick(item) )" Icon="@SvgIcon.Pencil">Edit</GridCommandButton>
svdimitr marked this conversation as resolved.
Show resolved Hide resolved
</GridCommandColumn>
</GridColumns>
<DetailTemplate>
<TelerikForm Model="@GridItemInEditMode"
OnValidSubmit="@OnGridFormValidSubmit"
Columns="2"
ColumnSpacing="2em">
<FormValidation>
<DataAnnotationsValidator />
</FormValidation>
<FormButtons>
<TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary"
Icon="@SvgIcon.Save">
Save
</TelerikButton>
<TelerikButton ButtonType="@ButtonType.Button"
OnClick="@OnGridFormCancel"
Icon="@SvgIcon.Cancel">
Cancel
</TelerikButton>
</FormButtons>
</TelerikForm>
</DetailTemplate>
</TelerikGrid>

<style>
.no-expand-column .k-hierarchy-col {
width: 0;
}

.no-expand-column .k-hierarchy-cell + .k-table-td,
.no-expand-column .k-hierarchy-cell + .k-table-th {
border-left-width: 0;
}

.no-expand-column .k-hierarchy-cell > * {
display: none;
}

.no-expand-column .k-detail-row:hover {
background-color: transparent;
}
svdimitr marked this conversation as resolved.
Show resolved Hide resolved
</style>

@code {
#nullable enable
svdimitr marked this conversation as resolved.
Show resolved Hide resolved

private List<SampleModel> GridData { get; set; } = new();

private TelerikGrid<SampleModel> GridRef { get; set; } = null!;
svdimitr marked this conversation as resolved.
Show resolved Hide resolved

private SampleModel? GridItemInEditMode { get; set; }

private async Task OnGridEditButtonClick(SampleModel item)
{
GridItemInEditMode = item.Clone();

var gridState = GridRef.GetState();
gridState.ExpandedItems = new List<SampleModel>() { item };

await GridRef.SetStateAsync(gridState);
}

private async Task OnGridAddButtonClick()
{
var newItem = new SampleModel();
GridData.Insert(0, newItem);
GridRef.Rebind();

await OnGridEditButtonClick(newItem);
}

private async Task OnGridFormValidSubmit()
{
if (GridItemInEditMode == null)
{
return;
}

var originalItemIndex = GridData.FindIndex(x => x.Id == GridItemInEditMode!.Id);

if (GridItemInEditMode!.Id == 0)
{
GridItemInEditMode.Id = ++LastId;
}

GridData[originalItemIndex] = GridItemInEditMode!;

var gridState = GridRef.GetState();
gridState.ExpandedItems = new List<SampleModel>();

await GridRef.SetStateAsync(gridState);

GridItemInEditMode = null;
}

private async Task OnGridFormCancel()
{
if (GridItemInEditMode?.Id == 0)
{
var itemIndex = GridData.FindIndex(x => x.Id == 0);
GridData.RemoveAt(itemIndex);
}

var gridState = GridRef.GetState();
gridState.ExpandedItems = new List<SampleModel>();

await GridRef.SetStateAsync(gridState);

GridItemInEditMode = null;
}

private int LastId { get; set; }

protected override void OnInitialized()
{
var rnd = new Random();

for (int i = 1; i <= 5; i++)
{
GridData.Add(new SampleModel()
{
Id = ++LastId,
Name = $"Name {LastId}",
Price = rnd.Next(1, 100) * 1.23m,
Quantity = rnd.Next(0, 1000),
StartDate = DateTime.Today.AddDays(-rnd.Next(1, 30)).AddMonths(-rnd.Next(1, 100))
});
Comment on lines +183 to +189
Copy link
Contributor

@dimodi dimodi May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does VS for Windows really add one more tab here? VS Mac doesn't do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was formatted by VS for Windows. I am not sure why.

}
}

public class SampleModel
{
[Display(AutoGenerateField = false)]
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public int Quantity { get; set; }
public DateTime? StartDate { get; set; }

public SampleModel Clone()
{
return new SampleModel()
{
Id = Id,
Name = Name,
Price = Price,
Quantity = Quantity,
StartDate = StartDate
};
}
}
}
````
svdimitr marked this conversation as resolved.
Show resolved Hide resolved
Loading