Recently due to the data format is not fully understood, in the use of the DataGrid loading data when the various error is really crazy, today to a man pointing, finally solved the problem of data loading, a little experience, hope to share with you.
First JS, here just focus on the content of Loadfilter. Loadfilter Introduction: Return filter data display. The function takes a parameter, ' data ', to point to the source data (i.e., the obtained data source, such as a JSON object). You can change the standard data format of the source data. This function must return a standard data object that contains the ' total ' and ' rows ' properties. The returned data format must be {todal:int rows, rows: Data}.
1 functiongetuserlist (sqlwhere) {2$ ("#userlist"). DataGrid ({3width:400,4URL: ".. /ajax/usermajax.ashx ",5Method: ' Post ',6 queryparams: {7CMD: "List",8 Strsqlwhere:sqlwhere,9 },TenLoadfilter:function(data) { One return{total:data.data.length,rows:data.data}; A }, - columns: [[ -{field: "userid", Title: "UserID" }, the{field: "UserName", Title: "UserName" }, -{field: "Pwd", Title: "Password" }, -{field: "Regreason", Title: "Regreason" }, -{field: "QQ", Title: "QQ" }, +{field: "Headpic", Title: "Headpic" } - ]],});
Using MVC3 to build the background, because the main test DataGrid data load, query statements made a simplified "select * from UserInfo", the contents of the query is userinfo all the contents of the table, the return data content as follows: {"Data": [{"UserId ": +," UserName ":" Fffffff33dddds "," PWD ":" FFFF "," Regreason ":" FFF "," QQ ":" 33333 "," headpic ": null," RegDate ":" \/date "( 1435595121443) \/"," PrimaryKey ":" UserId "," isautoid ": True," DataBaseName ": 1," Isexternalconn ": false," Connname ":" ", "HASIDENTITYPK": true},{"UserId": "UserName": "fffffff33dddd", "PWD": "FFFF", "Regreason": "FFF", "QQ": "33333", " Headpic ": null," RegDate ":" \/date (1435595018103) \ "," PrimaryKey ":" UserId "," isautoid ": True," DataBaseName ": 1," Isexternalconn ": false," Connname ":" "," HASIDENTITYPK ": True}]}
For easy display of only two data, after Loadfilter, the data becomes
{todal:2,rows:[{"UserId": +, "UserName": "Fffffff33dddds", "PWD": "FFFF", "Regreason": "FFF", "QQ": "33333", "headpic": null, "RegDate": "\/date (1435595121443) \", "PrimaryKey": "UserId", "isautoid": true, "DataBaseName ": 1," Isexternalconn ": false," connname ":", "HASIDENTITYPK": true},{"UserId": "UserName": "fffffff33dddd", "PWD": " FFFF "," Regreason ":" FFF "," QQ ":" 33333 "," headpic ": null," RegDate ":" \/date (1435595018103) \ "," PrimaryKey ":" UserId ", "Isautoid": True, "DataBaseName": 1, "Isexternalconn": false, "Connname": "", "HASIDENTITYPK": True}]}, After that, you can use columns to display data freely!
1 Public classUsermajax:ihttphandler2 {3 HttpContext context;4JavaScriptSerializer JSS =NewJavaScriptSerializer ();5 stringJSON ="";6 Public voidProcessRequest (HttpContext context)7 {8 This. Context =context;9Context. request.contentencoding = encoding.getencoding ("Utf-8");Ten stringcmd = context. request["cmd"]. ToString (); One stringStrsqlwhere = context. request["Strsqlwhere"]. ToString (); AList<userinfo> userlist =UserInfoDal.C_UserInfo.GetList (strsqlwhere); -dictionary<string, list<userinfo>> db =Newdictionary<string, list<userinfo>>(); -Db. ADD ("Data", userlist); theJSON =JSS. Serialize (db); -Context. Response.Write (JSON);
Using Loadfilter for Data shaping in the DataGrid