BootstrapMVC+Bootstrap+Drapper用PagedList.Mvc支持多询问条件分页

前方几乎上举行一个小小小项目,使用了MVC+Bootstrap,以前做分页都是异步加载Mvc部分视图的方式,因为这是略项目,就不管一点。一般的列表页面,少不了有询问条件,下面分享下Drapper+PagedList.Mvc支持多询问条件分页的下更。

构建强类型Model

1.询问参数的Model

    public class OrderQueryParamModel
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderNo { get; set; }

        /// <summary>
        /// 客户名称
        /// </summary>
        public string CustomerName { get; set; }
    }

2.Orders分页数据Model

PagedList提供了一个StaticPagedList<T>泛型类来封装数据。(看看StaticPagedList的源代码,使用大便于,把T类型的多寡subset,pageNumber,pageSize,totalCount初始化进去就可以了。

 public StaticPagedList(IEnumerable<T> subset, IPagedList metaData) : this(subset, metaData.PageNumber, metaData.PageSize, metaData.TotalItemCount)
        {
        }

3.Orders著页面整体Model

    public class OrderViewModel
    {
        public OrderQueryParamModel QueryModel { get; set; }

        public PagedList.StaticPagedList<OrderModel> OrderList { get; set; } 
    }

OK,接下省在Controller中什么吃来OrderViewModel填充数据吧

        public ActionResult List(OrderViewModel orderViewModel, int page = 1)
        {
            var pagesize = 10;
            var count = 0;

            var orders = _orderService.GetOrders(page, pagesize, orderViewModel.QueryModel, ref count);

            orderViewModel.OrderList = new StaticPagedList<OrderModel>(orders, page, pagesize, count);

            return View(orderViewModel);
        }

Controller中代码很简短,接收POST过来的有限独参数,orderViewModel:包含查询参数Model,page:PagedList定义之时页。

顺手看看GetOrders()这个方式吧,为了便利懒得写存储过程,直接用了Drapper的QueryMultiple,感觉好有力啊。

        public List<OrderModel> GetOrders(int pageindex, int pagesize, OrderQueryParamModel query, ref int count)
        {
            var orders = new List<OrderModel>();
            var whereStr = string.Empty;

            if (query != null)
            {
                if (!string.IsNullOrEmpty(query.CustomerName))
                {
                    whereStr += string.Format(" and CustomerName like '%{0}%' ", query.CustomerName);
                }
            }

            var cmd = string.Format(@"SELECT COUNT(*) FROM [Orders] WHERE 1=1 {0};
                        SELECT *  FROM (
                        SELECT *, row_number() OVER (ORDER BY orderId DESC ) AS [row] 
                                  FROM [Orders] WHERE 1=1  {0} )t
                        WHERE t.row >@indexMin AND t.row<=@indexMax", whereStr);

            using (IDbConnection conn = BaseDBHelper.GetConn())
            {
                using (var multi = conn.QueryMultiple(cmd, 
                   new { indexMin = (pageindex - 1) * pagesize, indexMax = pageindex * pagesize }))
                {
                    count = multi.Read<int>().SingleOrDefault();
                    orders = multi.Read<OrderModel>().ToList();
                }
            }
            return orders;
        }

此间而留心下之凡,multi.Read的各个必须与Sql查询出来的数据集合顺序一样。

前端数据显示

1.率先以View中添加援

@using PagedList.Mvc;
@using PagedList;
@model Models.OrderViewModel 

2.吧查询创建一个表单

<div class="page-header">

    @using (Html.BeginForm("List", "Order", FormMethod.Post, new { id = "OrderForm", @class = "form-horizontal" }))
    {
        @Html.Raw("客户名称:") @Html.TextBoxFor(m => m.QueryModel.CustomerName)
        @Html.Raw("订单编号:") @Html.TextBoxFor(m => m.QueryModel.OrderNo)

        <button type="submit" class="btn btn-purple btn-sm">查询</button>

        //咿,这个干吗用的?后面会解释
        <input type="hidden" name="page" value="1" />
    }
</div>

3.绑定数据

<table class="table loading table-bordered margin-top-5 margin-bottom-5">
    <thead>
        <tr>
            <th>订单编号</th>
            <th>客户名称</th>
            <th>手机号码</th>            
            <th>商品数量</th>
            <th>订单金额</th>
            <th>下单时间</th>
        </tr>
    </thead>
    <tbody>

        @foreach (var item in Model.OrderList)
        {
            <tr>
                <td>@item.orderNo</td>
                <td>@item.customerName</td>
                <td>@item.customerMobile</td>
                <td>@item.productQuantity</td>
                <td>@item.orderAmount</td>
                <td>@item.orderCreateTime</td>
            </tr>
        }
    </tbody>
</table>

4.绑定分页插件数据

@if (Model.OrderList != null&&Model.OrderList.Any())
{
    <div class="pagedList" style="margin:0 auto;text-align:center">
        @Html.PagedListPager(Model.OrderList, page => Url.Action("List", new { page }), PagedListRenderOptions.Classic)
    </div>
}

 

OK,一切搞定了,运行而见面发觉,分页导航生成的链接都是 “/Order/List/2”
这种形式,无法支撑我们把其他查询参数为传递到Controller。

咱们转移一个思路,为什么未将page这个参数放到form表单去了?
 还记我们form中产生一个name=page 的hidden input吧?

    $(function () {

        $(".pagination > li > a").click(function () {

            event.preventDefault();

            var index = $(this).html();

            if (index == '»') {
                index = parseInt($(".pagination > li[class=active] > a").html()) + 1;
            }
            if (index == '«') {
                index = parseInt($(".pagination > li[class=active] > a").html()) - 1;
            }

            if (index < 1) return;

            $("input[name=page]").val(index);
            $("#OrderForm").submit();
        });

    });

 

透过就段JS,直接将本分页的a标签作废了,获取他的page值Bootstrap放到了form中,然后径直触发form的submit(),这样就是满足了我们一般的查询工作需求。 

正文博客园地址:http://www.cnblogs.com/struggle999/p/6918524.html

相关文章