easyui datagrid+mvc+json之asp.net分页查询

最近在做分页查询的功能,在网上也翻看了不少,但是自己的吸收能力就差了好多,而且当时最大的想法就是,怎么就没有我想要的那种,既是easyui的,又要用mvc的架构,还要能够实现底层的分页传值,用.net平台写的demo,那时就想,等我做出来了,我也要写一篇分页查询的博客,专门为了实现这种需求来做的demo

效果图

前台view

  1. <tableid="list_data"class="easyui-datagrid"style="width:1075px;height:300px"cellpascing="0"cellpadding="0"></table>

[javascript]
    <scripttype="text/javascript">
  1. //表格的样式
  2. $(function(){
  3. $('#list_data').datagrid({
  4. title:'建议',
  5. iconCls:'icon-view',//图标
  6. loadMsg:"数据加载中,请稍后......",108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> width:1056,
  7. height:'auto',108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> nowrap:false,
  8. striped:true,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> border: collapsible://是否可折叠
  9. fit://自动大小
  10. url:"/EvaluationSuggestion/GetData",0); background-color:inherit">//controller的地址,controller的名字+返回json的方法
  11. remoteSort: singleSelect://是否单选
  12. pagination://分页控件
  13. rownumbers://行号
  14. columns:[[
  15. {field:'SuggestionContent',title:'建议',width:'1056'},0); background-color:inherit">//选择
  16. ]],
  17. });
  18. //设置分页控件
  19. varp=$('#list_data').datagrid('getPager');
  20. $(p).pagination({
  21. pageNumber:1,//默认显示第几页
  22. pageSize:10,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> pageList:[5,10,15],108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> beforePageText:'第',108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> afterPageText:'页共{pages}页',108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> displayMsg:'当前显示{from}-{to}条记录共{total}条记录'
  23. </script>

解说:如果想要进行分页,首先就必须将datagrid的属性pagination设置为true

pager是分页栏,这个标签用来设置分页的总体参数,

url是链接的重要根地址,pager标签会在这个链接的基础上附加分页参数。

Controller.cs

[csharp]
    #region查询建议的controller
  1. ///<summary>
  2. ///查询建议的controller
  3. ///</summary>
  4. ///<returns>返回值action,用于与view层交互</returns>
  5. publicActionResultSuggestionIndex()
  6. {
  7. returnView();
  8. }
  9. #endregion
  10. #region将建议数据转换成json字符串
  11. ///将建议数据转换成json字符串
  12. ///<returns>返回json字符串</returns>
  13. publicJsonResultGetData()
  14. IEvaluationSuggestionWCFsuggestion=ServiceFactory.GetEvaluationSuggestion();
  15. List<EvaluationSuggestion>lstResut=newList<EvaluationSuggestion>();
  16. //接收datagrid传来的参数
  17. varpageIndex=int.Parse(Request["page"]);//当前页
  18. varpageSize=int.Parse(Request["rows"]);//页面行数
  19. vartotal=0;
  20. lstResut=suggestion.QuerySuggestionbyPage(pageSize,pageIndex,outtotal);
  21. vardata=new
  22. {
  23. total,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> rows=lstResut
  24. };
  25. returnJson(data,JsonRequestBehavior.AllowGet);
  26. #endregion

解说:pagerows是直接可以从前台获取的。data设置数据格式,转换成Json字符串后,能够在分页中正确获取。


服务端

    #region分页查询+排序:
  1. ///分页查询+排序
  2. ///<typeparamname="Tkey">泛型</typeparam>
  3. ///<paramname="pageSize">每页大小</param>
  4. ///<paramname="pageIndex">当前页码</param>
  5. ///<paramname="total">总数量</param>
  6. ///<paramname="orderbyLambda">排序条件</param>
  7. ///<paramname="isAsc">是否升序</param>
  8. ///<returns>IQueryable泛型集合</returns>
  9. publicIQueryable<T>LoadPageItems<Tkey>(intpageSize,intpageIndex,153); font-weight:bold; background-color:inherit">outinttotal,Func<T,Tkey>orderbyLambda,153); font-weight:bold; background-color:inherit">boolisAsc)
  10. total=MyBaseDbContext.Set<T>().Count();
  11. if(isAsc)
  12. vartemp=MyBaseDbContext.Set<T>().OrderBy<T,Tkey>(orderbyLambda)
  13. .Skip(pageSize*(pageIndex-1))
  14. .Take(pageSize);
  15. returntemp.AsQueryable();
  16. }
  17. else
  18. vartemp=MyBaseDbContext.Set<T>().OrderByDescending<T,Tkey>(orderbyLambda)
  19. .Skip(pageSize*(pageIndex-1))
  20. .Take(pageSize);
  21. returntemp.AsQueryable();
  22. #endregion

解说:这个是我们底层的类库,我直接贴过来的,底层使用EF,涉及到lambda表达式。不过除去形式,分页查询的思想都是一样的,真分页,根据记录总数,每页记录数和当前页码,获取当前页码的数据集合。页数=总记录数/每页记录数。当前页码的数据集合,向数据库传递条件筛选,from(当前页码-1*每页记录数to当前页码*每页记录数获取当前页码数据集合。具体实现自己来做吧。

相关文章

文章浏览阅读2.4k次。最近要优化cesium里的热力图效果,浏览...
文章浏览阅读1.2w次,点赞3次,收藏19次。在 Python中读取 j...
文章浏览阅读1.4k次。首字母缩略词 API 代表应用程序编程接口...
文章浏览阅读802次,点赞10次,收藏10次。解决一个JSON反序列...
文章浏览阅读882次。Unity Json和Xml的序列化和反序列化_uni...