简体   繁体   English

引导表中的分页/全局搜索在 Blazor 中无法正常工作

[英]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 工作做以下事情。

  1. Import css and js files as usual into _Host.cshtml file (Or the file you're using as main file).像往常一样将 css 和 js 文件导入 _Host.cshtml 文件(或您用作主文件的文件)。 I installed bootstrap-table files into my project but I think it could work with CDN.我将引导表文件安装到我的项目中,但我认为它可以与 CDN 一起使用。 Of course, JS files just before close the body tag.当然,JS 文件只是在关闭 body 标记之前。
    ///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>
  1. Create a js custom file (This is going to contain the code to initialize the bootstrap-table once page is rendered).创建一个 js 自定义文件(这将包含在呈现页面后初始化引导表的代码)。 And add it after bootstrap-table.min.js reference.并在 bootstrap-table.min.js 参考后添加。
    <script src="js/bootstrapTableScript.js"></script>
  1. Write next code inside that file.在该文件中编写下一个代码。 (bootstrapTableScript.js file) (bootstrapTableScript.js 文件)
    window.bootstrapTableFunction = {
        initBootstrapTable: function () {
             $('[data-toggle="table"]').bootstrapTable();
        }
    };
  1. Go to the file you want to insert the table and inject IJSRuntime interface, so you could call JS functions in Runtime转到要插入表格的文件并注入 IJSRuntime 接口,这样就可以在 Runtime 中调用 JS 函数
    @inject IJSRuntime JSRuntime
  1. In the same razor file, insert the html table code.在同一个 razor 文件中,插入 html 表格代码。 I used the bootstrap page sample table我使用了引导页面示例表
    <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>
  1. Write this code in the same razor file (where the table is) but inside code section.将此代码写入同一剃须刀文件(表所在的位置)但在代码部分中。
    protected override async void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            await JSRuntime.InvokeAsync<object>("bootstrapTableFunction.initBootstrapTable");
        }
    }
  1. Try it!试试看! It should work This is how you should see它应该工作这就是你应该看到的

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM