zoukankan      html  css  js  c++  java
  • EasyUI 之 DataGrid利用用拉姆达表达式实现分页查询

      首先,我们在DataGrid的URL中加上我们要查询的条件:查询用户名不是“呵呵”的所有用户。

    [html] view plain copy
     
    1. <div>  
    2.         <table id="dg" class="easyui-datagrid" style=" 600px; height: 350px" >  
    3.             <thead>  
    4.                 <tr>  
    5.                     <th data-options="field:'UserID',148,sortable:true">ID</th>  
    6.                     <th data-options="field:'UserName',148,sortable:true">姓名</th>  
    7.                     <th data-options="field:'Sex',148,sortable:true">性别</th>  
    8.                 </tr>  
    9.             </thead>  
    10.   
    11.         </table>  
    12.     </div>  
    13.   
    14.     <!--datagrid基本设置-->  
    15.     <script type="text/javascript">  
    16.         $(function () {  
    17.             $('#dg').datagrid({  
    18.                 title: '测试表格',  
    19.                 url: "/EvaluationSituation/jsonTest?strUserName=呵呵", //添加查询条件  
    20.                 pagination: true,//显示分页工具栏  
    21.                 fitColumns: true, //自动大小  
    22.   
    23.             });  
    24.         });  
    25.     </script>  

            然后我们在后台接收参数,并执行查询

    [csharp] view plain copy
     
    1. public JsonResult  jsonTest()  
    2.        {  
    3.            #region 制造假数据  
    4.            List<User> listUser = new List<User>();  
    5.   
    6.            listUser.Add(new User {   
    7.                UserID ="001",  
    8.                UserName="呵呵",  
    9.                Sex ="男"  
    10.            });  
    11.            listUser.Add(new User  
    12.            {  
    13.                UserID = "002",  
    14.                UserName = "哈哈",  
    15.                Sex = "女"  
    16.            });   
    17.            listUser.Add(new User  
    18.            {  
    19.                UserID = "003",  
    20.                UserName = "嘿嘿",  
    21.                Sex = "男"  
    22.            });  
    23.            listUser.Add(new User  
    24.            {  
    25.                UserID = "004",  
    26.                UserName = "嘻嘻",  
    27.                Sex = "男"  
    28.            });  
    29.            listUser.Add(new User  
    30.            {  
    31.                UserID = "002",  
    32.                UserName = "哈哈",  
    33.                Sex = "女"  
    34.            });  
    35.            listUser.Add(new User  
    36.            {  
    37.                UserID = "003",  
    38.                UserName = "嘿嘿",  
    39.                Sex = "男"  
    40.            });  
    41.            listUser.Add(new User  
    42.            {  
    43.                UserID = "004",  
    44.                UserName = "嘻嘻",  
    45.                Sex = "男"  
    46.            });  
    47.            listUser.Add(new User  
    48.            {  
    49.                UserID = "002",  
    50.                UserName = "哈哈",  
    51.                Sex = "女"  
    52.            });  
    53.            listUser.Add(new User  
    54.            {  
    55.                UserID = "003",  
    56.                UserName = "嘿嘿",  
    57.                Sex = "男"  
    58.            });  
    59.            listUser.Add(new User  
    60.            {  
    61.                UserID = "004",  
    62.                UserName = "嘻嘻",  
    63.                Sex = "男"  
    64.            });  
    65.            listUser.Add(new User  
    66.            {  
    67.                UserID = "002",  
    68.                UserName = "哈哈",  
    69.                Sex = "女"  
    70.            });  
    71.            listUser.Add(new User  
    72.            {  
    73.                UserID = "003",  
    74.                UserName = "嘿嘿",  
    75.                Sex = "男"  
    76.            });  
    77.            listUser.Add(new User  
    78.            {  
    79.                UserID = "004",  
    80.                UserName = "嘻嘻",  
    81.                Sex = "男"  
    82.            });  
    83.            #endregion  
    84.   
    85.            string strUserName = Request["strUserName"];//查询条件  
    86.            var pageIndex = int.Parse(Request["page"]);//当前页  
    87.            var pageSize = int.Parse(Request["rows"]);//页面行数    
    88.            var total = 0;  
    89.   
    90.            //首先得到符合该查询条件的总记录条数  
    91.            total = listUser.Where(p => p.UserName != strUserName).Count();  
    92.   
    93.            //根据页号、页面大小和查询条件查询当前页应该显示的内容  
    94.            var listQuery = listUser.Where(p => p.UserName != strUserName)  //查询条件为:UserName不等于strUserName  
    95.                             .OrderBy(p => p.UserID)    //根据UserID进行升序排序  
    96.                             .Skip(pageSize * (pageIndex - 1))  //跳过记录中的前(pageSize * (pageIndex - 1))行,假如当前页为第二页-pageIndex=2,每页显示10条数据-pageSize=10,那么就跳过记录中的前10条数据,达到分页查询的效果  
    97.                             .Take(pageSize);   //取出pageSize条记录  
    98.   
    99.            var data = new  
    100.            {  
    101.                total,  
    102.                rows = listQuery  
    103.            };  
    104.   
    105.            //将数据转换成Json格式  
    106.            JsonResult jsonUser = new JsonResult();  
    107.            jsonUser = Json(data);  
    108.   
    109.   
    110.            return jsonUser;  
    111.        }  


            然后我们就可以看见结果:

                                                       

                                                     

            分页查询是一个很普通的东西,基本上只要有表格就需要分页查询,所以这些东西我们还是需要掌握。其中的拉姆达表达式,我们也可以在VS中通过F12转到定义,多看看其中方法的定义。

  • 相关阅读:
    LeetCode 1110. Delete Nodes And Return Forest
    LeetCode 473. Matchsticks to Square
    LeetCode 886. Possible Bipartition
    LeetCode 737. Sentence Similarity II
    LeetCode 734. Sentence Similarity
    LeetCode 491. Increasing Subsequences
    LeetCode 1020. Number of Enclaves
    LeetCode 531. Lonely Pixel I
    LeetCode 1091. Shortest Path in Binary Matrix
    LeetCode 590. N-ary Tree Postorder Traversal
  • 原文地址:https://www.cnblogs.com/yachao1120/p/6492785.html
Copyright © 2011-2022 走看看