|
| 1 | +--- |
| 2 | +title: Delete Confirmation |
| 3 | +page_title: TreeList - Delete Confirmation Dialog |
| 4 | +description: Enable delete confirmation dialog in TreeList for Blazor. |
| 5 | +slug: treelist-delete-confirmation |
| 6 | +tags: telerik,blazor,treelist,delete,confirmation,dialog |
| 7 | +published: True |
| 8 | +position: 3 |
| 9 | +--- |
| 10 | + |
| 11 | +# Delete Confirmation Dialog |
| 12 | +The delete confirmation dialog triggers before item deletion. You can enable it by setting the `ConfirmDelete` parameter of the TreeList to `true`. |
| 13 | + |
| 14 | +>caption Delete Confirmation dialog |
| 15 | +
|
| 16 | + |
| 17 | + |
| 18 | +>caption Enabling of the Delete Confirmation Dialog |
| 19 | +
|
| 20 | +````CSHTML |
| 21 | +@* TreeList with enabled Delete Confirmation Dialog *@ |
| 22 | +
|
| 23 | +@using System.ComponentModel.DataAnnotations |
| 24 | +
|
| 25 | +<TelerikTreeList Data="@Data" |
| 26 | + OnDelete="@DeleteItem" |
| 27 | + Pageable="true" Height="400px" |
| 28 | + ConfirmDelete="true"> |
| 29 | + <TreeListColumns> |
| 30 | + <TreeListCommandColumn> |
| 31 | + <TreeListCommandButton Command="Delete" Icon="delete">Delete</TreeListCommandButton> |
| 32 | + </TreeListCommandColumn> |
| 33 | + <TreeListColumn Field="Name" Expandable="true" /> |
| 34 | + <TreeListColumn Field="Id" /> |
| 35 | + <TreeListColumn Field="EmailAddress" /> |
| 36 | + <TreeListColumn Field="HireDate" /> |
| 37 | + </TreeListColumns> |
| 38 | +</TelerikTreeList> |
| 39 | +
|
| 40 | +@code { |
| 41 | + public List<Employee> Data { get; set; } |
| 42 | +
|
| 43 | + async Task DeleteItem(TreeListCommandEventArgs args) |
| 44 | + { |
| 45 | + var item = args.Item as Employee; |
| 46 | +
|
| 47 | + await MyService.Delete(item); |
| 48 | +
|
| 49 | + await GetTreeListData(); |
| 50 | + } |
| 51 | +
|
| 52 | + public class Employee |
| 53 | + { |
| 54 | + public int Id { get; set; } |
| 55 | +
|
| 56 | + [Required] |
| 57 | + public string Name { get; set; } |
| 58 | + public string EmailAddress { get; set; } |
| 59 | + public DateTime HireDate { get; set; } |
| 60 | +
|
| 61 | + public List<Employee> DirectReports { get; set; } |
| 62 | + public bool HasChildren { get; set; } |
| 63 | +
|
| 64 | + public override bool Equals(object obj) |
| 65 | + { |
| 66 | + if (obj is Employee) |
| 67 | + { |
| 68 | + return this.Id == (obj as Employee).Id; |
| 69 | + } |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | +
|
| 74 | + async Task GetTreeListData() |
| 75 | + { |
| 76 | + Data = await MyService.Read(); |
| 77 | + } |
| 78 | +
|
| 79 | + protected override async Task OnInitializedAsync() |
| 80 | + { |
| 81 | + await GetTreeListData(); |
| 82 | + } |
| 83 | +
|
| 84 | + public static class MyService |
| 85 | + { |
| 86 | + private static List<Employee> _data { get; set; } = new List<Employee>(); |
| 87 | + private static int LastId { get; set; } = 1; |
| 88 | +
|
| 89 | + public static async Task<List<Employee>> Read() |
| 90 | + { |
| 91 | + if (_data.Count < 1) |
| 92 | + { |
| 93 | + for (int i = 1; i < 15; i++) |
| 94 | + { |
| 95 | + Employee root = new Employee |
| 96 | + { |
| 97 | + Id = LastId, |
| 98 | + Name = $"root: {i}", |
| 99 | + EmailAddress = $"{i}@example.com", |
| 100 | + HireDate = DateTime.Now.AddYears(-i), |
| 101 | + DirectReports = new List<Employee>(), |
| 102 | + HasChildren = true |
| 103 | + }; |
| 104 | + _data.Add(root); |
| 105 | + LastId++; |
| 106 | +
|
| 107 | + for (int j = 1; j < 4; j++) |
| 108 | + { |
| 109 | + int currId = LastId; |
| 110 | + Employee firstLevelChild = new Employee |
| 111 | + { |
| 112 | + Id = currId, |
| 113 | + Name = $"first level child {j} of {i}", |
| 114 | + EmailAddress = $"{currId}@example.com", |
| 115 | + HireDate = DateTime.Now.AddDays(-currId), |
| 116 | + DirectReports = new List<Employee>(), |
| 117 | + HasChildren = true |
| 118 | + }; |
| 119 | + root.DirectReports.Add(firstLevelChild); |
| 120 | + LastId++; |
| 121 | +
|
| 122 | + for (int k = 1; k < 3; k++) |
| 123 | + { |
| 124 | + int nestedId = LastId; |
| 125 | + firstLevelChild.DirectReports.Add(new Employee |
| 126 | + { |
| 127 | + Id = LastId, |
| 128 | + Name = $"second level child {k} of {j} and {i}", |
| 129 | + EmailAddress = $"{nestedId}@example.com", |
| 130 | + HireDate = DateTime.Now.AddMinutes(-nestedId) |
| 131 | + }); ; |
| 132 | + LastId++; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +
|
| 138 | + return await Task.FromResult(_data); |
| 139 | + } |
| 140 | +
|
| 141 | + public static async Task Delete(Employee itemToDelete) |
| 142 | + { |
| 143 | + RemoveChildRecursive(_data, itemToDelete); |
| 144 | + } |
| 145 | +
|
| 146 | + static void RemoveChildRecursive(List<Employee> items, Employee item) |
| 147 | + { |
| 148 | + for (int i = 0; i < items.Count(); i++) |
| 149 | + { |
| 150 | + if (item.Equals(items[i])) |
| 151 | + { |
| 152 | + items.Remove(item); |
| 153 | +
|
| 154 | + return; |
| 155 | + } |
| 156 | + else if (items[i].DirectReports?.Count > 0) |
| 157 | + { |
| 158 | + RemoveChildRecursive(items[i].DirectReports, item); |
| 159 | +
|
| 160 | + if (items[i].DirectReports.Count == 0) |
| 161 | + { |
| 162 | + items[i].HasChildren = false; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | +```` |
| 170 | + |
| 171 | + |
| 172 | +## See Also |
| 173 | + |
| 174 | +* [Live Demo: TreeList Inline Editing](https://demos.telerik.com/blazor-ui/treelist/editing-inline) |
| 175 | +* [Live Demo: TreeList PopUp Editing](https://demos.telerik.com/blazor-ui/treelist/editing-popup) |
| 176 | +* [Live Demo: TreeList InCell Editing](https://demos.telerik.com/blazor-ui/treelist/editing-incell) |
| 177 | +* [Live Demo: TreeList Custom Editor Template](https://demos.telerik.com/blazor-ui/treelist/custom-editor) |
| 178 | +* [Live Demo: TreeList Custom Edit Form](https://demos.telerik.com/blazor-ui/treelist/editing-custom-form) |
| 179 | + |
0 commit comments