|
| 1 | +--- |
| 2 | +title: Popup Form Template |
| 3 | +page_title: Grid Popup Form Template |
| 4 | +description: Learn how to define a custom popup create or edit template in the Blazor Data Grid. The template allows you to customize the layout and the content of the create/edit popup. |
| 5 | +slug: grid-templates-popup-form |
| 6 | +tags: telerik,blazor,grid,templates,popup,edit,create |
| 7 | +published: True |
| 8 | +position: 50 |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | +# Popup Form Template |
| 13 | + |
| 14 | +With the `FormTemplate` feature, you can customize the appearance and content of the create/edit Popup window of the Grid. |
| 15 | + |
| 16 | +>caption Using a `FormTemplate` to modify the Edit/Create Popup window. |
| 17 | +
|
| 18 | +````CSHTML |
| 19 | +@using System.Collections.Generic; |
| 20 | +
|
| 21 | +<TelerikGrid @ref="@GridRef" |
| 22 | + Data="@GridData" |
| 23 | + EditMode="@GridEditMode.Popup" |
| 24 | + Pageable="true" |
| 25 | + Width="950px" |
| 26 | + PageSize="5" |
| 27 | + OnDelete="@DeleteItem"> |
| 28 | + <GridToolBarTemplate> |
| 29 | + <GridCommandButton Command="Add" Icon="@FontIcon.Plus">Add Employee</GridCommandButton> |
| 30 | + </GridToolBarTemplate> |
| 31 | + <GridSettings> |
| 32 | + <GridPopupEditSettings MaxHeight="95vh" MaxWidth="95vw"></GridPopupEditSettings> |
| 33 | + <GridPopupEditFormSettings ColumnSpacing="20px" Orientation="@FormOrientation.Horizontal" Columns="2"> |
| 34 | + <FormTemplate> |
| 35 | + @{ |
| 36 | + EditItem = context.Item as Person; |
| 37 | +
|
| 38 | + <TelerikForm Model="@EditItem" OnValidSubmit="@OnValidSubmit"> |
| 39 | + <FormItems> |
| 40 | + <FormItem Field="EmployeeId" Enabled="false"></FormItem> |
| 41 | + <FormItem Field="Name"></FormItem> |
| 42 | + <FormItem Field="AgeInYears" LabelText="Custom Age Label:"></FormItem> |
| 43 | + <FormItem Field="HireDate" LabelText="Custom Hire Date Label:"></FormItem> |
| 44 | + </FormItems> |
| 45 | + <FormButtons> |
| 46 | + <TelerikButton Icon="@nameof(FontIcon.Save)">Save</TelerikButton> |
| 47 | + <TelerikButton Icon="@nameof(FontIcon.Cancel)" ButtonType="@ButtonType.Button" OnClick="@OnCancel">Cancel</TelerikButton> |
| 48 | + </FormButtons> |
| 49 | + </TelerikForm> |
| 50 | + } |
| 51 | + </FormTemplate> |
| 52 | + </GridPopupEditFormSettings> |
| 53 | + </GridSettings> |
| 54 | + <GridColumns> |
| 55 | + <GridColumn Field=@nameof(Person.EmployeeId) Editable="false" /> |
| 56 | + <GridColumn Field=@nameof(Person.Name) /> |
| 57 | + <GridColumn Field=@nameof(Person.AgeInYears) Title="Age" /> |
| 58 | + <GridColumn Field=@nameof(Person.HireDate) Title="Hire Date" /> |
| 59 | + <GridCommandColumn> |
| 60 | + <GridCommandButton Command="Edit" Icon="@FontIcon.Pencil">Edit</GridCommandButton> |
| 61 | + <GridCommandButton Command="Delete" Icon="@FontIcon.Trash">Delete</GridCommandButton> |
| 62 | + </GridCommandColumn> |
| 63 | + </GridColumns> |
| 64 | +</TelerikGrid> |
| 65 | +
|
| 66 | +@code { |
| 67 | + public TelerikGrid<Person> GridRef { get; set; } |
| 68 | + public List<Person> GridData { get; set; } |
| 69 | + public Person EditItem { get; set; } |
| 70 | + private List<Person> _people; |
| 71 | +
|
| 72 | + public List<Person> People |
| 73 | + { |
| 74 | + get |
| 75 | + { |
| 76 | + if (_people == null) |
| 77 | + { |
| 78 | + _people = GeneratePeople(30); |
| 79 | + } |
| 80 | +
|
| 81 | + return _people; |
| 82 | + } |
| 83 | + } |
| 84 | +
|
| 85 | + protected override void OnInitialized() |
| 86 | + { |
| 87 | + LoadData(); |
| 88 | + } |
| 89 | +
|
| 90 | + private void LoadData() |
| 91 | + { |
| 92 | + GridData = GetPeople(); |
| 93 | + } |
| 94 | +
|
| 95 | + private void DeleteItem(GridCommandEventArgs args) |
| 96 | + { |
| 97 | + DeletePerson(args.Item as Person); |
| 98 | +
|
| 99 | + LoadData(); |
| 100 | + } |
| 101 | +
|
| 102 | + private async Task OnValidSubmit() |
| 103 | + { |
| 104 | +
|
| 105 | + if (EditItem.EmployeeId != default) |
| 106 | + { |
| 107 | + UpdatePerson(EditItem); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + CreatePerson(EditItem); |
| 112 | + } |
| 113 | +
|
| 114 | + await ExitEditAsync(); |
| 115 | +
|
| 116 | + LoadData(); |
| 117 | + } |
| 118 | +
|
| 119 | + private async Task OnCancel() |
| 120 | + { |
| 121 | + await ExitEditAsync(); |
| 122 | + } |
| 123 | +
|
| 124 | + private async Task ExitEditAsync() |
| 125 | + { |
| 126 | + var state = GridRef?.GetState(); |
| 127 | + state.OriginalEditItem = null; |
| 128 | + state.EditItem = null; |
| 129 | + state.InsertedItem = null; |
| 130 | +
|
| 131 | + await GridRef?.SetStateAsync(state); |
| 132 | + } |
| 133 | +
|
| 134 | + #region Service Methods |
| 135 | + public List<Person> GetPeople() |
| 136 | + { |
| 137 | + return People; |
| 138 | + } |
| 139 | +
|
| 140 | + public DataSourceResult GetPeople(DataSourceRequest request) |
| 141 | + { |
| 142 | + return People.ToDataSourceResult(request); |
| 143 | + } |
| 144 | +
|
| 145 | + public void DeletePerson(Person person) |
| 146 | + { |
| 147 | + People.Remove(person); |
| 148 | + } |
| 149 | +
|
| 150 | + public void UpdatePerson(Person person) |
| 151 | + { |
| 152 | + var index = People.FindIndex(i => i.EmployeeId == person.EmployeeId); |
| 153 | + if (index != -1) |
| 154 | + { |
| 155 | + People[index] = person; |
| 156 | + } |
| 157 | + } |
| 158 | +
|
| 159 | + public void CreatePerson(Person person) |
| 160 | + { |
| 161 | + person.EmployeeId = People.Max(x => x.EmployeeId) + 1; |
| 162 | +
|
| 163 | + People.Insert(0, person); |
| 164 | + } |
| 165 | +
|
| 166 | + private List<Person> GeneratePeople(int count, int startIndex = 0) |
| 167 | + { |
| 168 | + List<Person> result = new List<Person>(); |
| 169 | +
|
| 170 | + for (int i = startIndex; i < startIndex + count; i++) |
| 171 | + { |
| 172 | + result.Add(new Person() |
| 173 | + { |
| 174 | + EmployeeId = i, |
| 175 | + Name = "Employee " + i.ToString(), |
| 176 | + AgeInYears = i, |
| 177 | + GraduateGrade = (i % 6) + 1, |
| 178 | + HireDate = new DateTime(2020, 6, 1).Date.AddDays(count - (i % 7)), |
| 179 | + MeetingDate = new DateTime(2020, 6, 1).Date.AddDays((i % 4)), |
| 180 | + IsOutOfOffice = i % 3 == 0 ? true : false |
| 181 | + }); |
| 182 | + } |
| 183 | +
|
| 184 | + return result; |
| 185 | + } |
| 186 | + #endregion |
| 187 | +} |
| 188 | +```` |
| 189 | + |
| 190 | +## See Also |
| 191 | + |
| 192 | + * [Grid Popup Buttons Template]({%slug grid-templates-popup-buttons%}) |
| 193 | + * [Live Demo: Grid Templates](https://demos.telerik.com/blazor-ui/grid/templates) |
| 194 | + * [Live Demo: Grid Popup Edit Form Template](https://demos.telerik.com/blazor-ui/grid/popup-edit-form-template) |
0 commit comments