博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
datagrid中巧用loadFilter对数据整形
阅读量:6138 次
发布时间:2019-06-21

本文共 2998 字,大约阅读时间需要 9 分钟。

    最近由于数据格式没有完全弄懂,在使用datagrid加载数据时各种报错真让人抓狂,今天得一高人指点,总算是解决了数据加载问题,略有心得,望与大家分享。

   先是js,这里只需关注loadfilter里的内容。loadfilter介绍:返回过滤数据显示。该函数带一个参数'data'用来指向源数据(即:获取的数据源,比如Json对象)。您可以改变源数据的标准数据格式。这个函数必须返回包含'total'和'rows'属性的标准数据对象。返回的数据格式必须为{todal:int 行数 ,rows:数据}。

1 function GetUserList(Sqlwhere) { 2     $("#userlist").datagrid({ 3         width: 400, 4         url: "../ajax/UserMAjax.ashx", 5         method: 'post', 6         queryParams: { 7             cmd: "list", 8             strSqlWhere: Sqlwhere, 9         },10         loadFilter:function(data){11             return {total:data.data.length,rows:data.data};12         },13         columns: [[14             { field: "UserId", title: "userid" },15             { field: "UserName", title: "username" },16               { field: "Pwd", title: "password" },17         { field: "RegReason", title: "regreason" },18 { field: "QQ", title: "QQ" },19   { field: "HeadPic", title: "headpic" }20         ]],             });

采用mvc3搭建的后台,由于主要测试datagrid数据加载,查询语句做了简化"select * from userinfo",查询出的内容为userinfo表中的所有内容,返回数据内容如下:{"data":[{"UserId":21,"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":20,"UserName":"fffffff33dddd","Pwd":"ffff","RegReason":"fff","QQ":"33333","HeadPic":null,"RegDate":"\/Date(1435595018103)\/","PrimaryKey":"UserId","IsAutoID":true,"DataBaseName":1,"IsExternalConn":false,"connName":"","HasIdentityPK":true}]}

为便于显示只取两条数据,经过loadfilter后,数据变为

{

todal:2,rows:[{"UserId":21,"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":20,"UserName":"fffffff33dddd","Pwd":"ffff","RegReason":"fff","QQ":"33333","HeadPic":null,"RegDate":"\/Date(1435595018103)\/","PrimaryKey":"UserId","IsAutoID":true,"DataBaseName":1,"IsExternalConn":false,"connName":"","HasIdentityPK":true}]},之后就可以使用columns随意显示数据啦!

1     public class UserMAjax : IHttpHandler 2     { 3         HttpContext context; 4         JavaScriptSerializer jss = new JavaScriptSerializer(); 5         string json = ""; 6         public void ProcessRequest(HttpContext context) 7         { 8             this.context = context; 9             context.Request.ContentEncoding = Encoding.GetEncoding("utf-8");10             string cmd = context.Request["cmd"].ToString();11             string strSqlwhere = context.Request["strSqlWhere"].ToString();12             List
UserList = UserInfoDal.C_UserInfo.GetList(strSqlwhere);13 Dictionary
> db = new Dictionary
>();14 db.Add("data", UserList);15 json = jss.Serialize(db);16 context.Response.Write(json);

 

转载于:https://www.cnblogs.com/cilec/p/4649569.html

你可能感兴趣的文章
数据加密插件
查看>>
linux后台运行程序
查看>>
win7 vs2012/2013 编译boost 1.55
查看>>
IIS7如何显示详细错误信息
查看>>
Android打包常见错误之Export aborted because fatal lint errors were found
查看>>
Tar打包、压缩与解压缩到指定目录的方法
查看>>
配置spring上下文
查看>>
Python异步IO --- 轻松管理10k+并发连接
查看>>
Oracle中drop user和drop user cascade的区别
查看>>
登记申请汇总
查看>>
Office WORD如何取消开始工作右侧栏
查看>>
Android Jni调用浅述
查看>>
CodeCombat森林关卡Python代码
查看>>
第一个应用程序HelloWorld
查看>>
(二)Spring Boot 起步入门(翻译自Spring Boot官方教程文档)1.5.9.RELEASE
查看>>
Java并发编程73道面试题及答案
查看>>
企业级负载平衡简介(转)
查看>>
ICCV2017 论文浏览记录
查看>>
科技巨头的交通争夺战
查看>>
当中兴安卓手机遇上农行音频通用K宝 -- 卡在“正在通讯”,一直加载中
查看>>