fix(module: table): edit row demo can't recovery the editing on cancel (#1745)

This commit is contained in:
James Yeung 2021-07-18 13:04:25 +08:00 committed by GitHub
parent 1758181dea
commit f80e40d1bf

View File

@ -39,9 +39,9 @@
{ {
<a @onclick="() => saveEdit(data.Id)" class="save">Save</a> <a @onclick="() => saveEdit(data.Id)" class="save">Save</a>
<Popconfirm Title="Sure to cancel?" <Popconfirm Title="Sure to cancel?"
OnConfirm="() => cancelEdit(data.Id)" OnConfirm="() => cancelEdit(data.Id)"
OkText="Yes" OkText="Yes"
CancelText="No"> CancelText="No">
<a>Cancel</a> <a>Cancel</a>
</Popconfirm> </Popconfirm>
} }
@ -57,7 +57,7 @@
@code{ @code{
class ItemData record ItemData
{ {
public string Id { get; set; } public string Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
@ -66,7 +66,7 @@
}; };
IDictionary<string, (bool edit, ItemData data)> editCache = new Dictionary<string, (bool edit, ItemData data)>(); IDictionary<string, (bool edit, ItemData data)> editCache = new Dictionary<string, (bool edit, ItemData data)>();
ItemData[] listOfData = { }; List<ItemData> listOfData = new();
int i = 0; int i = 0;
string editId; string editId;
@ -74,33 +74,28 @@
void startEdit(string id) void startEdit(string id)
{ {
var data = editCache[id]; var data = editCache[id];
data.edit = true; editCache[id] = (true, data.data with { }); // add a copy in cache
editCache[id] = data;
} }
void cancelEdit(string id) void cancelEdit(string id)
{ {
var data = listOfData.FirstOrDefault(item => item.Id == id); var data = listOfData.FirstOrDefault(item => item.Id == id);
editCache[id] = new(false, data); editCache[id] = (false, data); // recovery
} }
void saveEdit(string id) void saveEdit(string id)
{ {
var data = listOfData.FirstOrDefault(item => item.Id == id); var index = listOfData.FindIndex(item => item.Id == id);
editCache[id] = new(false, data); listOfData[index] = editCache[id].data; // apply the copy to data source
} editCache[id] = (false, listOfData[index]); // don't affect rows in editing
void updateEditCache()
{
listOfData.ForEach(item =>
{
editCache[item.Id] = new(false, item);
});
} }
protected override void OnInitialized() 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(); listOfData = Enumerable.Range(0, 100).Select(i => new ItemData { Id = $"{i}", Name = $"Edrward {i}", Age = 32, Address = $"London Park no. {i}" }).ToList();
updateEditCache(); listOfData.ForEach(item =>
{
editCache[item.Id] = (false, item);
});
} }
} }