MVC calls Easyui's datagrid simple

Source: Internet
Author: User

Always wanted to use the Easyui DataGrid in the project, but for a variety of reasons, it was not implemented.

These two days in the development of a project, the desire has finally been realized.

First look at the effect:

The implementation steps are this:

1. Draw the DataGrid on the page with the following code:

<table id="USERLISTTB" class="Easyui-datagrid"title="User List"> </table>function Listload () {varColumnssetting =[{field:'Enname', Title:'English name', Width: -, sortable:true, Formatter:function (Val, rowdata) {varURL ="/user/index/66666666"; return "<a href=\ '"+ URL +"\ ');>"+rowdata.enname+"</a>"; }}, {field:'Cnname', Title:'Chinese name', Width: the}, {field:'DomainName', Title:'domain account', Width: -}, {field:'ID', Title:'ID', Width: the, align:' Right'}, {field:'UnitCost', Title:'Department', Width: the, align:' Right'}, {field:'attr1', Title:'Permission Groups', Width: -}, {field:'Status', Title:'Status', Width: -, align:'Center' }                ]; $('#UserListTb'). DataGrid ({URL:'/user/getuserlist', Queryparams: {name:"Wuf"}, Method:'Get', Width:1077, Height: -, Fitcolumns:true, pagination:true, PageSize: -, Singleselect:true, Showpagelist:false, pagelist: [1, -, -, -], Rownumbers:true, nowrap:false, Loadmsg:'Load ...', columns: [columnssetting], onloadsuccess:function (row) {}, Onloaderror:function (arguments)    {}        }); }
View Code

2, background receive parameters, return the required JSON data.

        /// <summary>        ///user list JSON/// </summary>        /// <returns></returns>         PublicActionResult getuserlist () {intCurrentPageIndex = requestextension.getquerystring<int> ("page",0); intPageSize = requestextension.getquerystring<int> ("rows",0); String Sort= Requestextension.getquerystring<string> ("Sort",""); String Order= Requestextension.getquerystring<string> ("Order",""); String name= Requestextension.getquerystring<string> ("name",""); Pagination Pagin=NewPagination {CurrentPageIndex=CurrentPageIndex, PageSize=pagesize.= String.IsNullOrEmpty (sort)?"": String.Concat (sort +" "+order)}; Usersmodel condition=NewUsersmodel (); KeyValuePair<pagination, ilist<usersmodel>> list =usersrepository.userspagination (Pagin, condition); returnjsonextension.jsonpagination (list. Value, Pagin. CurrentPageIndex, Pagin. PageSize, List.        Key.totalitemcount); }
View Code

3, the key code, the implementation of the JSON paging method.

        /// <summary>        ///pagination Processing/// </summary>        /// <param name= "dataList" ></param>        /// <param name= "page" ></param>        /// <param name= "Rows" ></param>        /// <returns></returns>         Public StaticJsonresult jsonpagination (ienumerable<dynamic> dataList,intPageintRowsintDatalistauctalcount =-1)        {            varresult =dataList; Dictionary<string,Object> JSON =Newdictionary<string,Object>(); intTotal =Datalistauctalcount; if(Total = =-1&& DataList! =NULL)            {                //Automatic PagingTotal =Datalist.count (); Result= Datalist.skip (Page-1) *rows). Take (rows).            ToList (); } JSON. ADD (" Total", total); Json. ADD ("rows", result); return NewJsonresult () {Data = json, Jsonrequestbehavior =Jsonrequestbehavior.allowget}; }
View Code

4, Data paging query code

         PublicKeyvaluepair<pagination, ilist<usersmodel>>userspagination (pagination pagin, Usersmodel condition) {using(SqlConnection conn =dapperfactory.crateopenconnection ()) {String Condtionstr=""; String by="createtime DESC"; if(!String.IsNullOrEmpty (Pagin. )) { by=Pagin.                A. } String executeQuery= String.Format (@"with Pagintable as (SELECT row_number () over (ORDER by {1}) as RowID, ID, Domain                                        Name, Enname, PassWord, Cnname, Email, DeptID, Roleid, Createman, Createtime, Editman, EditTime, IsValid from Users Where 1= 1 {0}) SELECT * from pagintable where R Owid Between ((@CurrentPageIndex-1) * @PageSize) + 1 and (@CurrentPageIndex * @PageSize)", Condtionstr, by-and-by); String Executecount= String.Format ("SELECT COUNT (*) as Countnum from Users WHERE 1= 1 {0}", CONDTIONSTR); varMixcondition =New{CurrentPageIndex=Pagin. CurrentPageIndex, PageSize=Pagin.                PageSize}; List<UsersModel> Listscore = conn. Query<usersmodel>(ExecuteQuery, mixcondition).                ToList (); Pagin. Totalitemcount= Conn. Query<int32> (Executecount, mixcondition). Singleordefault<int32>(); KeyValuePair<pagination, ilist<usersmodel>> result =NewKeyvaluepair<pagination, ilist<usersmodel>>(Pagin, Listscore); returnresult; }        }
View Code

Looking at this streamlined code, and the powerful interface features, it is a kind of pride of it people, dear you, do you have this feeling?

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.