在线设计器往往是集成到项目中,且是为不懂技术的业务人员使用的,因此开发人员不期望用户来自己配置数据源,而是在打开设计器时直接配置好数据源他们就可以直接设计即可。
本篇文章就来分享下如何实现新建报表时内置数据源。
1. 打开 View-Design-Index.cshtml 页面,在代码后面调用以下接口,默认新建时创建模板报表:
- GrapeCity.ActiveReports.WebDesigner.renderApplication(designerId, designerOptions);
- // 在Create时候为报表打开模板文件,方便为新建报表绑定数据源
- GrapeCity.ActiveReports.WebDesigner.api.createReport({
- templateInfo: {
- id: "OrganizationBrandedReport.rdlx",
-
- },
- });
复制代码 2. 修改 Services中的ITemplatesService.cs
- public interface ITemplatesService
- {
- TemplateInfo[] GetTemplatesList();
- byte[] GetTemplate(string id,string ds);
- TemplateThumbnail GetTemplateThumbnail(string id);
- }
复制代码 3. 修改 Implementation 中的FileSystemTemplate.cs
- public byte[] GetTemplate(string id,string ds)
- {
- var fullPath = Path.Combine(_rootFolder.FullName, id);
- if (!File.Exists(fullPath)) throw new FileNotFoundException();
- var templateXml = File.ReadAllBytes(fullPath);
- var template = ReportConverter.FromXML(templateXml);
-
- // 为Template绑定数据源
- AddDataSetDataSource(template,ds);
- var thumbnail = template.EmbeddedImages.FirstOrDefault(i => i.Name == templateThumbnailName);
- if (thumbnail != null) template.EmbeddedImages.Remove(thumbnail);
- var templateJson = ReportConverter.ToJson(template);
- return templateJson;
- }
- // 为报表创建数据源,并指定Json 字串
- public static Report AddDataSetDataSource(Report report, string ds)
- {
- // create DataSource for the report
- DataSource dataSource = new DataSource();
- dataSource.Name = "TESTData";
- dataSource.ConnectionProperties.DataProvider = "JSON";
- string datasource1 = "jsondoc="+ds;
- dataSource.ConnectionProperties.ConnectString = datasource1;
- //Create DataSet with specified query and load database fields to the DataSet
- DataSet dataSet = new DataSet();
- Query query = new Query();
- dataSet.Name = "Sample DataSet";
- query.DataSourceName = "TESTData";
- query.CommandText = ExpressionInfo.FromString("$.[*]");
- dataSet.Query = query;
- String[] fieldsList = new String[] { "postId", "id", "name","body" };
- foreach (string fieldName in fieldsList)
- {
- Field field = new Field(fieldName, fieldName, null);
- dataSet.Fields.Add(field);
- }
- //create report definition with specified DataSet and DataSource
- report.DataSources.Add(dataSource);
- report.DataSets.Add(dataSet);
- return report;
- }
复制代码 4. 修改 Contollers里面的 DesignController.cs Create方法
- public ActionResult Create([FromQuery(Name = "datasource")] string datasource1,[FromQuery(Name = "theme")] string theme = "blue")
- {
-
- TempData["datasource"] = datasource1;// 保存Query中的DataSource String
- ViewBag.Theme = theme;
- return View("Index");
- }
复制代码
5. 修改 Contollers里面的 TemplatesController.cs GetTemplate方法
- if (string.IsNullOrWhiteSpace(id)) return BadRequest();
- //获取前台传递的 datasource String
- var datasource01 = TempData["datasource"] as string;
- var template = templatesService.GetTemplate(id, datasource01);// 将DataSource String 传递给模板,对应的要修改 ItempaletService 的GetTemplate方法增加参数2
- return File(template, "application/json");
复制代码
|
|