【问题标题】:How to Apply Pagination for Dynamically Created HTML Table in Asp.net MVC如何在 Asp.net MVC 中为动态创建的 HTML 表应用分页
【发布时间】:2014-08-05 21:10:11
【问题描述】:

我在 MVC 中有动态 HTML 表格,例如:

<table id="dt_basic" class="table dataTable">
      <thead style="text-align: left;">
            <tr>
                <th><i class="fa fa-lg fa-plus-circle"></i></th>
                <th style="text-align: left">Name</th>
                <th style="text-align: left">Resource Type</th>
                <th style="text-align: left">Week Start Date</th>
                <th style="text-align: left">Week End Date</th>
                <th style="text-align: left">Status</th>
            </tr>
     </thead>
     <tbody role="alert" aria-live="polite" aria-relevant="all">

          @foreach (var item in Model.GetContractorNotEnteredDetails)
              {
                <tr class="trList">
                   <td class="tdEmpId"><a class="text-success accordion-toggle ViewTimesheet" data-toggle="collapse" data-target="#demo1" id="view1" rel="@item.CONTR_ID"><i class="fa fa-lg fa-plus-circle"></i></a></td>
                   <td style="text-align: left">@Html.TextBoxFor(m => item.CONTRACTOR_NAME, new { @class = "project-input", @readonly = true, @style = "width:180px;text-align:left" })
                   <td style="text-align: left" class="tdResType">@Html.TextBoxFor(m => item.RES_TYPE, new { @class = "project-input", @readonly = true, @style = "text-align:left;" })</td>
                   <td style="text-align: left" class="tdStartDate">@Html.TextBoxFor(m => item.SDate, "{0:MM/dd/yyyy}", new { @class = "project-input startdate", @readonly = true })</td>
                   <td style="text-align: left" class="tdEndDate">@Html.TextBoxFor(m => item.EDate, "{0:MM/dd/yyyy}", new { @class = "project-input enddate", @readonly = true })</td>
                   <td style="text-align: left" class="tdStatus" title="@Html.DisplayFor(m => item.DESCR)"><span class="label-primary" style="color:white">&nbsp;&nbsp;@Html.DisplayFor(m => item.EMP_STATUS, new { @readonly = true, @style = "text-align:center;" })&nbsp;&nbsp;</span></td>

                   </tr>
       </tbody>
       <tfoot>
            <tr>
             <td> <div id="pager" class="pager" style="position: absolute; text-align: center;">
                  <img src="http://mottie.github.com/tablesorter/addons/pager/icons/first.png" class="first" />
                  <img src="http://mottie.github.com/tablesorter/addons/pager/icons/prev.png" class="prev" />
                  <span class="pagedisplay"></span>
                  <!-- this can be any element, including an input -->
                  <img rc="http://mottie.github.com/tablesorter/addons/pager/icons/next.png" class="next" />
                  <img src="http://mottie.github.com/tablesorter/addons/pager/icons/last.png" class="last" />
                 <select class="pagesize">
                   <option selected="selected" value="10">10</option>
                   <option value="20">20</option>
                   <option value="30">30</option>
                   <option value="40">40</option>
                   </select>
             </div>
          </td>
      </tr>
  </tfoot>
 </table>

如何在客户端使用 jQuery 或任何其他方式申请分页。 我尝试过使用 jQuery "tablesorterPager" 之类的

  <script src="~/Scripts/jquery-2.1.1.js"></script>
  <script src="~/Scripts/jquery.tablesorter.js"></script>
  <script src="~/Scripts/jquery.tablesorter.pager.js"></script>       

   <script>
     $(document).ready(function () {           
            $("#dt_basic")
            .tablesorter({ widthFixed: true, widgets: ['zebra'] })
            .tablesorterPager({ container: $("#pager") });
     });

   </script>

但它不起作用,是否有任何替代分页。 请任何人帮助我。

【问题讨论】:

  • 你能做到吗?我遇到了同样的问题。我必须为动态创建的 HTML 表格进行分页。

标签: jquery asp.net-mvc-4 pagination


【解决方案1】:

您可以使用'DataTables' jQuery 插件,它是开源的并且有良好的API 支持。

你需要像这样包含 jQuery 和 DataTable 插件:

<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>

<script type="text/javascript" src="jquery.dataTables.js"></script>

并在所需表上调用 dataTable :

$(document).ready(function () {           
            $("#dt_basic")
            .dataTable({
               "bPaginate": true,
               "bLengthChange": true,
               "bFilter": true});
      });

您可以使用 Datable 做更多的事情,例如过滤、排序、分页、搜索等。

DataTable 官方网站 - DataTable

【讨论】:

  • 感谢您的回复..!!由于一些问题和执行其他操作,我只需要使用 html 表。我曾尝试使用 jQuery "tableSorterPager" 但没有得到。
  • 这仅适用于 html 表格,在加载您的 html 后,它将应用分页、排序等。并且不会影响您的任何操作或功能。
  • 我只使用了html表格,但是表格行是动态创建的,是否适用于动态创建的表格??
  • 是的,它会起作用,在这里你是动态生成数据行而不是整个表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多