首先,我们在DataGrid的URL中加上我们要查询的条件:查询用户名不是“呵呵”的所有用户。
- <div>
- <table id="dg" class="easyui-datagrid" style="width: 600px; height: 350px" >
- <thead>
- <tr>
- <th data-options="field:‘UserID‘,width:148,sortable:true">ID</th>
- <th data-options="field:‘UserName‘,width:148,sortable:true">姓名</th>
- <th data-options="field:‘Sex‘,width:148,sortable:true">性别</th>
- </tr>
- </thead>
-
- </table>
- </div>
-
-
- <script type="text/javascript">
- $(function () {
- $(‘#dg‘).datagrid({
- title: ‘测试表格‘,
- url: "/EvaluationSituation/jsonTest?strUserName=呵呵", //添加查询条件
- pagination: true,//显示分页工具栏
- fitColumns: true, //自动大小
-
- });
- });
- </script>
然后我们在后台接收参数,并执行查询
- public JsonResult jsonTest()
- {
- #region 制造假数据
- List<User> listUser = new List<User>();
-
- listUser.Add(new User {
- UserID ="001",
- UserName="呵呵",
- Sex ="男"
- });
- listUser.Add(new User
- {
- UserID = "002",
- UserName = "哈哈",
- Sex = "女"
- });
- listUser.Add(new User
- {
- UserID = "003",
- UserName = "嘿嘿",
- Sex = "男"
- });
- listUser.Add(new User
- {
- UserID = "004",
- UserName = "嘻嘻",
- Sex = "男"
- });
- listUser.Add(new User
- {
- UserID = "002",
- UserName = "哈哈",
- Sex = "女"
- });
- listUser.Add(new User
- {
- UserID = "003",
- UserName = "嘿嘿",
- Sex = "男"
- });
- listUser.Add(new User
- {
- UserID = "004",
- UserName = "嘻嘻",
- Sex = "男"
- });
- listUser.Add(new User
- {
- UserID = "002",
- UserName = "哈哈",
- Sex = "女"
- });
- listUser.Add(new User
- {
- UserID = "003",
- UserName = "嘿嘿",
- Sex = "男"
- });
- listUser.Add(new User
- {
- UserID = "004",
- UserName = "嘻嘻",
- Sex = "男"
- });
- listUser.Add(new User
- {
- UserID = "002",
- UserName = "哈哈",
- Sex = "女"
- });
- listUser.Add(new User
- {
- UserID = "003",
- UserName = "嘿嘿",
- Sex = "男"
- });
- listUser.Add(new User
- {
- UserID = "004",
- UserName = "嘻嘻",
- Sex = "男"
- });
- #endregion
-
- string strUserName = Request["strUserName"];
- var pageIndex = int.Parse(Request["page"]);
- var pageSize = int.Parse(Request["rows"]);
- var total = 0;
-
-
- total = listUser.Where(p => p.UserName != strUserName).Count();
-
-
- var listQuery = listUser.Where(p => p.UserName != strUserName)
- .OrderBy(p => p.UserID)
- .Skip(pageSize * (pageIndex - 1))
- .Take(pageSize);
-
- var data = new
- {
- total,
- rows = listQuery
- };
-
-
- JsonResult jsonUser = new JsonResult();
- jsonUser = Json(data);
-
-
- return jsonUser;
- }
然后我们就可以看见结果:
分页查询是一个很普通的东西,基本上只要有表格就需要分页查询,所以这些东西我们还是需要掌握。其中的拉姆达表达式,我们也可以在VS中通过F12转到定义,多看看其中方法的定义。
EasyUI 之 DataGrid利用用拉姆达表达式实现分页查询
原文:http://www.cnblogs.com/yachao1120/p/6492785.html