grid++json页面数据传入
最近遇到一个问题,就是要用Grid++做页面数据报表打印,但是翻了Grid++文档就是没有直接从页面上传数据的,都是要加载txt文档,填写txt文档的url。自己尝试直接页面上传JSON数据到Grid++控件中,竟然成功了,分享一下。
静态框赋值Report.ParameterByName("ProductName").AsString="";
1.html页面代码如下:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>会员资料报表</title> <!--<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>--> <script src="CreateControl.js" type="text/javascript"></script> <script type="text/javascript"> function window_onload() { var Data = "{\"Detail\": [" + "{\"ProductID\": 2,\"ProductName\": \"牛奶\",\"UnitPrice\": 15.2,\"Quantity\": 20,\"Amount\": 304}," + "{\"ProductID\": 16,\"ProductName\": \"饼干\",\"UnitPrice\": 13.9,\"Quantity\": 35,\"Amount\": 486.5}," + "{\"ProductID\": 36,\"ProductName\": \"鱿鱼\",\"UnitPrice\": 15.2,\"Quantity\": 25,\"Amount\": 380}," + "{\"ProductID\": 59,\"ProductName\": \"苏澳奶酪\",\"UnitPrice\": 44,\"Quantity\": 30,\"Amount\": 1320}" + "]}"; var d = JSON.parse(Data); ReportViewer.Stop(); var Report = ReportViewer.Report; var Recordset = Report.DetailGrid.Recordset; Report.PrepareLoadData(); for(var i = 0; i < d.Detail.length; i++) { Recordset.Append(); Report.FieldByName("C1").AsString = d.Detail[i].ProductID; Report.FieldByName("C2").AsString = d.Detail[i].ProductName; Report.FieldByName("C3").AsString = d.Detail[i].UnitPrice; Report.FieldByName("C4").AsString = d.Detail[i].Quantity; Report.FieldByName("C5").AsString = d.Detail[i].Amount; Recordset.Post(); } ReportViewer.Start(); } </script> </head> <body onload="window_onload()"> <script type="text/javascript"> CreatePrintViewerEx("100%", "100%", "www.grf", "", true, ""); </script> </body> </html>
2.Grid++文件代码如下,txt保存改后缀为grf就可以:
{ "Version":"6.3.0.1", "Font":{ "Name":"宋体", "Size":90000, "Weight":400, "Charset":134 }, "Printer":{ }, "DetailGrid":{ "Recordset":{ "Field":[ { "Name":"C1" }, { "Name":"C2" }, { "Name":"C3" }, { "Name":"C4" }, { "Name":"C5" } ] }, "Column":[ { "Name":"Column1", "Width":2.38 }, { "Name":"Column2", "Width":2.38 }, { "Name":"Column3", "Width":2.38 }, { "Name":"Column4", "Width":2.38 }, { "Name":"Column5", "Width":2.38 } ], "ColumnContent":{ "Height":0.508, "ColumnContentCell":[ { "Column":"Column1", "DataField":"C1" }, { "Column":"Column2", "DataField":"C2" }, { "Column":"Column3", "DataField":"C3" }, { "Column":"Column4", "DataField":"C4" }, { "Column":"Column5", "DataField":"C5" } ] }, "ColumnTitle":{ "Height":0.508, "ColumnTitleCell":[ { "GroupTitle":false, "Column":"Column1", "TextAlign":"MiddleCenter", "Text":"ProductID" }, { "GroupTitle":false, "Column":"Column2", "TextAlign":"MiddleCenter", "Text":"ProductName" }, { "GroupTitle":false, "Column":"Column3", "TextAlign":"MiddleCenter", "Text":"UnitPrice" }, { "GroupTitle":false, "Column":"Column4", "TextAlign":"MiddleCenter", "Text":"Quantity" }, { "GroupTitle":false, "Column":"Column5", "TextAlign":"MiddleCenter", "Text":"Amount" } ] } }, "Parameter":[ { "Name":"ProductID" }, { "Name":"ProductName" }, { "Name":"UnitPrice" }, { "Name":"Quantity" }, { "Name":"Amount" } ], "ReportHeader":[ { "Name":"ReportHeader1", "Height":1.05833, "Control":[ { "Type":"StaticBox", "Name":"StaticBox1", "Left":5.79967, "Top":0.1905, "Width":2.794, "Height":0.804333, "Font":{ "Name":"宋体", "Size":156000, "Weight":400, "Charset":134 }, "Text":"会员集料" } ] } ] }
注意:grf文件中要赋值字段必须和js赋值一样多,不然就会报错显示不出来。
如果直接传入 json格式的话,加载数据用 ReportViewer.Report.LoadDataFromXML(jsonstr);
ReportViewer.Stop();
ReportViewer.Report.LoadDataFromXML(jsonstr);
//启动报表运行
ReportViewer.Start();
若jsonstr中用到动态加载的数据(记录集)那上述加载的不显示记录集,但是若是生成txt文件加载能显示记录集。不知道是什么什么原因引起的?