From f80e40d1bfab5437f8b021a6ab40973217a6aeb6 Mon Sep 17 00:00:00 2001 From: James Yeung Date: Sun, 18 Jul 2021 13:04:25 +0800 Subject: [PATCH] fix(module: table): edit row demo can't recovery the editing on cancel (#1745) --- .../Demos/Components/Table/demo/EditRow.razor | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/site/AntDesign.Docs/Demos/Components/Table/demo/EditRow.razor b/site/AntDesign.Docs/Demos/Components/Table/demo/EditRow.razor index f63797ef..cffa1bc4 100644 --- a/site/AntDesign.Docs/Demos/Components/Table/demo/EditRow.razor +++ b/site/AntDesign.Docs/Demos/Components/Table/demo/EditRow.razor @@ -39,9 +39,9 @@ { Save + OnConfirm="() => cancelEdit(data.Id)" + OkText="Yes" + CancelText="No"> Cancel } @@ -57,7 +57,7 @@ @code{ - class ItemData + record ItemData { public string Id { get; set; } public string Name { get; set; } @@ -66,7 +66,7 @@ }; IDictionary editCache = new Dictionary(); - ItemData[] listOfData = { }; + List listOfData = new(); int i = 0; string editId; @@ -74,33 +74,28 @@ void startEdit(string id) { var data = editCache[id]; - data.edit = true; - editCache[id] = data; + editCache[id] = (true, data.data with { }); // add a copy in cache } void cancelEdit(string id) { var data = listOfData.FirstOrDefault(item => item.Id == id); - editCache[id] = new(false, data); + editCache[id] = (false, data); // recovery } void saveEdit(string id) { - var data = listOfData.FirstOrDefault(item => item.Id == id); - editCache[id] = new(false, data); - } - - void updateEditCache() - { - listOfData.ForEach(item => - { - editCache[item.Id] = new(false, item); - }); + var index = listOfData.FindIndex(item => item.Id == id); + listOfData[index] = editCache[id].data; // apply the copy to data source + editCache[id] = (false, listOfData[index]); // don't affect rows in editing } protected override void OnInitialized() { - listOfData = Enumerable.Range(0, 100).Select(i => new ItemData { Id = $"{i}", Name = $"Edrward {i}", Age = 32, Address = $"London Park no. {i}" }).ToArray(); - updateEditCache(); + listOfData = Enumerable.Range(0, 100).Select(i => new ItemData { Id = $"{i}", Name = $"Edrward {i}", Age = 32, Address = $"London Park no. {i}" }).ToList(); + listOfData.ForEach(item => + { + editCache[item.Id] = (false, item); + }); } } \ No newline at end of file