博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC简捷调用EasyUI的datagrid
阅读量:5907 次
发布时间:2019-06-19

本文共 5373 字,大约阅读时间需要 17 分钟。

一直想在项目中使用EasyUi的datagrid,但种种原因,没有实现。

这两天在开发一个项目中,愿望终于得以实现。

先看效果:

实现步骤是这样的:

1,在页面中画dataGrid,具体代码如下:

function ListLoad() { var columnsSetting = [ { field: 'EnName', title: '英文名', width: 100,sortable:true, formatter: function (val, rowData) { var url = "/User/Index/66666666"; return ""+rowData.EnName+""; } }, { field: 'CnName', title: '中文名', width: 80 }, { field: 'DomainName', title: '域帐号', width: 120 }, { field: 'ID', title: 'ID', width: 80, align: 'right' }, { field: 'unitcost', title: '部门', width: 80, align: 'right' }, { field: 'attr1', title: '权限组', width: 250 }, { field: 'status', title: '状态', width: 60, align: 'center' } ]; $('#UserListTb').datagrid({ url: '/User/GetUserList', queryParams: { name: "wuf" }, method: 'get', width: 1077, height: 250, fitColumns: true, pagination: true, pageSize: 20, singleSelect: true, showPageList: false, pageList: [1,20, 50, 100], rownumbers: true, nowrap: false, loadMsg: 'Load……', columns: [columnsSetting], onLoadSuccess: function (row) {}, onLoadError: function (arguments) {} }); }
View Code

2,后台接收参数,返回需要的Json数据。

///         /// 用户列表Json        ///         /// 
public ActionResult GetUserList() { int currentPageIndex = RequestExtension.GetQueryString
("page", 0); int pagesize = RequestExtension.GetQueryString
("rows", 0); String sort = RequestExtension.GetQueryString
("sort", ""); String order = RequestExtension.GetQueryString
("order", ""); String name = RequestExtension.GetQueryString
("name", ""); Pagination pagin = new Pagination { CurrentPageIndex = currentPageIndex, PageSize = pagesize, OrderBy = String.IsNullOrEmpty(sort) ? "" : String.Concat(sort + " " + order) }; UsersModel condition = new UsersModel(); KeyValuePair
> list = usersRepository.UsersPagination(pagin, condition); return JsonExtension.JsonPagination(list.Value, pagin.CurrentPageIndex, pagin.PageSize, list.Key.TotalItemCount); }
View Code

3,关键代码,实现Json分页的方法。

///         /// 分页处理        ///         ///         ///         ///         /// 
public static JsonResult JsonPagination(IEnumerable
dataList, int page, int rows, int dataListAuctalCount = -1) { var result = dataList; Dictionary
json = new Dictionary
(); int total = dataListAuctalCount; if (total == -1 && dataList != null) { //自动分页 total = dataList.Count(); result = dataList.Skip((page - 1) * rows).Take(rows).ToList(); } json.Add("total", total); json.Add("rows", result); return new JsonResult() { Data = json, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; }
View Code

4,数据分页查询代码

public KeyValuePair
> UsersPagination(Pagination pagin, UsersModel condition) { using (SqlConnection conn = DapperFactory.CrateOpenConnection()) { String condtionStr = ""; String orderBy = "CreateTime DESC"; if (!String.IsNullOrEmpty(pagin.OrderBy)) { orderBy = pagin.OrderBy; } String executeQuery = String.Format(@"WITH pagintable AS( SELECT ROW_NUMBER() OVER(ORDER BY {1} )AS RowID, ID, DomainName, EnName, PassWord, CnName, Email, DeptID, RoleId, CreateMan, CreateTime, EditMan, EditTime, IsValid FROM Users WHERE 1= 1 {0} ) SELECT * FROM pagintable where RowID between ((@CurrentPageIndex - 1) * @PageSize) + 1 and (@CurrentPageIndex * @PageSize)", condtionStr, orderBy); String executeCount = String.Format("SELECT COUNT(*) AS CountNum FROM Users WHERE 1= 1 {0} ", condtionStr); var mixCondition = new { CurrentPageIndex = pagin.CurrentPageIndex, PageSize = pagin.PageSize }; List
listScore = conn.Query
(executeQuery, mixCondition).ToList(); pagin.TotalItemCount = conn.Query
(executeCount, mixCondition).SingleOrDefault
(); KeyValuePair
> result = new KeyValuePair
>(pagin, listScore); return result; } }
View Code

看着这精简的代码,和界面上强大的功能,真是有种IT人的自豪感,亲爱的您,是不是也有这种感觉呢?

 

转载于:https://www.cnblogs.com/ushou/p/3718610.html

你可能感兴趣的文章
[转]Apache重写中的flag说明
查看>>
20171011
查看>>
自我总结(九)---
查看>>
【N-Queens】cpp
查看>>
soap 的应用
查看>>
APP的广告模式
查看>>
STM32F4相关
查看>>
WPF自定义控件与样式(11)-等待/忙/正在加载状态-控件实现
查看>>
margin:0 auto 与 text-align:center 的区别(转载)
查看>>
判断一个字符是否为数字的两种方法(C/C++)
查看>>
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleExcept问题解决方案
查看>>
600. Non-negative Integers without Consecutive Ones
查看>>
关于正则表达式的入门心得
查看>>
滑雪在日本 之 新泻篇 5
查看>>
【344】Jupyter relevant problems
查看>>
axios 拦截 , 页面跳转, token 验证(自己摸索了一天搞出来的)
查看>>
基础设施即服务系列:Windows Azure上支持Linux虚拟机
查看>>
BTree和B+Tree详解
查看>>
3D打印浪潮中的赢家与输家
查看>>
链接自动化测试工具xenu
查看>>