|
| 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: blazor, grid, popup, template |
| 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. Add a button that triggers the popup for each row by using a `GridCommandColumn` or a column [`Template`](slug:grid-templates-column). |
| 32 | + |
| 33 | +2. Anchor the [`Popover`](slug:popover-overview) to the button via `AnchorSelector`. |
| 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 | + <GridCommandColumn> |
| 45 | + @{ |
| 46 | + var dataItem = (SampleModel)context; |
| 47 | + } |
| 48 | + <GridCommandButton Id="@( $"button{dataItem.Id}" )" Icon="@SvgIcon.DocumentManager" |
| 49 | + OnClick="@OnPopupShowButtonClick" /> |
| 50 | + </GridCommandColumn> |
| 51 | + <GridColumn Field="@nameof(SampleModel.Price)" /> |
| 52 | + <GridColumn Field="@nameof(SampleModel.Quantity)" /> |
| 53 | + </GridColumns> |
| 54 | +</TelerikGrid> |
| 55 | +
|
| 56 | +<TelerikPopover @ref="@PopoverRef" |
| 57 | + AnchorSelector="@PopoverAnchorSelector" |
| 58 | + Position="@PopoverPosition.Right" |
| 59 | + Offset="20" |
| 60 | + Width="240px" |
| 61 | + Height="140px"> |
| 62 | + <PopoverHeader> |
| 63 | + Popover for @ModelInPopup?.Name |
| 64 | + </PopoverHeader> |
| 65 | + <PopoverContent> |
| 66 | + <div style="padding:2em;"> |
| 67 | + <TelerikButton OnClick="@( () => PopoverRef?.Hide() )">Hide</TelerikButton> |
| 68 | + </div> |
| 69 | + </PopoverContent> |
| 70 | +</TelerikPopover> |
| 71 | +
|
| 72 | +@code { |
| 73 | + private List<SampleModel> GridData { get; set; } = new(); |
| 74 | + private TelerikPopover? PopoverRef { get; set; } |
| 75 | +
|
| 76 | + private string PopoverAnchorSelector { get; set; } = "#button1"; |
| 77 | +
|
| 78 | + private SampleModel? ModelInPopup { get; set; } |
| 79 | +
|
| 80 | + private async Task OnPopupShowButtonClick(GridCommandEventArgs args) |
| 81 | + { |
| 82 | + ModelInPopup = (SampleModel)args.Item; |
| 83 | +
|
| 84 | + PopoverRef?.Hide(); |
| 85 | + PopoverAnchorSelector = $"#button{ModelInPopup.Id}"; |
| 86 | + await Task.Delay(1); |
| 87 | +
|
| 88 | + PopoverRef?.Show(); |
| 89 | + } |
| 90 | +
|
| 91 | + protected override void OnInitialized() |
| 92 | + { |
| 93 | + var rnd = new Random(); |
| 94 | + for (int i = 1; i <= 7; i++) |
| 95 | + { |
| 96 | + GridData.Add(new SampleModel() |
| 97 | + { |
| 98 | + Id = i, |
| 99 | + Name = $"Name {i}", |
| 100 | + GroupName = $"Group {i % 3 + 1}", |
| 101 | + Price = rnd.Next(1, 100) * 1.23m, |
| 102 | + Quantity = rnd.Next(0, 1000), |
| 103 | + StartDate = DateTime.Now.AddDays(-rnd.Next(60, 1000)), |
| 104 | + IsActive = i % 4 > 0 |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + public class SampleModel |
| 109 | + { |
| 110 | + public int Id { get; set; } |
| 111 | + public int? ParentId { get; set; } |
| 112 | + public string Name { get; set; } = string.Empty; |
| 113 | + public string GroupName { get; set; } = string.Empty; |
| 114 | + public decimal Price { get; set; } |
| 115 | + public int Quantity { get; set; } |
| 116 | + public object Icon { get; set; } = SvgIcon.File; |
| 117 | + public DateTime StartDate { get; set; } |
| 118 | + public DateTime EndDate { get; set; } |
| 119 | + public bool IsActive { get; set; } |
| 120 | + } |
| 121 | +} |
| 122 | +```` |
| 123 | + |
| 124 | +## See Also |
| 125 | + |
| 126 | +* [Grid Overview Documentation](slug:grid-overview) |
| 127 | +* [Popover Overview Documentation](slug:popover-overview) |
0 commit comments