visit
This blog will show you how to:
First off, we will create a new ActiveReports 16 JSViewer Core MVC Application.
<div style="width: 100%; overflow-x: hidden">
<div style="float:left; width: 125pt" class="main-nav navbar">
<div id='list-heading'>Select Country</div>
<ul id="countriesList" class="nav navbar-nav"></ul>
</div>
<div style="float:right;width:calc(100% - 125pt)" id="viewerContainer">
</div>
</div>
Then, change the on load event of the body to populate the countriesList.
<body onload="populateList()">
function populateList() {
let countriesList = document.getElementById("countriesList");
let oReq = new XMLHttpRequest();
oReq.onload = function () {
var lookup = {};
var countriesObj = [];
fetch('//demodata.grapecity.com/northwind/api/v1/Customers')
.then(res => res.json())
.then((out) => {
for (var item, i = 0; item = out[i++];) {
var country = item.country;
if (!(country in lookup)) {
lookup[country] = 1;
countriesObj.push(country);
}
}
countriesObj.sort();
for (let i = 0; i < countriesObj.length; i++) {
const countryName = countriesObj[i];
const countries = document.createElement('li');
countries.className = 'countriesList_item';
const countryItem = document.createElement('span');
countryItem.innerText = countryName;
countries.appendChild(countryItem);
countriesList.appendChild(countries);
countries.addEventListener('click', function () {
loadViewer(countryName);
});
}
})
.catch(err => { throw err });
}
oReq.open("get", "reports", false);
oReq.send();
}
At this point, we’ve created the countriesList element, populated it, and added an event listener to pass the selected country from this list to the loadViewer() function. The loadViewer function takes the user input, a parameter, and passes it to the report to display filtered data:
let viewer=null;
function loadViewer(country) {
if (viewer == null) {
viewer = GrapeCity.ActiveReports.JSViewer.create({
element: '#viewerContainer',
reportID: "MyReport",
reportParameters: [{ name: 'selectedCountry', values: [country] }]
});
}
else { viewer.openReport("MyReport", [{name: 'selectedCountry', values:[country]}])}
}
public PageReport rdlReport = new PageReport();
rdlReport.Report.PaperOrientation = GrapeCity.ActiveReports.PageReportModel.PaperOrientation.Landscape;
rdlReport.Report.PageHeight = "8in";
rdlReport.Report.PageWidth = "11in";
//Page Header
GrapeCity.ActiveReports.PageReportModel.PageHeaderFooter headers = new GrapeCity.ActiveReports.PageReportModel.PageHeaderFooter();
rdlReport.Report.PageHeader = headers;
headers.Height = ".5in";
headers.PrintOnFirstPage = true;
headers.PrintOnLastPage = true;
//Page Footer
GrapeCity.ActiveReports.PageReportModel.PageHeaderFooter footer = new GrapeCity.ActiveReports.PageReportModel.PageHeaderFooter();
rdlReport.Report.PageFooter = footer;
footer.Height = ".5in";
footer.PrintOnFirstPage = true;
footer.PrintOnLastPage = true;
GrapeCity.ActiveReports.PageReportModel.TextBox reportTitle = new GrapeCity.ActiveReports.PageReportModel.TextBox()
{
Name = "Report Title",
Value = "=\"List of Customers in \" & Parameters!selectedCountry.Value",
Height = "0.5in",
Width = "5in",
Top = "0in",
Left = "0.5in",
Style = { TextAlign = "Left", FontSize = "18pt", FontWeight = "Bold" }
};
GrapeCity.ActiveReports.PageReportModel.TextBox pageNumber = new GrapeCity.ActiveReports.PageReportModel.TextBox();
pageNumber.Name = "pNumber";
pageNumber.Height = ".5cm";
pageNumber.Width = "1cm";
pageNumber.Left = "25cm";
pageNumber.Top = "0cm";
pageNumber.Value = "=Globals!PageNumber";
footer.ReportItems.Add(pageNumber);
rdlReport.Report.Body.ReportItems.Add(reportTitle);
GrapeCity.ActiveReports.PageReportModel.Table customersTable = new GrapeCity.ActiveReports.PageReportModel.Table()
{
Name = "CustomersTable",
Top = "0.75in",
Left = "0.5in",
DataSetName = "MyDataSet"
};
//Creating table header
customersTable.Header = new GrapeCity.ActiveReports.PageReportModel.Header();
customersTable.Header.TableRows.Add(new GrapeCity.ActiveReports.PageReportModel.TableRow() { Height = ".4in" });
var customerHeader = customersTable.Header.TableRows[0].TableCells;
//First cell in the table header
customerHeader.Add(new GrapeCity.ActiveReports.PageReportModel.TableCell());
customerHeader[0].ReportItems.Add(new GrapeCity.ActiveReports.PageReportModel.TextBox()
{
Name = "Company Name",
Value = "Company Name",
Style = { BorderStyle = { Bottom = "Solid" },
VerticalAlign = "Middle",
PaddingLeft="3pt",
TextAlign = "Left",
BackgroundColor = "WhiteSmoke",
FontWeight = "Bold" }
});
customersTable.TableColumns.Add(new GrapeCity.ActiveReports.PageReportModel.TableColumn() { Width = "2.35in" });
Next, add a details row to the table. You only need one detail row, which will repeat as many times as there are records. Creating a details row follows the same procedure as creating the header row. But first, create the customerDetails cell that will represent a cell in the table details row:
//Detail Row
customersTable.Details.TableRows.Clear();
customersTable.Details.TableRows.Add(new GrapeCity.ActiveReports.PageReportModel.TableRow() { Height = ".3in" });
var customerDetails = customersTable.Details.TableRows[0].TableCells;
//First cell in the Details row
customerDetails.Add(new GrapeCity.ActiveReports.PageReportModel.TableCell());
customerDetails[0].ReportItems.Add(new GrapeCity.ActiveReports.PageReportModel.TextBox()
{
Name = "CompanyNameBox",
Value = "=Fields!CompanyName.Value",
Width = "2.35in",
Style = { BorderColor = { Bottom = "WhiteSmoke" }, BorderStyle = { Bottom = "Solid" }, TextAlign = "Left", PaddingLeft="3pt", VerticalAlign="Middle"}
});
//Table footer
customersTable.Footer = new GrapeCity.ActiveReports.PageReportModel.Footer();
customersTable.Footer.TableRows.Add(new GrapeCity.ActiveReports.PageReportModel.TableRow() { Height = ".5in" });
var customerFooter = customersTable.Footer.TableRows[0].TableCells;
//First cell in the footer
customerFooter.Add(new GrapeCity.ActiveReports.PageReportModel.TableCell());
customerFooter[0].ReportItems.Add(new GrapeCity.ActiveReports.PageReportModel.TextBox()
{
Name = "Company Name",
Value = "=\"Total Customer Count: \" & CountRows()",
Style = {
VerticalAlign ="Middle",
PaddingLeft="3pt",
TextAlign = "Left",
FontWeight = "Bold" }
});
customersTable.TableColumns.Add(new GrapeCity.ActiveReports.PageReportModel.TableColumn() { Width = "2.35in" });
rdlReport.Report.Body.ReportItems.Add(customersTable);
You can either create this parameter in such a way as to prompt the user for input before rendering the report or hide the prompt altogether. By hiding the parameter, the user is not asked to enter a parameter value. In our case, since the user is already making a selection on our web page, outside of the viewer component, we should create this as a hidden parameter.
//Create a hidden parameter
GrapeCity.ActiveReports.PageReportModel.ReportParameter selectedCountry = new GrapeCity.ActiveReports.PageReportModel.ReportParameter()
{
Name = "selectedCountry",
Prompt = "Select a country",
Hidden = true
};
//Add the parameter to the report
rdlReport.Report.ReportParameters.Add(selectedCountry);
rdlReport.Report.DataSources.Add(myDataSource());
rdlReport.Report.DataSets.Add(myDataSet());
private GrapeCity.ActiveReports.PageReportModel.DataSource myDataSource()
{
GrapeCity.ActiveReports.PageReportModel.DataSource myDS = new GrapeCity.ActiveReports.PageReportModel.DataSource();
myDS.Name = "MyDataSource";
myDS.ConnectionProperties.DataProvider = "JSON";
myDS.ConnectionProperties.ConnectString = "jsondoc=//demodata.grapecity.com/northwind/api/v1/Customers";
return myDS;
}
private GrapeCity.ActiveReports.PageReportModel.IDataSet myDataSet()
{
GrapeCity.ActiveReports.PageReportModel.DataSet myDSet = new GrapeCity.ActiveReports.PageReportModel.DataSet();
GrapeCity.ActiveReports.PageReportModel.Query myQuery = new GrapeCity.ActiveReports.PageReportModel.Query();
myDSet.Name = "MyDataSet";
myQuery.DataSourceName = "MyDataSource";
myQuery.CommandText = "$.[*]";
myDSet.Query = myQuery;
//Create individual fields
GrapeCity.ActiveReports.PageReportModel.Field country = new GrapeCity.ActiveReports.PageReportModel.Field("country", "country", null);
GrapeCity.ActiveReports.PageReportModel.Field compName = new GrapeCity.ActiveReports.PageReportModel.Field("companyName", "companyName", null);
GrapeCity.ActiveReports.PageReportModel.Field contactName = new GrapeCity.ActiveReports.PageReportModel.Field("contactName", "contactName", null);
GrapeCity.ActiveReports.PageReportModel.Field address = new GrapeCity.ActiveReports.PageReportModel.Field("address", "address", null);
GrapeCity.ActiveReports.PageReportModel.Field cityName = new GrapeCity.ActiveReports.PageReportModel.Field("city", "city", null);
GrapeCity.ActiveReports.PageReportModel.Field phone = new GrapeCity.ActiveReports.PageReportModel.Field("phone", "phone", null);
//Create filter to use Parameter
GrapeCity.ActiveReports.PageReportModel.Filter countryFilter = new GrapeCity.ActiveReports.PageReportModel.Filter
{
FilterExpression = "=Fields!country.Value",
FilterValues = { "=Parameters!selectedCountry.Value" }
};
//Add fields and filter to the dataset
myDSet.Fields.Add(country);
myDSet.Fields.Add(compName);
myDSet.Fields.Add(contactName);
myDSet.Fields.Add(address);
myDSet.Fields.Add(cityName);
myDSet.Fields.Add(phone);
myDSet.Filters.Add(countryFilter);
return myDSet;
}
app.UseReporting(settings =>
{
settings.UseEmbeddedTemplates(EmbeddedReportsPrefix, Assembly.GetAssembly(GetType()));
settings.UseCustomStore(GetReport);
settings.UseCompression = true;
});
private object GetReport(string arg)
{
if (!IsReport(arg))
return null;
Reports.ReportDefinition reportDef = new Reports.ReportDefinition();
return reportDef.rdlReport;
}