The EasyUI DataGrid uses the lambda expression to implement paging query. easyuidatagrid

Source: Internet
Author: User

The EasyUI DataGrid uses the lambda expression to implement paging query. easyuidatagrid

In the previous blog "Two Methods for assigning values to the DataGrid in EasyUI", we introduced two methods for assigning values to the DataGrid. At the same time, we also left a problem that the paging effect was not actually implemented, today, let's talk about how MVC + EasyUI-DataGrid uses lambda expressions to implement real paging queries.


First, add the query condition in the DataGrid URL: Query all users whose user name is not "Haha.

<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 "> name </th> <th data-options =" field: 'sex', width: 148, sortable: true "> gender </th> </tr> </thead> </table> </div> <! -- Datagrid Basic settings --> <script type = "text/javascript" >$ (function () {$ ('# dg '). datagrid ({title: 'test table', url: "/EvaluationSituation/jsonTest? StrUserName = haha ", // Add the query condition pagination: true, // display the pagination toolbar fitColumns: true, // automatic size}) ;}); </script>



Then we receive parameters in the background and execute the query

Public JsonResult jsonTest () {# region manufacture false data List <User> listUser = new List <User> (); listUser. add (new User {UserID = "001", UserName = "Haha", Sex = "male"}); listUser. add (new User {UserID = "002", UserName = "Haha", Sex = "female"}); listUser. add (new User {UserID = "003", UserName = "", Sex = "male"}); listUser. add (new User {UserID = "004", UserName = "", Sex = "male"}); listUser. add (new User {UserID = "00 2 ", UserName =" Haha ", Sex =" female "}); listUser. add (new User {UserID = "003", UserName = "", Sex = "male"}); listUser. add (new User {UserID = "004", UserName = "", Sex = "male"}); listUser. add (new User {UserID = "002", UserName = "Haha", Sex = "female"}); listUser. add (new User {UserID = "003", UserName = "", Sex = "male"}); listUser. add (new User {UserID = "004", UserName = "", Sex = "male"}); listUser. A Dd (new User {UserID = "002", UserName = "Haha", Sex = "female"}); listUser. add (new User {UserID = "003", UserName = "", Sex = "male"}); listUser. add (new User {UserID = "004", UserName = "", Sex = "male"}); # endregion string strUserName = Request ["strUserName"]; // query condition var pageIndex = int. parse (Request ["page"]); // var pageSize = int. parse (Request ["rows"]); // The number of page rows var total = 0; // obtain the total number of records that meet the query conditions Tal = listUser. Where (p => p. UserName! = StrUserName). Count (); // query the content of the current page based on the page number, page size, and query conditions. var listQuery = listUser. Where (p => p. UserName! = StrUserName) // the query condition is: UserName is not equal to strUserName. orderBy (p => p. userID) // sort by UserID in ascending order. skip (pageSize * (pageIndex-1) // Skip the previous (pageSize * (pageIndex-1) line in the record. If the current page is the second page-pageIndex = 2, 10 data entries per page-pageSize = 10, the first 10 data entries in the record are skipped for paging query. take (pageSize); // retrieve the pageSize record var data = new {total, rows = listQuery}; // convert the data to Json format JsonResult jsonUser = new JsonResult (); jsonUser = Json (data); return jsonUser ;}

Then we can see the result:






Paging query is a very common thing. Basically, as long as there is a table, paging query is required, so we still need to know these things. The Lambda expression can also be converted to the definition through F12 in VS. Let's take a look at the definition of the method.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.