This example shows how to set report variables on the server-side. First, you need to add the Stimulsoft libraries and scripts, required for the component to work. All code should be added in the
<head>
block of the HTML page:
<?php
require_once 'vendor/autoload.php';
?>
...
<?php
$js = new \Stimulsoft\StiJavaScript(\Stimulsoft\StiComponentType::Viewer);
$js->renderHtml();
?>
Next, in the
<script>
block, create and configure an event handler:
<script type="text/javascript">
<?php
$handler = new \Stimulsoft\StiHandler();
$handler->renderHtml();
Next, create the viewer with the necessary options and define the
onPrepareVariables
event. If value set to
true
, this event will be passed to the server-side event handler:
$options = new \Stimulsoft\Viewer\StiViewerOptions();
$options->appearance->fullScreenMode = true;
$viewer = new \Stimulsoft\Viewer\StiViewer($options);
$viewer->onPrepareVariables = true;
Next, create and load a report. The
loadFile()
method does not load the report object on the server side, it only generates the necessary JavaScript code. The report will be loaded into a JavaScript object on the client side:
$report = new \Stimulsoft\Report\StiReport();
$report->loadFile('reports/Variables.mrt');
$viewer->report = $report;
?>
Next, render the necessary JavaScript code and visual HTML part of the component, and close the
</script>
block. The rendered code will be placed inside the specified HTML element:
function onLoad() {
<?php
$viewer->renderHtml('viewerContent');
?>
}
</script>
...
<body onload="onLoad();">
<div id="viewerContent"></div>
</body>
Finally, process report variables on the server-side in the
onPrepareVariables()
method. You can set the values of the report variables, the value types must match the original types. If the variable contained an expression, the already calculated value will be passed. The new values will be passed to the report generator. By default, all server-side events are located in the
handler.php
file:
$handler->onPrepareVariables = function ($args)
{
/*
$args->variables['VariableString']->value = 'Value from Server-Side';
$args->variables['VariableDateTime']->value = '2020-01-31 22:00:00';
$args->variables['VariableStringRange']->value->from = 'Aaa';
$args->variables['VariableStringRange']->value->to = 'Zzz';
$args->variables['VariableStringList']->value[0] = 'Test';
$args->variables['VariableStringList']->value = ['1', '2', '2'];
$args->variables['NewVariable'] = ['value' => 'New Value'];
*/
// Values for 'Variables.mrt' report template
if (count($args->variables) > 0) {
$args->variables['Name']->value = 'Maria';
$args->variables['Surname']->value = 'Anders';
$args->variables['Email']->value = Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein.';
$args->variables['Address']->value = 'Obere Str. 57, Berlin';
$args->variables['Sex']->value = false;
$args->variables['BirthDay']->value = '1982-03-20 00:00:00';
}
return StiResult::success();
};
Auf dem Screenshot unten Sie können das Ergebnis des Beispiel-Codes ansehen: