bootstrap-paginador 分页 + MVC

la página de arranque-paginador

  • representaciones

Preparación 1. Demostración

1.1. Entorno de programación

    - VS2019 

1.2. Preparación

  • Plugin de paginación (bootstrap-paginador) Descargas: https://github.com/lyonlai/bootstrap-paginator
  • Después de la descarga de encontrar los bootstrap-paginator-master\bootstrap-paginator-master\documentation\index.htmldetalles sobre el plug-in
  • Encontrar el paquete de los siguientes archivos y las referencias al proyecto
   <script src="~/Files/jquery-1.9.1.min.js"></script>
    <script src="~/Files/bootstrap-paginator.js"></script>
    <link href="~/Files/bootstrap.min.css" rel="stylesheet" />

2. Antecedentes

namespace MvcPagingDemo.Models
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Adress { get; set; }
        public string Email { get; set; }
        public string Tel { get; set; }
        
        public bool Sex { get; set; }
    }
}
  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPagingDemo.Models;
namespace MvcPagingDemo.Controllers
{
 public class HomeController : Controller
 {
     List<Student> stuList = new List<Student>
     {
         new Student{  Id=1, Name="张三", Sex=true, Adress="北京", Email="[email protected]", Tel="18877222723" },
         new Student{  Id=2, Name="李四", Sex=true, Adress="上海", Email="[email protected]", Tel="18877222723" },
         new Student{  Id=3, Name="王五", Sex=true, Adress="苏州", Email="[email protected]", Tel="18877222723" },
         new Student{  Id=4, Name="刘六", Sex=false , Adress="杭州", Email="[email protected]", Tel="18877222723" },
         new Student{  Id=5, Name="曹七", Sex=false , Adress="郑州", Email="[email protected]", Tel="18877222723" },
         new Student{  Id=6, Name="冯八", Sex=false , Adress="扬州", Email="[email protected]", Tel="18877222723" },
         new Student{  Id=7, Name="妲九", Sex=true, Adress="非洲", Email="[email protected]", Tel="18877222723" },
         new Student{  Id=8, Name="万十", Sex=true, Adress="广州", Email="[email protected]", Tel="18877222723" },
           new Student{  Id=9, Name="妲九", Sex=true, Adress="非洲", Email="[email protected]", Tel="18877222723" },
         new Student{  Id=10, Name="万十", Sex=true, Adress="广州", Email="[email protected]", Tel="18877222723" },
           new Student{  Id=11, Name="妲九", Sex=true, Adress="非洲", Email="[email protected]", Tel="18877222723" },
         new Student{  Id=12, Name="万十", Sex=true, Adress="广州", Email="[email protected]", Tel="18877222723" },
     }; 
     public ActionResult Index()
     { 
         return View();
     }
     /// <summary>
     /// 分页
     /// </summary>
     /// <param name="page">当前页</param>
     /// <param name="pageSize">每页显示数</param>
     /// <param name="total">查询数据的总行数</param>
     /// <param name="totalPages">总页数</param>
     /// <returns></returns>
     public ActionResult StuListShow(int page = 1, int pageSize = 3)
     {
         int total = 0;
         List<Student> list = stuList;
         total = list.Count;   //总数量
         ViewBag.totalPages = (int)Math.Ceiling((decimal)(total) / pageSize);//总页数 
         var targets = list.Skip(pageSize * (page - 1)).Take(pageSize);
         return PartialView(targets);
     }
}
}

3. El extremo delantero de la primera introducción js otros documentos en _Layout.cshtmlel

   <script src="~/Files/jquery-1.9.1.min.js"></script>
  <script src="~/Files/bootstrap-paginator.js"></script>
  <link href="~/Files/bootstrap.min.css" rel="stylesheet" />

4. Añadir el plug-in pestaña distal de configuración (bootstrap-Paginator)

  • En primer lugar div es una vista parcial, para mostrar datos
  • El segundo tapón div el portador de paginación, para la visualización de la pestaña de enchufe
 @{ 
      ViewBag.title="Index" ;
 }
<div id="data_table">
    @Html.Action("StuListShow")
</div>

<div id="example"></div> 

<script type='text/javascript'>
    var options = {
        currentPage: 1,                           //当前页 
        totalPages: $("#totalpage").val(),       //总页数 
        bootstrapMajorVersion: 2,               // bootstrap->版本2:必须要用div显示,版本3用ul
        size: "normal",                          //大小:large,normal,small,mini
        alignment: "center",                    //对齐方式
        itemTexts: function (type, page, current)//页面项目文字
        {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "尾页";
                case "page":
                    return  page;
            }
        },
        numberOfPages: 5,  //通过属性numberOfPages可控制最大页面数。此属性仅接受整数。 
        onPageClicked: function (e, originalEvent, type, page)
        {
            //   异步方式 1
            //$.post("/Home/StuListShow", { page: page, pageSize: 3 }, function (data) {
            //    $("#data_table").html(data);
            //});
              //   异步方式 2
            $.ajax({
                   url: '/Home/StuListShow',
                   type: 'post',
                   data: { page: page, pageSize: 3 },
                   dataType: 'html',
                   success: function (data){
                        $("#data_table").html(data);
                       }
                    });
        }
}
    $('#example').bootstrapPaginator(options);
</script>

5, una vista parcial de la parte delantera del código (StuListShow.cshtml)

@using MvcPagingDemo.Models
@model IEnumerable<MvcPagingDemo.Models.Student>

<table class="table table-hover table-bordered">
    <tr>
        <td>编号</td>
        <td>姓名</td>
        <td>电话</td>
        <td>邮箱</td>
        <td>地址</td> 
    </tr>
    @foreach (var stu in Model)
    {
        <tr>
            <td>@stu.Id</td>
            <td>@stu.Name</td>
            <td>@stu.Tel</td>
            <td>@stu.Email</td>
            <td>@stu.Adress</td> 
        </tr>
    }
</table> 
<input type="hidden" id="totalpage" value="@ViewBag.totalPages" />

Figura 6. Efecto

Supongo que te gusta

Origin www.cnblogs.com/zgsy/p/12667580.html
Recomendado
Clasificación