js前台对datagrid的定义代码,如下
mygrid = $(‘#mytable‘).datagrid({ fit: true, //自动大小 height: ‘auto‘, rownumbers: true, //行号 fitColumns: true, collapsible: true, //是否可折叠的 loadMsg: ‘数据装载中......‘, singleSelect: true, //单行选取 pagination: true //显示分页 // columns: [[]] }); //设置分页控件 $("#mytable").datagrid(‘getPager‘).pagination({ beforePageText: "第", afterPageText: "页", displayMsg: "当前 {from} - {to}条数据 共{total} 条数据", pageSize: 100, pageList: [20, 30, 40, 50, 60, 70, 80, 90, 100], onBeforeRefresh: function () { }, onRefresh: function (pageNumber, pageSize) { }, onChangePageSize: function () { }, onSelectPage: function (pageNumber, pageSize) { getData(); } });
js查询请求代码,如下:
function getData() { var param = ""; $(".mynon").each(function () { if ($(this).attr("checked") == "checked") param = param + "#" + $(this).attr("id"); }); if (param != "") { var grid = $(‘#mytable‘); var options = grid.datagrid(‘getPager‘).data("pagination").options; //选项 var pagenum = options.pageNumber; //页数 var total = options.total; //总数 var max = Math.ceil(total / options.pageSize); var pagesize = options.pageSize; //每页行数 var sttime = $("#starttime").combobox(‘getText‘); var ettime = $("#endtime").combobox(‘getText‘); $.ajax({ type: "post", url: "../frm/report.aspx/GetReport", data: "{‘begin‘:‘" + sttime + "‘,‘end‘:‘" + ettime + "‘,‘param‘:‘" + param + "‘,‘pagenum‘:‘" + pagenum + "‘,‘pagesize‘:‘" + pagesize + "‘}", contentType: "application/json; charset=utf-8", dataType: "json", async: false, success: function (data) { var d = eval(‘(‘ + data.d + ‘)‘); try { $(‘#mytable‘).datagrid("loadData", d); } catch (e) { alert(e); } }, error: function (err) { alert("错误代码:" + err.status); } }); } else { alert("请选择相应的设备!"); } } });
ASP.NET后台代码,如下图:
[WebMethod] public static string GetReport(string begin, string end, string param, string pagenum, string pagesize) { string userID = HttpContext.Current.Session["userID"].ToString(); string[] paras = param.Split(‘#‘); string measures = "‘"; for (int i = 0; i < paras.Length; i++) { measures = measures + "‘,‘" + paras[i]; } measures = measures + "‘"; string beginTime = begin + ":00"; string endTime = end + ":00"; int totalcount = 0; DataTable dt = drWater.BLL.infoManager.GetHistoryPageInfo(userID, beginTime, endTime, measures, pagenum, pagesize, out totalcount); List<json.dataItemJson> dataList = new List<json.dataItemJson>(); if (dt != null) { if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { json.dataItemJson item = new json.dataItemJson(); ...... item.status = dt.Rows[i]["status"].ToString(); dataList.Add(item); } } } json.dataListJson dataListJson = new json.dataListJson(); dataListJson.total = totalcount; dataListJson.rows = dataList; JavaScriptSerializer js = new JavaScriptSerializer(); js.MaxJsonLength = int.MaxValue; string str = js.Serialize(dataListJson); return str; }
经过多次调试,返回的json字符串,虽然符合easyUi datagrid的数据格式标准,但是载入数据的时候出错!
使用eval转化json字符串之后,解决该问题!
时间: 2024-10-26 15:55:56