[英]Pagination/global search in bootstrap table is not working properly in Blazor
I'm using bootstrap-table in my application to achieve pagination, global search, etc. It is a single-page application that uses Blazor framework(.net 5)<\/strong> .我在我的应用程序中使用 bootstrap-table 来实现分页、全局搜索等。它是一个使用Blazor framework(.net 5)<\/strong>的单页应用程序。 The problem is when I run the app, it immediately shows a bootstrap table with pagination, search but then it disappears and turns to a regular bootstrap table.
问题是当我运行应用程序时,它立即显示一个带有分页、搜索的引导表,但随后它消失并变成了一个常规的引导表。
What I tried:我尝试了什么:
My Index.razor我的索引.razor
@page "/"
@inherits IndexBase
<div class="container mt-3 mb-3">
<table class="table table-hover" id="tblCustomers"
data-toggle="table"
data-pagination="true"
data-search="true"
data-search-align="left"
data-show-refersh="true"
data-detail-view="true"
data-detail-formatter="detailFormatter">
<thead>
<tr>
<th data-field="Id" data-width="18" data-align="center" data-sortable="true">Cust Id</th>
<th data-field="Name" data-width="18" data-align="center" data-sortable="true">Cust Name</th>
<th data-field="GId" data-width="18" data-align="center" data-sortable="true">G Id</th>
<th data-field="AccNo" data-width="18" data-align="center" data-sortable="true">Acc No</th>
<th data-field="Active" data-width="18" data-align="center" data-sortable="true">Active</th>
<th data-field="Key" data-width="18" data-align="center" data-sortable="true">Key</th>
<th data-field="action" data-align="center">Action</th>
</tr>
</thead>
<tbody>
@if (Customers == null)
{
<tr rowspan="10">
<td colspan="8" style="text-align:center"> <div class="spinner">
<img src="/images/loader.gif" height="60" width="40"/>
</div>
</td>
</tr>
}
else
@foreach (var cust in Customers)
{
<tr>
<td>@cust.CustomerId</td>
<td>@cust.CustomerName</td>
<td>@cust.GId</td>
<td>@cust.AccNo</td>
<td>@cust.IsActive</td>
<td>Not available</td>
<td>
<a href="" class="edit" style="text-decoration:none;" title="edit" @onclick="(()=>OnEdit(cust.CustomerId))" @onclick:preventDefault> <i class="fa fa-edit fa-lg fa-charade"></i></a>
<a href="" class="remove" style="text-decoration:none;" title="remove" @onclick="() =>OnDelete(cust)" @onclick:preventDefault> <i class="fa fa-trash fa-lg fa-charade"></i></a>
</td>
</tr>
}
</tbody>
</table>
</div>
I had the same problem with bootstrap-table in a Blazor app.我在 Blazor 应用程序中遇到了与 bootstrap-table 相同的问题。 I think I got a solution.
我想我有一个解决办法。
For some reason, Blazor loads js scripts at the beginning and it seems bootstrap-table is loading correctly.出于某种原因,Blazor 在开始时加载 js 脚本,并且似乎 bootstrap-table 加载正确。 But after loading, the table you want to apply bootstrap, stays in a raw style and state.
但加载后,您要应用引导程序的表保持原始样式和状态。 I don't really know why, I guess it's a kind of Blazor behavior.
我真的不知道为什么,我想这是一种 Blazor 行为。 (I think we should learn exactly how blazor works to know the real reason).
(我认为我们应该确切地了解 blazor 是如何工作的,才能知道真正的原因)。
Anyway, few weeks ago I applied a pdf viewer tool and I follow the installing instructions.无论如何,几周前我应用了一个 pdf 查看器工具,并按照安装说明进行操作。 It was the same that other tools, call some js and css files... But as I was following the Blazor installing steps, I noticed that they do a kind of JS running function after render, not just importing files.
其他工具也是一样,调用一些js和css文件...但是当我按照Blazor安装步骤进行时,我注意到它们在渲染后执行了一种JS运行功能,而不仅仅是导入文件。
So, after this explanation I could make bootstrap-table work doing the following.所以,在这个解释之后,我可以让 bootstrap-table 工作做以下事情。
///CSS FILES
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="bootstrap-icons/font/bootstrap-icons.css" />
<link rel="stylesheet" href="bootstrap-table/bootstrap-table.min.css" />
///JS FILES
<script src="jquery/jquery.min.js"></script>
<script src="bootstrap-table/bootstrap-table.min.js"></script>
<script src="js/bootstrapTableScript.js"></script>
window.bootstrapTableFunction = {
initBootstrapTable: function () {
$('[data-toggle="table"]').bootstrapTable();
}
};
@inject IJSRuntime JSRuntime
<table data-toggle="table" data-search="true">
<thead>
<tr>
<th>Item ID</th>
<th>Item Name</th>
<th>Item Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Item 1</td>
<td>$1</td>
</tr>
<tr>
<td>2</td>
<td>Item 2</td>
<td>$2</td>
</tr>
</tbody>
</table>
protected override async void OnAfterRender(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeAsync<object>("bootstrapTableFunction.initBootstrapTable");
}
}
So, this is how it works... That OnAfterRender event is going to be ran when the razor page is already rendered.所以,这就是它的工作原理...... OnAfterRender 事件将在剃刀页面已经呈现时运行。 At that time, we are going to call an object called bootstrapTableFunction (the one is in your bootstrapTableScript file... Step 2 and 3).
那时,我们将调用一个名为 bootstrapTableFunction 的对象(该对象在您的 bootstrapTableScript 文件中……第 2 步和第 3 步)。 That object has a function called initBootstrapTable that is going to initialize the code that transform a simple table into a bootstrap-table.
该对象有一个名为 initBootstrapTable 的函数,它将初始化将简单表转换为引导表的代码。 And that's it!
就是这样! You have your bootstrap-table.
你有你的引导表。
This table example has the search function applied.此表示例应用了搜索功能。 So, if you want other functions like sort or so, you have to add data-sort and that kind of suff in html table, or inside bootstrapTableScript.js if you want it by javascript.
因此,如果您想要其他功能,如排序等,您必须在 html 表中添加数据排序和类似的功能,或者如果您想要通过 javascript 在 bootstrapTableScript.js 中添加。 Just like the official bootstrap-table documentation says.
就像官方的引导表文档说的那样。
Hope this be useful!希望这有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.