c# - 使用 Blazor 和 C# 刷新 html 表数据
问题描述
我有一种情况,我有一个 for 循环,它从我的数据模型中创建我的 html 表,该表从 SQL server express 获取数据。我想知道是否可以创建一个自动刷新方法,其中表数据只刷新而不是整个页面,如果没有,那么可能是 OnClick 按钮将从数据模型中检索最新数据并相应地更新表的方法。
我是 blazor 和 C# 的新手,因此将不胜感激,我当前的页面结构目前如下所示:
@page "/employees"
@using DataLib;
@inject IEmployeeData _db
@if (employees is null)
{
<p style="color:white;"><em>Loading . . .</em></p>
}
else
{
<table class="table" id="myTable">
<thead>
<tr>
<th>Entry Date</th>
<th>Employee</th>
</tr>
</thead>
<tbody>
@foreach (var employee in employees)
{
<tr>
<td>@employee.EntryDate</td>
<td>@employee.POI</td>
</tr>
}
</tbody>
</table>
}
@code{
private List<EmployeeModel> employees;
protected override async Task OnInitializedAsync()
{
employees = await _db.GetEmployee();
}
}
当我加载此页面和手动刷新时,上述内容非常完美。
有什么方法可以帮助我吗?
谢谢。
解决方案
不确定这是您可以尝试的目标对接;
@inject IEmployeeData _db
@if (employees is null)
{
<p style="color:white;"><em>Loading . . .</em></p>
}
else
{
<table class="table" id="myTable">
<thead>
<tr>
<th>Entry Date</th>
<th>Employee</th>
</tr>
</thead>
<tbody>
@foreach (var employee in employees)
{
<tr>
<td>@employee.EntryDate</td>
<td>@employee.POI</td>
</tr>
}
</tbody>
</table>
<button @onclick="GetEmployees"> Refresh Employee List</button>
}
@code{
private List<EmployeeModel> employees;
protected override async Task OnInitializedAsync()
{
GetEmployees()
}
private async void GetEmployees()
{
employees.Clear();
employees = await _db.GetEmployee();
StateHasChanged();
}
祝你好运,
推荐阅读
- apache-spark - Spark & PySpark:表级访问控制?
- filter - 有没有办法使用 solr 文本标记器和 n-edge-gram 过滤器
- javascript - 如何修复正在声明但从未读取其值的 Javascript 滑块按钮功能?
- google-json-api - 如何从 Google JSON API 获得准确的结果,就像真正的 Google 搜索一样
- git - Composer 需要自定义包缓存问题
- unit-testing - 使用 `vue-test-utils` 和 `jest` 使用 `Updated` 钩子进行测试
- node.js - 使用 NodeJS 和 Cheerio 抓取需要太多时间
- java - WorkManager 在设备被锁定时多次调用 doWork() 方法
- dynamic - AWS 负载均衡器需要 CNAME 作为 DNS 指针。www.domain 被覆盖,但根域不被 SSL 证书覆盖
- shopware - Shopware 6:如何在产品详细信息页面中获取插件配置值