bootstrap-paginator分页示例 源码 MVC

准备

1.数据:bootstrap包(含分页插件bootstrap-paginator.js)

 

2.技术方案:ajax动态加载分页、部分视图、BLL取数

代码

模板页

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    <script src="~/Content/Scripts/jquery/jquery-2.1.1.min.js"></script>
    <link href="~/Content/Scripts/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" />
    <script src="~/Content/Scripts/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
    <script src="~/Content/Scripts/bootstrap-3.3.7-dist/js/bootstrap-paginator.js"></script>
    @RenderSection("scripts")
</head>
<body>
    <div class="container" style="width:auto;margin:0 0px;">
        @RenderBody()
    </div>
</body>
</html>

  

主页

@using LeaRun.Entity;
@{
    ViewBag.Title = "View1";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts{
    <script type="text/javascript">
        var limit = 20;
        function initTable() {
            $.ajax({
                url: '../SystemSetup/LoadData',
                type: 'post',
                data: { page: 1, limit: limit },
                dataType: 'html',
                success: function (data) {
                    $("#data_table").html(data);
                    var pageCount = $('#datatotle').val(); //总页数
                    var options = {
                        bootstrapMajorVersion: 3, //版本
                        currentPage: 1, //当前页数
                        totalPages: pageCount, //总页数
                        numberOfPages: 5,
                        itemTexts: function (type, page, current) {
                            switch (type) {
                                case "first":
                                    return "首页";
                                case "prev":
                                    return "上一页";
                                case "next":
                                    return "下一页";
                                case "last":
                                    return "末页";
                                case "page":
                                    return page;
                            }
                        },//点击事件,用于通过Ajax来刷新整个list列表
                        onPageClicked: function (event, originalEvent, type, page) {
                            $.ajax({
                                url: '../SystemSetup/LoadData',
                                type: 'post',
                                data: { page: page, limit: limit },
                                dataType: 'html',
                                success: function (data) {
                                    $("#data_table").html(data);
                                }
                            });
                        }
                    };
                    $('#pageLimit').bootstrapPaginator(options);
                }
            });
            
        }

        $(function () {
            initTable();
        });
    </script>
}
<div class="row clearfix">
    <div class="col-md-12 column">
        <table class="table">
            <thead>
                <tr>
                    <th>
                        编号
                    </th>
                    <th>
                        名称
                    </th>
                    <th>
                        菜单
                    </th>
                    <th>
                        等级
                    </th>
                    <th>
                        启用
                    </th>
                    <th>
                        创建时间
                    </th>
                </tr>
            </thead>
            <tbody id="data_table">
            </tbody>
        </table>
        <div class="col-md-12 column text-center">
            <ul id="pageLimit"></ul>
        </div>
    </div>
</div>

分页

@using LeaRun.Entity;
@{
    #region
    List<Base_Module>
    data = ViewBag.Data as List<Base_Module>;
    if (data == null)
    {
        data = new List<Base_Module>();
    }
    int btotel = ViewBag.BTotel;
    #endregion
}
<input id="datatotle" type="text" hidden="hidden" value="@ViewBag.Totle"/>
@for (int i = 0; i < data.Count; i++)
{
    <tr class="@(i%2==0?"active":"")">
        <td>
            @(btotel++)
        </td>
        <td>
            @data[i].FullName
        </td>
        <td>
            @data[i].Location
        </td>
        <td>
            @data[i].Level
        </td>
        <td>
            @(data[i].Enabled == 1 ? "启用" : "未启用")
        </td>
        <td>
            @(Convert.ToDateTime(data[i].CreateDate).ToString("yyyy-MM-dd"))
        </td>
    </tr>
}

Controller

using LeaRun.Business;
using LeaRun.Business.BaseUtility;
using LeaRun.Entity;
using LeaRun.Utilities;
using System.Collections.Generic;
using System.Web.Mvc;

namespace LeaRun.WebApp.Controllers
{
    public class SystemSetupController : Controller
    {
        public Base_ModuleBll base_modulebll = new Base_ModuleBll();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LoadData(int page, int limit)
        {
            int total = 0;
            List<Base_Module> list = base_modulebll.GetList(out total, page: page, rows: limit);
            ViewBag.Data = list;
            ViewBag.Totle = total;
            ViewBag.BTotel = (page - 1) * limit + 1;
            return PartialView("LoadData");
        }
    }
}

效果

 

转载于:https://www.cnblogs.com/blogs2014/p/8421600.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.org.controller; import java.io.PrintWriter; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.org.BaseController; import com.org.model.User; import com.org.service.IUserService; /** * @Author:liangjilong * @Date:2014-2-25 * @Version:1.0 * @Description: */ @Controller public class UserController extends BaseController { @Resource private IUserService userService; /*** * 方法一请求使用String * * 请求@RequestMapping匹配的URL request * * @param response * @return * @throws Exception */ @RequestMapping(value = "/userList1.do") public String userList1(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<User> lists = userService.getUserList(); if (lists != null) { request.setAttribute("userList", lists); } // userList指跳转到userList.jsp页面 return "userList"; } /** * 方法二请求使用ModelAndView * * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = "/userList2.do") public ModelAndView userList2(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<User> lists = userService.getUserList(); if (lists != null) { request.setAttribute("userList", lists); } // userList指跳转到userList.jsp页面 return new ModelAndView("userList"); } /*** * 自定义标签实现分页 * * @param request * @param response * @param @ResponseBody ajax响应 * @param method={RequestMethod.POST,RequestMethod.GET}表单请求 * @param consumes="application/json; charset=UTF-8"请求格式是json * @return * @throws UnsupportedEncodingException * @throws Exception */ @RequestMapping(value = "/pageList.do" ,method={RequestMethod.POST,RequestMethod.GET}) public @ResponseBody ModelAndView getUserInfo(Model model, @RequestParam(required = false) String username, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize) { try { String userName = new String(username.getBytes("ISO-8859-1"),"UTF-8");//处理乱码 Map<String, Object> map = new HashMap<String, Object>(); username=(username==null)?"":username; map.put("username", username);//username必须要和ibatis配置的property=username一致 Integer totalCount = this.userService.getUserCount(map); this.initPage(map, pageNum, pageSize, totalCount); List list = this.userService.getUserLists(map); this.initResult(model, list, map); return new ModelAndView("pagerList"); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 添加用户 * @param type * @param model * @return */ @RequestMapping("/addUser.do") public ModelAndView addUser(@RequestParam String username, Model model) { User user = new User(); user.setUsername(username); this.userService.addUser(user); return this.getUserInfo(model, null, null, null); } /*** * 删除用户 * @param id * @param pageNum * @param pageSize * @param model * @return */ @RequestMapping(value="/delUser.do",method={RequestMethod.POST,RequestMethod.GET},consumes="application/json; charset=UTF-8") @ResponseBody public ModelAndView delUser(@RequestParam(required = true) Integer id, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, Model model, HttpServletRequest request,HttpServletResponse response) { PrintWriter out=null; JSONObject result=new JSONObject(); try { out=response.getWriter(); this.userService.delUserById(id); result.put("flag", true); out.write(result.toString()); } catch (Exception e) { try { result.put("flag", false); out.write(result.toString()); } catch (JSONException e1) { e1.printStackTrace(); } } return null; //return this.getUserInfo(model, null, pageNum, pageSize); } /*** * 编辑用户 * @param id * @param model * @return */ @RequestMapping("/getUserById.do") public ModelAndView getUserById(@RequestParam(required = true) Integer id, Model model) { User u = this.userService.getUserById(id); model.addAttribute("user", u); return new ModelAndView("editUser"); } /*** * 编辑用户 * @param id * @param model * @return */ @RequestMapping("/editUser.do") public ModelAndView editUser(@RequestParam(required = true) Integer id, @RequestParam String username, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, Model model) { User u = new User(); u.setUsername(username); this.userService.editUser(u); return this.getUserInfo(model, null, pageNum, pageNum); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值