|
| 1 | +--- |
| 2 | +title: How to Display a Popup in a Grid Cell |
| 3 | +description: Learn how to display a popup beside an icon embedded in a cell of the Grid for Blazor. |
| 4 | +type: how-to |
| 5 | +page_title: How to Display Popup Next to Icon in Grid for Blazor |
| 6 | +slug: grid-kb-popup-in-cell |
| 7 | +tags: grid, blazor, popup, cell |
| 8 | +res_type: kb |
| 9 | +ticketid: 1689992, 1640846 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +I want to display a Popup beside an icon embedded within a cell in the [Grid for Blazor](slug:grid-overview). The Popup should appear when the icon is clicked. |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To display a Popup within a grid cell: |
| 30 | + |
| 31 | +1. Define a dictionary to store references to all popups associated with the Grid items. This ensures that each Popup has a unique identifier tied to its corresponding item. |
| 32 | + |
| 33 | +2. Use the `AnchorSelector` parameter of the `TelerikPopup` component to dynamically associate each popup with its corresponding icon. Set the `AnchorSelector` to the unique ID of the icon element. |
| 34 | + |
| 35 | +Here is an example implementation: |
| 36 | + |
| 37 | +````RAZOR |
| 38 | +<TelerikGrid Data="@GridData" |
| 39 | + TItem="@SampleModel" |
| 40 | + Pageable="true" |
| 41 | + Sortable="true" |
| 42 | + FilterMode="GridFilterMode.FilterRow"> |
| 43 | + <GridColumns> |
| 44 | + <GridColumn Field="@nameof(SampleModel.Name)"> |
| 45 | + <Template> |
| 46 | + @{ |
| 47 | + var dataItem = (SampleModel)context; |
| 48 | + if (!PopupRefs.ContainsKey(dataItem.Id)) |
| 49 | + { |
| 50 | + PopupRefs.Add(dataItem.Id, null); |
| 51 | + } |
| 52 | + } |
| 53 | + <TelerikButton Id="@( $"button{dataItem.Id}" )" Icon="@SvgIcon.DocumentManager" |
| 54 | + OnClick="@( () => PopupRefs[dataItem.Id]?.Show() )" /> |
| 55 | + <TelerikPopup @ref="@PopupRefs[dataItem.Id]" |
| 56 | + AnchorSelector="@( $"#button{dataItem.Id}" )" |
| 57 | + AnchorHorizontalAlign="@PopupAnchorHorizontalAlign.Right" |
| 58 | + AnchorVerticalAlign="@PopupAnchorVerticalAlign.Top" |
| 59 | + HorizontalAlign="@PopupHorizontalAlign.Left" |
| 60 | + VerticalAlign="@PopupVerticalAlign.Top" |
| 61 | + Width="240px" |
| 62 | + Height="140px"> |
| 63 | + <div style="padding:2em;"> |
| 64 | + Popup for item |
| 65 | + <TelerikButton OnClick="@( () => PopupRefs[dataItem.Id]?.Hide() )">Hide Popup for @dataItem.Name</TelerikButton> |
| 66 | + </div> |
| 67 | + </TelerikPopup> |
| 68 | + </Template> |
| 69 | + </GridColumn> |
| 70 | + <GridColumn Field="@nameof(SampleModel.Price)" /> |
| 71 | + <GridColumn Field="@nameof(SampleModel.Quantity)" /> |
| 72 | + </GridColumns> |
| 73 | +</TelerikGrid> |
| 74 | +
|
| 75 | +@code { |
| 76 | + private List<SampleModel> GridData { get; set; } = new(); |
| 77 | + private Dictionary<int, TelerikPopup?> PopupRefs { get; set; } = new(); |
| 78 | +
|
| 79 | + protected override void OnInitialized() |
| 80 | + { |
| 81 | + var rnd = new Random(); |
| 82 | +
|
| 83 | + for (int i = 1; i <= 7; i++) |
| 84 | + { |
| 85 | + GridData.Add(new SampleModel() |
| 86 | + { |
| 87 | + Id = i, |
| 88 | + Name = $"Name {i}", |
| 89 | + GroupName = $"Group {i % 3 + 1}", |
| 90 | + Price = rnd.Next(1, 100) * 1.23m, |
| 91 | + Quantity = rnd.Next(0, 1000), |
| 92 | + StartDate = DateTime.Now.AddDays(-rnd.Next(60, 1000)), |
| 93 | + IsActive = i % 4 > 0 |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | +
|
| 98 | + public class SampleModel |
| 99 | + { |
| 100 | + public int Id { get; set; } |
| 101 | + public int? ParentId { get; set; } |
| 102 | + public string Name { get; set; } = string.Empty; |
| 103 | + public string GroupName { get; set; } = string.Empty; |
| 104 | + public decimal Price { get; set; } |
| 105 | + public int Quantity { get; set; } |
| 106 | + public object Icon { get; set; } = SvgIcon.File; |
| 107 | + public DateTime StartDate { get; set; } |
| 108 | + public DateTime EndDate { get; set; } |
| 109 | + public bool IsActive { get; set; } |
| 110 | + } |
| 111 | +} |
| 112 | +```` |
| 113 | + |
| 114 | +## See Also |
| 115 | + |
| 116 | +* [Grid Overview Documentation](slug:grid-overview) |
| 117 | +* [Popup Overview Documentation](slug:popup-overview) |
0 commit comments