Interactive reports

Top  Previous  Next

Interactive reports can respond to mouse-clicks on any specified report objects in the preview window. For example, clicking on the data line may run a new report with detailed data for the selected line.

 

Any report can be made to be interactive by creating a “TfrxReport.OnClickObject” event handler, for example:

 

Pascal:

 

procedure TForm1.frxReport1ClickObject(Page: TfrxPage; View: TfrxView;

                                      Button: TMouseButton;

                                      Shift: TShiftState;

                                      var Modified: Boolean);

begin

 if View.Name = 'Memo1' then

   ShowMessage('Memo1 contents:' + #13#10 + TfrxMemoView(View).Text);

 if View.Name = 'Memo2' then

 begin

   TfrxMemoView(View).Text := InputBox('Edit', 'Edit Memo2 text:',

                                       TfrxMemoView(View).Text);

   Modified := True;

 end;

end;

 

C++:

 

void __fastcall TForm1::frxReport1ClickObject(TfrxView *Sender,

                                             TMouseButton Button,

                                             TShiftState Shift,

                                             bool &Modified)

{

 TfrxMemoView * Memo;

 if(Memo =  dynamic_cast <TfrxMemoView *> (Sender))

 {

   if(Memo->Name == "Memo1")

     ShowMessage("Memo1 contents:\n\r" + Memo->Text);

   if(Memo->Name == "Memo2")

   {

     Memo->Text = InputBox("Edit", "Edit Memo2 text:", Memo->Text);

      Modified = true;

   }

 }

}

 

In the “OnClickObject” handler, you can do the following:

- modify the contents of an object or a page passed into the handler (the “Modified” output parameter should be set so that the changes are implemented)

- call the “TfrxReport.PrepareReport” method to rebuild a report

 

In the example, clicking on the “Memo1” object results in the display of a message showing the contents of the object. When clicking on the “Memo2” object a dialogue is displayed where the contents of this object can be changed. Setting the “Modified” flag to “True” makes the change permanent.

 

In the same way other responses can be made, such as running a new report. PLEASE NOTE : in FastReport v3 and above the TfrxReport component can only display one report in the preview window (unlike FastReport version 2.x). So either the new report must be run in a separate TfrxReport object or the current report must be erased from the current TfrxReport object.

 

The user can be given a visual clue to clickable objects by changing the mouse cursor when it passes over the objects in the preview window. To do this, select the object in the report designer and set its cursor property to something other than crDefault.

 

There is one more concern with making objects clickable. Simple reports can test either the object’s name or the object's content to evoke a response. However, this is not always so straight forward in more complicated reports. For example, in a master-detail report when clicking on the “Memo1” object having content '12', where does the data come from? The primary key identifies this line unambiguously. FastReport provides a “TagStr” property for storing any useful string data (in our case the primary key).

 

Let's illustrate this with a report included in the FastReportDemo.exe example - a 'Simple list' demo. It is a list of company clients, with data such as “client name”, “address”, “contact person” etc. The data source is the “Customer.db” table from the DBDEMOS demo database. This table has a primary key, the “CustNo” field, which is not displayed in the report output. Our problem is to determine which record is referred to when clicking on any object in the finished report. The value of the primary key is required, which can be entered as an expression into the “TagStr” property of all the objects in the MasterData band:

 

[Customers."CustNo"]

 

When a report is being built the “TagStr” property’s contents are evaluated in the same way as for the contents of text objects, that is values are substituted for named variables and expressions (enclosed in square brackets) are evaluated. That is why the “TagStr” property of the objects lying on the MasterData band contains values such '1005' or '2112', etc. after report building. A simple conversion from a string to an integer gives us the value of the primary key from which the required record can be found.

 

If the primary key is composite (i.e. it contains more than one field) the “TagStr” property’s contents may be the following:

 

[Table1."Field1"];[Table1."Field2"]

 

After building the report the “TagStr” property may contain a string value of '1000;1', from which the individual field values can be fairly simply extracted.