|
| 1 | +--- |
| 2 | +title: Keep Form Items Rendering Order after Show/Hide |
| 3 | +description: How to keep the form item rendering order when changing the form item visibility. |
| 4 | +type: how-to |
| 5 | +page_title: Keep Form Items Rendering Order after Show/Hide |
| 6 | +slug: form-kb-keep-fields-order |
| 7 | +position: |
| 8 | +tags: |
| 9 | +ticketid: 1647539 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Form for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +This knowledge base article answers the following questions: |
| 26 | + |
| 27 | +* How do I keep the rendering order of Form items? |
| 28 | +* Is it possible to toggle the visibility of a Form item and keep its rendering order instead of showing it as last? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +The `<FormItems>` tag doesn't render Form items directly. This tag is used only to register `<FormItem>` instances. The Form considers conditional field items, whose visibility may change, as the last items to be processed. |
| 33 | + |
| 34 | +To preserve the rendering place of each form item, you need to use the `TelerikFormItemRenderer`. |
| 35 | + |
| 36 | +````CSHTML |
| 37 | +<TelerikForm Model="@model"> |
| 38 | + <FormItems> |
| 39 | +
|
| 40 | + <FormItem Field="@nameof(FormModel.Field1)"> |
| 41 | + <Template> |
| 42 | + <label>Field1</label> |
| 43 | + <TelerikRadioGroup Data="@Items" TextField="@nameof(RadioButtonItem.TextField)" ValueField="@nameof(RadioButtonItem.ValueField)" @bind-Value="@model.Field1" |
| 44 | + Layout="RadioGroupLayout.Horizontal"> |
| 45 | + </TelerikRadioGroup> |
| 46 | + </Template> |
| 47 | + </FormItem> |
| 48 | +
|
| 49 | + <FormItem Field="@nameof(FormModel.Field2)"> |
| 50 | + <Template> |
| 51 | + <label>Field2</label> |
| 52 | + <TelerikNumericTextBox @bind-Value="@model.Field2"></TelerikNumericTextBox> |
| 53 | + </Template> |
| 54 | + </FormItem> |
| 55 | +
|
| 56 | + <FormItem Field="@nameof(FormModel.Field3)"> |
| 57 | + <Template> |
| 58 | + <label>Field3</label> |
| 59 | + <TelerikNumericTextBox @bind-Value="@model.Field3"></TelerikNumericTextBox> |
| 60 | + </Template> |
| 61 | + </FormItem> |
| 62 | +
|
| 63 | + </FormItems> |
| 64 | +
|
| 65 | + <FormItemsTemplate Context="formContext"> |
| 66 | + @{ |
| 67 | + var formItems = formContext.Items.Cast<IFormItem>(); |
| 68 | + } |
| 69 | + <TelerikFormItemRenderer Item="@formItems.First(x => x.Field == nameof(FormModel.Field1))" /> |
| 70 | + @if (model.Field1 == 2) |
| 71 | + { |
| 72 | + <TelerikFormItemRenderer Item="@formItems.First(x => x.Field == nameof(FormModel.Field2))" /> |
| 73 | + } |
| 74 | + <TelerikFormItemRenderer Item="@formItems.First(x => x.Field == nameof(FormModel.Field3))" /> |
| 75 | + </FormItemsTemplate> |
| 76 | +</TelerikForm> |
| 77 | +
|
| 78 | +@code { |
| 79 | + private FormModel model { get; set; } = new FormModel(); |
| 80 | +
|
| 81 | + private List<RadioButtonItem> Items = new List<RadioButtonItem>() |
| 82 | + { |
| 83 | + new RadioButtonItem { ValueField = 1, TextField = "Item 1" }, |
| 84 | + new RadioButtonItem { ValueField = 2, TextField = "Item 2" }, |
| 85 | + new RadioButtonItem { ValueField = 3, TextField = "Item 3" }, |
| 86 | + }; |
| 87 | +
|
| 88 | + public class FormModel |
| 89 | + { |
| 90 | + public int Field1 { get; set; } |
| 91 | + public int Field2 { get; set; } |
| 92 | + public int Field3 { get; set; } |
| 93 | + } |
| 94 | +
|
| 95 | + public class RadioButtonItem |
| 96 | + { |
| 97 | + public string TextField { get; set; } |
| 98 | + public int ValueField { get; set; } |
| 99 | + } |
| 100 | +} |
| 101 | +```` |
| 102 | + |
| 103 | +## See Also |
| 104 | + |
| 105 | +* [Form Template for All Items]({%slug form-formitems-formitemstemplate%}) |
0 commit comments