我正尝试在服务器端Blazor应用程序中使用datatables.net,希望能对您有所帮助。
我已经尝试过https://datatables.net/forums/discussion/56389/datatables-with-blazor中途提到的代码,但是,我遇到的问题是,当我在页面之间浏览时,某些UI元素(例如页面调度)正在被复制。
下面是我的_Host.cshtml文件
@page "/"
@namespace MyApplication.Web.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MyApplication.Web</title>
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/datatables/js/jquery.dataTables.min.js"></script>
<link href="/lib/datatables/css/jquery.dataTables.min.css" rel="stylesheet" />
</head>
<body>
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">?</a>
</div>
<script>
function AddDataTable(table) {
$(document).ready(function () {
$(table).DataTable({
"searching": false
});
});
}
</script>
<script>
function RemoveDataTable(table) {
$(document).ready(function () {
$(table).DataTable().destroy();
});
}
</script>
<script src="_framework/blazor.server.js"></script>
</body>
</html>
下面是剃刀组件中的代码
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MyApplication.Shared.Entities;
using MyApplication.Web.Interfaces;
namespace MyApplication.Web.Pages.Admin
{
public partial class ListAdministrators
{
[Inject]
public IAdministratorDataService AdministratorDataService { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
public List<Administrator> Administrators { get; set; }
protected override async Task OnInitializedAsync()
{
Administrators = (await AdministratorDataService.GetAll()).ToList();
}
protected void NavigateToAddAdministrator()
{
NavigationManager.NavigateTo("/Admin/AdministratorEdit");
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await JSRuntime.InvokeAsync<object>("AddDataTable", "#dataTable");
}
}
}
要指出的一点是,在https://datatables.net/forums/discussion/56389/datatables-with-blazor上它们具有我在上面的代码中具有的RemoveDataTable函数,但是,我不确定如何在单击按钮之外调用该函数。
首次加载-一切正常
浏览到首页-仍在显示分页UI元素
答案 0 :(得分:3)
我似乎已经通过以下方法解决了重复的UI元素问题,但是我很想知道是否有更好的方法,实际上,我敢肯定一定有,也许这个问题应该是多么糟糕。下面的方法?
_Host.cshtml现在添加了以下内容
<script src="~/scripts/JSInterop.js"></script>
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/datatables/js/jquery.dataTables.min.js"></script>
<link href="/lib/datatables/css/jquery.dataTables.min.css" rel="stylesheet" />
创建了一个新的组件DataTable.razor
@inject Microsoft.JSInterop.IJSRuntime JSRuntime;
<div id="@id">
@ChildContent
</div>
@code
{
[Parameter]
public bool Searchable { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
private string id { get; set; } = "DataTable-" + Guid.NewGuid().ToString();
protected override void OnParametersSet()
{
StateHasChanged();
base.OnParametersSet();
}
protected async override Task OnAfterRenderAsync(bool firstRender)
{
await JSRuntime.InvokeAsync<string>("AddDataTable", new object[] { "#" + id + " table", Searchable });
await base.OnAfterRenderAsync(firstRender);
}
}
创建了一个简单的JS文件JSInterop.js
function AddDataTable(table, searching) {
$(table).DataTable({
"searching": searching
});
}
然后我在页面上创建数据表时使用以下代码
<DataTable Searchable="true">
<table class="table">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Access Level</th>
<th>Date Added</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var administrator in Administrators)
{
<tr>
<td>@administrator.Id</td>
<td>@administrator.LastName</td>
<td>@administrator.AccessLevel.GetDisplayName()</td>
<td>@administrator.DateCreated.ToShortDateString()</td>
<td>
<a href="@($"Admin/EditAdministrator/{administrator.Id}")" class="btn btn-primary table-btn">
Edit
</a>
</td>
</tr>
}
</tbody>
</table>
</DataTable>