在 Blazor Web 程序集 html table 中动态添加和删除行
Dynamic row add & delete in html table in Blazor web assembly
我正在开发一个 blazor webassembly 应用程序,我在其中具有添加或删除 html 行的功能。我们可以在 Blazor 中轻松完成吗?或者我们必须去javascript?提前致谢
我正在寻找类似这样的输出或任何与我的要求类似的输出。对此类解决方案的任何 link 也应该有所帮助。只是示例图片:
是这样的吗?
<table style="width:100%">
<tr>
<th>Name</th>
<th>Value</th>
<th>Command</th>
</tr>
@foreach(var model in models)
{
<tr>
<td>@model.Name</td>
<td>@model.Value</td>
<td>
<button @onclick="() => models.Remove(model)">
X
</button>
</td>
</tr>
}
</table>
<button @onclick="@(() => models.Add(new Model(){Name = nameTextField, Value = Int32.Parse(valueTextField)}))">
New value
</button>
<div>
Name: <input @bind="@nameTextField" @oninput="(e)=> { nameTextField = e.Value ==null? string.Empty:(string)e.Value; }" />
</div>
<div>
Value: <input type="number" @bind="@valueTextField" @oninput="(e)=> { valueTextField = e.Value ==null? string.Empty:(string)e.Value; }" />
</div>
@code {
string nameTextField = "";
string valueTextField = "";
List<Model> models = new()
{
new Model(){Name="Row1",Value = 1},
new Model(){Name="Row2",Value = 2}
};
}
Model.cs:
public class Model
{
public string Name {get;set;}
public int Value {get;set;}
}
我正在开发一个 blazor webassembly 应用程序,我在其中具有添加或删除 html 行的功能。我们可以在 Blazor 中轻松完成吗?或者我们必须去javascript?提前致谢
我正在寻找类似这样的输出或任何与我的要求类似的输出。对此类解决方案的任何 link 也应该有所帮助。只是示例图片:
是这样的吗?
<table style="width:100%">
<tr>
<th>Name</th>
<th>Value</th>
<th>Command</th>
</tr>
@foreach(var model in models)
{
<tr>
<td>@model.Name</td>
<td>@model.Value</td>
<td>
<button @onclick="() => models.Remove(model)">
X
</button>
</td>
</tr>
}
</table>
<button @onclick="@(() => models.Add(new Model(){Name = nameTextField, Value = Int32.Parse(valueTextField)}))">
New value
</button>
<div>
Name: <input @bind="@nameTextField" @oninput="(e)=> { nameTextField = e.Value ==null? string.Empty:(string)e.Value; }" />
</div>
<div>
Value: <input type="number" @bind="@valueTextField" @oninput="(e)=> { valueTextField = e.Value ==null? string.Empty:(string)e.Value; }" />
</div>
@code {
string nameTextField = "";
string valueTextField = "";
List<Model> models = new()
{
new Model(){Name="Row1",Value = 1},
new Model(){Name="Row2",Value = 2}
};
}
Model.cs:
public class Model
{
public string Name {get;set;}
public int Value {get;set;}
}