【发布时间】:2020-04-18 05:29:06
【问题描述】:
我正在为我正在制作的网站制作表格组件。我希望能够在一个页面上创建、更新、添加和删除表中的条目。我还将在整个网站上使用这个组件,并使用多种类作为输入。我去了here 学习如何做到这一点。我还认为在表格中的每一行都有一个内部组件会很聪明。到目前为止,这是我的代码:
可编辑类.Razor
@using WbotV2.Data
@using System
@using System.Collections
@using System.Reflection
@typeparam TItem
<tr>
@foreach (var datum in Data)
{
<td>@datum</td>
}
<td><button class="btn btn-success">Edit</button></td>
<td>@ChildContent</td>
</tr>
@code {
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter]
public TItem Item { get; set; }
public List<string> Data;
public bool Editable = false;
protected override async Task OnInitializedAsync()
{
Data = GetData(Item);
}
public static List<string> GetData<T>(T instance)
{
List<string> ret = new List<string>();
FieldInfo[] infos = typeof(T).GetFields();
foreach (FieldInfo info in infos)
{
ret.Add(info.GetValue(instance).ToString());
}
return ret;
}
}
这个组件应该允许我将一个类作为参数传入,然后编辑它的字段。它也不应该将用户带到单独的页面来编辑它(如果可能的话,我希望将它内联在表格中)。我的想法是使用绑定来做到这一点,但类名是任意的,所以我不知道如何将它传递给@bind,这就是我卡住的地方。
表格组件.razor
@using WbotV2.Data
@using System
@using System.Collections
@using System.Reflection
@typeparam TItem
<table class="table">
<thead>
<tr>
@foreach (string header in Headers)
{
<th>@header</th>
}
<td>Edit</td>
<td>Delete</td>
<td><button class="btn btn-primary">Add</button></td>
</tr>
</thead>
<tbody>
@foreach (var item in Items)
{
<EditableClass Item="@item"><button class="btn btn-danger" @onclick="(Delete(item))">Delete</button></EditableClass>
}
</tbody>
</table>
@code {
[Parameter]
public IList<TItem> Items { get; set; }
[Parameter]
public TableUtilityClass<TItem> DataInstance { get; set; }
public string[] Headers;
protected override async Task OnInitializedAsync()
{
Headers = GetHeaders();
}
public string[] GetHeaders()
{
List<string> ret = new List<string>();
FieldInfo[] infos = typeof(TItem).GetFields();
foreach (FieldInfo info in infos)
{
ret.Add(info.Name);
}
return ret.ToArray();
}
public void Delete(TItem itemToDelete)
{
DataInstance.Delete(itemToDelete);
StateHasChanged();
Console.WriteLine("Here");
}
public void Add(TItem itemToAdd)
{
DataInstance.Add(itemToAdd);
StateHasChanged();
Console.WriteLine("Here");
}
public List<List<string>> GetDataString<T>(IEnumerable<T> instance)
{
List<List<string>> ret = new List<List<string>>();
foreach (T item in instance)
{
ret.Add(new List<string>());
FieldInfo[] infos = typeof(T).GetFields();
foreach (FieldInfo info in infos)
{
ret[ret.Count - 1].Add(info.GetValue(item).ToString());
Console.WriteLine($"{info.Name}: {info.GetValue(item)}");
}
}
return ret;
}
}
这个组件应该在一个表中有很多可编辑的类组件,并处理对它的新条目的添加和删除。我目前的问题是删除,我尝试实现this solution,但我在此行<EditableClass Item="@item"><button class="btn btn-danger" @onclick="(Delete(item))">Delete</button></EditableClass> 上遇到了无法从void 转换为Microsoft.AspNetCore.Components.EventCallback 错误我也尝试过this 和this .我也希望在这里得到一些帮助,解决错误或找到解决问题的巧妙方法。
Table Utility Class.cs
using System.Threading.Tasks;
namespace WbotV2.Data
{
public abstract class TableUtilityClass<T>
{
public abstract Task Delete(T Item);
public abstract Task Add(T Item);
}
}
这个类应该只是有助于轻松与许多不同类型的服务进行交互,因为我预计会有很多。
最后,这是我的 Country.razor(表格组件进入的页面)
@page "/countries"
@using WbotV2.Data
@inject WbotCountryService CountryService
<h1>Country Information</h1>
@if (countries == null)
{
<p><em>Loading...</em></p>
}
else
{
<TableComponent Items="@countries" DataInstance="@CountryService"/>
}
@code {
public List<WbotCountry> countries;
protected override async Task OnInitializedAsync()
{
countries = await CountryService.GetCountriesAsync();
}
}
对于那些想知道 Country 服务看起来像这样的人:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WbotV2.Data
{
public class WbotCountryService : TableUtilityClass<WbotCountry>
{
public WbotData WbotDataInstance;
public WbotCountryService()
{
WbotDataInstance = WbotData.LoadData();
}
public Task<List<WbotCountry>> GetCountriesAsync()
{
return Task.FromResult(WbotDataInstance.countries);
}
public override Task Delete(WbotCountry c)
{
WbotDataInstance.countries.Add(c);
WbotDataInstance.SaveData();
return Task.CompletedTask;
}
public override Task Add(WbotCountry c)
{
WbotDataInstance.countries.Remove(c);
WbotDataInstance.SaveData();
return Task.CompletedTask;
}
}
}
如有任何帮助,我们将不胜感激!
【问题讨论】:
-
显然
@onclick="(Delete(item))">是错误的,为什么@onclick="@( ()=>(Delete(item)))">不是您的解决方案?应该工作。 -
只是想说我尊重
foreach (var datum in data)声明