当前位置: 首页 > news >正文

MongoDb 中的PageFilter

PageFilter的定义


 
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>
    
}


相关文章:

  • 新浪换标,也许应该做点什么了
  • 使用joint.js 绘制图
  • myfaces与mojarra(JSF RI)处理空属性类型的不同
  • jsonp 示例
  • 平媒怎么了?
  • The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF
  • Neubla3中的骨骼动画: CoreAnimation子系统
  • TPL Part 1 Task 的使用
  • 准备移植xynth到arm板上了。
  • TPL - Part 2 异常处理常用方式
  • 一个关于TCHAR的离奇bug
  • C# 使用Xamarin 开发应用 -- 切换Activity
  • Vsual Studio2005打开速度很慢的超级解决方案
  • OO系统分析员之路--用例分析系列(7)--用例规约的编写--业务规则和实体描述[整理重发]...
  • 批量删除多台服务器上的漫游用户配置文件
  • #Java异常处理
  • 【React系列】如何构建React应用程序
  • 【知识碎片】第三方登录弹窗效果
  • 230. Kth Smallest Element in a BST
  • HTTP中GET与POST的区别 99%的错误认识
  • JAVA 学习IO流
  • mysql常用命令汇总
  • MySQL-事务管理(基础)
  • ng6--错误信息小结(持续更新)
  • overflow: hidden IE7无效
  • PAT A1120
  • VUE es6技巧写法(持续更新中~~~)
  • webpack+react项目初体验——记录我的webpack环境配置
  • win10下安装mysql5.7
  • 不用申请服务号就可以开发微信支付/支付宝/QQ钱包支付!附:直接可用的代码+demo...
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 聊聊flink的BlobWriter
  • 码农张的Bug人生 - 初来乍到
  • 软件开发学习的5大技巧,你知道吗?
  • 使用SAX解析XML
  • 异步
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • raise 与 raise ... from 的区别
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • ​低代码平台的核心价值与优势
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (转)JAVA中的堆栈
  • (转)原始图像数据和PDF中的图像数据