MongoDb 中的PageFilter
来源:互联网 发布:精点数据 编辑:程序博客网 时间:2024/05/17 20:32
PageFilter的定义
在Controller中:
在 View 中:
DisplayTemplate的定义 :
public abstract class FilterBase { public string Prefix { get; set; } protected abstract void Internal_ApplyFilter<TDefaultDocument>(MongoCursor<TDefaultDocument> cursor); private readonly IList<FilterBase> _childFilters = new List<FilterBase>(); public T WithFilter<T>(T filter) where T : FilterBase { _childFilters.Add(filter); return filter; } public void AddFilter<T>(T filter) where T : FilterBase { _childFilters.Add(filter); } protected FilterBase(string prefix) { Prefix = prefix; } protected FilterBase(FilterBase relatedFilter) { Prefix = relatedFilter.Prefix; relatedFilter.AddFilter(this); } public MongoCursor<TDefaultDocument> ApplyFilter<TDefaultDocument>(MongoCursor<TDefaultDocument> cursor) { Internal_ApplyFilter(cursor); foreach (var childFilter in _childFilters) { childFilter.ApplyFilter<TDefaultDocument>(cursor); } return cursor; } } public class PagingFilter : FilterBase { private int _itemsPerPage = 50; private int _currentPage = 1; private int _pagesPerPaginate = 9; public bool IsHttpPost { get; private set; } public PagingFilter(FilterBase filterBase, IBaseFilterContext context) : base(filterBase) { IsNeedPaging = true; UpdateContext(filterBase.Prefix, context); } private PagingFilter(string prefix) : base(prefix) { IsNeedPaging = true; } public PagingFilter(string prefix, IBaseFilterContext context, int itemsPerPage = 50) : this(prefix) { UpdateContext(prefix, context, itemsPerPage); } private void UpdateContext(string prefix, IBaseFilterContext context, int itemsPerPage = 50) { ItemsPerPage = itemsPerPage; if (context == null) { throw new ArgumentNullException("context"); } try { var currentPage = Convert.ToInt32(context.Parameter(PrivateCurrentPageParamName(prefix))); if (currentPage > 0) { _currentPage = currentPage; } } catch (Exception) { } _itemsPerPage = itemsPerPage; try { itemsPerPage = Convert.ToInt32(context.Parameter(PrivateItemsPerPageParamName(prefix))); if (itemsPerPage > 0) { _itemsPerPage = ItemsPerPage; } } catch (Exception) { } } public PagingFilter(int pageNumber, int itemsPerPage, string prefix) : this(prefix) { _currentPage = pageNumber; ItemsPerPage = itemsPerPage; } private string PrivateItemsPerPageParamName(string prefix) { return string.Format("{0}ip", prefix); } public string ItemsPerPageParamName { get { return PrivateItemsPerPageParamName(Prefix); } } private string PrivateCurrentPageParamName(string prefix) { return string.Format("{0}cp", prefix); } public string CurrentPageParamName { get { return PrivateCurrentPageParamName(Prefix); } } public int Skip { get { return (CurrentPage - 1) * _itemsPerPage; } } public int Take { get { return _itemsPerPage; } set { _itemsPerPage = value; } } public bool IsNeedPaging { get; set; } public long TotalCount { get; set; } public int CurrentPage { get { if (_currentPage <= 0) { return 1; } return _currentPage; } } public bool IsCurrentPage(long page) { return CurrentPage == page; } public long PageEnd { get { var pageEnd = CurrentPage * ItemsPerPage; if (pageEnd > TotalCount) { return TotalCount; } return pageEnd; } } public int ItemsPerPage { get { return _itemsPerPage; } set { _itemsPerPage = value; } } public long TotalPagesCount { get { return TotalCount / ItemsPerPage + ((TotalCount % ItemsPerPage > 0) ? 1 : 0); } } public bool FirstPage { get { return CurrentPage == 1; } } public bool LastPage { get { return CurrentPage >= TotalPagesCount; } } public long[] PaginationIndexes { get { var pageWindow = (_pagesPerPaginate - 1) / 2; long start = CurrentPage - pageWindow; if (start < 1) { start = 1; } long end = CurrentPage + pageWindow; if (end < _pagesPerPaginate) { end = _pagesPerPaginate; } if (end > TotalPagesCount) { end = TotalPagesCount; start = TotalPagesCount - _pagesPerPaginate + 1; } if (start < 1) { start = 1; } var retVal = new long[end - start + 1]; for (int i = 0; i < retVal.Length; i++) { retVal[i] = start++; } return retVal; } } /*public MongoCursor<TDefaultDocument> ApplySort<TDefaultDocument>(MongoCursor<TDefaultDocument> cursor, SortByBuilder sort) { cursor.SetSortOrder(sort); throw new NotImplementedException(); }*/ protected override void Internal_ApplyFilter<TDefaultDocument>(MongoCursor<TDefaultDocument> cursor) { if (IsNeedPaging) { cursor.SetSkip(Skip).SetLimit(Take); TotalCount = cursor.Count(); } } public PagingFilter IsHttpPosting(bool isHttpPost) { this.IsHttpPost = isHttpPost; return this; } public PagingFilter SetCurrentPage(int page) { _currentPage = page; return this; } }
在Mongo查询时:
pageFilter.ApplyFilter("your_mongo_collection".Find(mainQuery))
在Controller中:
var pagingFilter = new PagingFilter("myd", new RequestFilterProperty(Request),20);
在 View 中:
@Html.DisplayFor(x => x.Paging)
DisplayTemplate的定义 :
@using Pearls.MVC@model XXX.Backend.Pagination.PagingFilter @if (Model.TotalPagesCount > 1){ <ul class="pagination pagination-sm"> <li class="@Model.FirstPage.AssignIfTrue("disabled")"> <a data-ishttppost="@Model.IsHttpPost" data-currentpage="@(Model.CurrentPage - 1)" href="@Url.AddParam(Model.CurrentPageParamName, Model.CurrentPage - 1)">«</a> </li> @foreach (var page in Model.PaginationIndexes) { <li class="@Model.IsCurrentPage(page).AssignIfTrue("active")"> <a data-ishttppost="@Model.IsHttpPost" data-currentpage="@page" href="@Url.AddParam(Model.CurrentPageParamName, page)">@Html.DisplayFor(x => page)</a> </li> } <li class="@Model.LastPage.AssignIfTrue("disabled")"> <a data-ishttppost="@Model.IsHttpPost" data-currentpage="@(Model.CurrentPage + 1)" href="@Url.AddParam(Model.CurrentPageParamName, Model.CurrentPage + 1)">»</a> </li> </ul> }
0 0
- MongoDb 中的PageFilter
- pagefilter
- com.opensymphony.module.sitemesh.filter.pagefilter作用?
- com.opensymphony.module.sitemesh.filter.pagefilter作用
- com.opensymphony.module.sitemesh.filter.pagefilter作用
- com.opensymphony.module.sitemesh.filter.pagefilter作用?
- Hbase查询结果分页(PageFilter)
- mongoDB中的DBRef
- 5.MongoDB中的索引。
- 6.MongoDB中的聚合
- mongoDB中的group
- Mongodb中的like查询
- mongoDb php中的使用
- MongoDB中的Group By
- mongodb中的一些符号
- mongodb中的null
- mongodb中的游标
- mongoDB 中的group
- 导数的几何意义和物理意义, 求曲线y=f(x) 在相应点处的切线方程,法线方程
- Linux那些事儿 之 戏说USB(大结局)还是那个match
- 黑马程序员——多线程(Lock、setDaemon、join、setPriority、yield)
- 项目管理之团队沟通里有“三不要?”
- Android网络图片加载优化
- MongoDb 中的PageFilter
- linux下的系统调用函数到内核函数的追踪
- SVN使用方法
- iOS开发中的UIApplication
- Exception(Java异常)
- 02-线性结构3. Pop Sequence (25)
- Java集合框架解析
- Mysql架构优化和索引——选择优化的数据类型
- VC2010添加菜单快捷键出现问题怎么办