FR User Functions to control Delphi data?

ksihotaksihota Victoria, BC Canada
edited 6:58PM in FastReport 4.0
Is it possible to build Sort/Filter procedures in Delphi code that Sort/Filter a Delphi string grid, then add them to FR's functions and call them within an FR report to sort or filter the data coming into the report? The Sorting/Filter choices would be controlled using an FR dialog.
Currently I sort and filter before a report is created but I was wondering if this was also possible just using FR.

Thanks for any help/suggestions.
This was also posted on the FR newsgroup.

Kim

Comments

  • ksihotaksihota Victoria, BC Canada
    edited 6:58PM
    Does anyone know if this can be done?
    I tried it out but it did not appear to work. I thought maybe that a procedure acting on a Delphi component that was called by the FR dialog prior to getting the actual data might work.
    I don't want to spend more time trying to get it to work if it simply won't because of how FR and Delphi work together.

    Anyone?
  • edited July 2013
    I'm not sure that I understood your needs correctly. I think the only chance to use a data filter, parametrised by a FR dialog, is to send all data to the report and realize the filter inside the report script code area. Of course you can write a FR user control to place it on the report data page for this task, but not necessary.

    Best regards
  • ksihotaksihota Victoria, BC Canada
    edited 6:58PM
    Thanks Groffy,

    My data comes from a Delphi AdvStringGrid using an FrUserDataset. In order to display the report properly the table data needs to be sorted and filtered. This can be done within the Delphi program before the report is called but users are not necessarily 'with it' enough to do this step first. I was hoping that I could write a report that included a dialog in FR which would allow me to set up the proper sort and filters rather than through the application (as this is a specialized report.)

    I thought that since a Delphi function could be used within FR then possibly a Delphi procedure could be controlled from the FR dialog to return the sorted and filtered data. I guess the FRUserDataset has already been populated and will therefore not be updated by any procedure called within FR. I guess my only options are to either script the sorting and filtering within FR (looks rather complicated) or simply add a specialized 'Easy' button for the client that would perform the sorting and filtering first (easiest fix but not very elegant.) I will look into how I might code the report to see if there is something I could do re sorting/filters before I change the application.

    Thanks again for your help.

    Kim

    Groffy wrote: »
    I'm not sure that I understood your needs correctly. I think the only chance to use a data filter, parametrised by a FR dialog, is to send all data to the report and realize the filter inside the report script code area. Of course you can write a FR user control to place it on the report data page for this task, but not necessary.

    Best regards
  • edited 6:58PM
    For custom functions you can use something like this code.
    From the report script you can call MyIntToHexFunction.

    Best Regards,
    Cristian Peta
    implementation
    
    uses SysUtils, Classes,  fs_iinterpreter;
    
    type
    
      TFunctions = class(TfsRTTIModule)
      private
        function CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; var Params: Variant): Variant;
      public
        constructor Create(AScript: TfsScript); override;
      end;
    
    function MyIntToHexFunction(Value, Digits: Integer): String;
    begin
    //Here you will call your function
      Result := IntToHex(Value, Digits);
    end;
    
    { TFunctions }
    
    constructor TFunctions.Create;
    begin
      inherited Create(AScript);
      AScript.AddMethod('function MyIntToHexFunction(Value, Digits: Integer): String', CallMethod, 'ctConv', ' ');
    end;
    
    function TFunctions.CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; var Params: Variant): Variant;
    begin
      if MethodName = 'MYINTTOHEXFUNCTION' then
        Result := MyIntToHexFunction(Params[0], Params[1]);
    end;
    
    { TFunctiiFR }
    
    initialization
      fsRTTIModules.Add(TFunctions);
    
  • edited 6:58PM
    Nice :-) I never tried this FastScript stuff.


  • edited 6:58PM
    Now I read again and I see what you need.
    I have a function for filtering and one similar for sorting.
    dl is a TDataModule

    Best Regards,
    Cristian Peta
    implementation
    
    uses SysUtils, Classes,  fs_iinterpreter;
    
    type
    
      TFunctions = class(TfsRTTIModule)
      private
        function CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; var Params: Variant): Variant;
      public
        constructor Create(AScript: TfsScript); override;
      end;
    
    procedure SetTableFilter(TableName, Filter: String);
    var
      i: Integer;
    begin
      for i := 0 to dl.ComponentCount - 1 do begin
        if (dl.Components[i] is TfrxDBDataset)
        and SameText(TfrxDBDataset(dl.Components[i]).UserName, TableName) then begin
          TClientDataSet(TfrxDBDataset(dl.Components[i]).DataSet).Filter := Filter;
          TClientDataSet(TfrxDBDataset(dl.Components[i]).DataSet).Filtered := True;
          Exit;
        end;
      end;
        ShowMessage('There is no table "' + TableName + '"!');
    end;
    
    { TFunctions }
    
    constructor TFunctions.Create;
    begin
      inherited Create(AScript);
      AScript.AddMethod('procedure SetTableFilter(TableName, Filter: String)', CallMethod, 'WinArhi', ' Filtreaza tabelul TableName cu conditia din Filter.'#13#10' Campurile se separa cu;');
    end;
    
    function TFunctions.CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; var Params: Variant): Variant;
    begin
      if MethodName = 'SETTABLEFILTER' then SetTableFilter(Params[0], Params[1])
    end;
    
    initialization
      fsRTTIModules.Add(TFunctions);
    

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.