User Data in Reports

This example shows how to use the user data in the report and register it from code.

To use the XML data, first you should load XML file and convert it to the XML object. You can use the StiTypeConverter.stringToXml() static method for this purpose. After this action you can register XML data it the report using the regData() method of the report object. As parameters, you should pass the data source name and alias, and this XML object:

private var reportString: String;
private var xmlData: XMLNode;

private function onInitialize(): void
{
...

// Load XML data file
var request2: URLRequest = new URLRequest("reports/data/Demo.xml");
var loader2: URLLoader = new URLLoader();
loader2.addEventListener(Event.COMPLETE, onXmlLoadComplete);
loader2.load(request2);
}

private function onXmlLoadComplete(event: Event): void
{
var loader: URLLoader = event.target as URLLoader;
var data: String = loader.data as String;

xmlData = StiTypeConverter.stringToXml(data);
}

private function onRegDataXmlClick(): void
{
// Create new report object
var report: StiReport = new StiReport();
// Load report from XML string
report.loadReportFromString(reportString);
// Registration of XML data with same name "Demo"
report.regData("Demo", "Demo", xmlData);
// Show report in Viewer dialog window
report.showDialog();
}


Also you can use the DataSet, DataTable, DataColumn classes to create the user data set. For example create a Customers data table with one CustomerName data column and three data rows. After creation you can use the regData() method of the report object:

private function onRegDataSetClick(): void
{
// Create new Data Table
var table: DataTable = new DataTable("Customers");
// Create new Data Column
var column: DataColumn = new DataColumn("CustomerName", StorageType.StringType);
// Add Data Column to Data Table
table.columns.add(column);

// Create new Data Rows
table.addNewRow().setValue("CustomerName", "John Smith");
table.addNewRow().setValue("CustomerName", "Antonio Moreno");
table.addNewRow().setValue("CustomerName", "Elizabeth Brown");

// Create new Data Set
var dataSet: DataSet = new DataSet("DataSetName");
// Add Data Table to Data Set
dataSet.tables.add(table);

// Create new report object
var report: StiReport = new StiReport();
// Registration of user Data Set
report.regData("DataSetName", "DataSetName", dataSet);
// Synchronize report dictionary
report.dictionary.synchronize();
// Design report in Designer dialog window
report.designDialog();
}


In the screenshot below you can see the result of the sample code.

User Data in Reports