Jump to content

callback procedure!


UniterYazilim

Recommended Posts

Hi,

// I click this button.
procedure TframeRapor1.btnKopyalaCopyGroupClick(Sender: TObject);
begin
  try
  	// DB Update Process
    CopyGroupCalculate(MainDataModule.dsetGetICMUserSymbolCopyGroupValue.Value);

	// Refresh the grid with DB
    GetICMReportMaster(UniMainModule.UserID.ToString,
      String(MainDataModule.dsetAccountAccount.Value));

    isCalculateCopyGroup := true;
  except
    on E: Exception do
      ShowMessage(E.ClassName + ' error raised, with message : ' + E.Message);
  end;
end;

procedure TframeRapor1.CopyGroupCalculate(ICMUserSymbolCopyGroupValue: string);
begin
  MainDataModule.dsetGeneral.Close;
  MainDataModule.dsetGeneral.SQL.Clear;
  MainDataModule.dsetGeneral.SQL.Add
    ('EXEC spSetICMReportCalculateCopyGroup @ICMReportUserSymbolIDs = :ICMReportUserSymbolIDs');
  MainDataModule.dsetGeneral.ParamByName('ICMReportUserSymbolIDs').Value :=
    MainDataModule.dsetGetICMUserSymbolCopyGroupValue.Value;

  MainDataModule.dsetGeneral.Execute;
  MainDataModule.dsetGeneral.Close;
end;

procedure TframeRapor1.GetICMReportMaster(UserID: string; Account: string);
var
  isCorrectSymbol: Boolean;
  ICMReportMasterRecNo: Integer;
begin
  try
    if (NOT MainDataModule.dsetGetICMReportMaster.IsEmpty) then
    begin
      ICMReportMasterRecNo := MainDataModule.dsetGetICMReportMaster.RecNo;
    end;

    dbGridGetICMReportMaster.BeginUpdate;
    MainDataModule.dsetGetICMReportMaster.Active := false;
    MainDataModule.dsetGetICMReportMaster.SQL.Clear;
    MainDataModule.dsetGetICMReportMaster.SQL.Add
      ('EXEC spGetICMReportMaster @UserID = :UserID, @Account = :Account');
    MainDataModule.dsetGetICMReportMaster.ParamByName('UserID').Value :=
      UniMainModule.UserID;
    MainDataModule.dsetGetICMReportMaster.ParamByName('Account').Value :=
      MainDataModule.dsetAccountAccount.Value;
    MainDataModule.dsetGetICMReportMaster.Active := true;

    dbGridGetICMReportMaster.EndUpdate;

    selectedSymbol := '';
    selectedSide := '';

    MainDataModule.dsetGetICMReportMaster.RecNo := ICMReportMasterRecNo;
    dbGridShow(TGrid.GetICMReportMaster);
  except
    on E: Exception do
      ShowMessage(E.ClassName + ' error raised, with message : ' + E.Message);
  end;
end;

// OnAfterLoad
procedure TframeRapor1.dbGridGetICMReportMasterAfterLoad(Sender: TUniDBGrid);
begin
  if (isCalculateCopyGroup) then
  begin
    isCalculateCopyGroup := false;
    GetSelectRecordDbGridByICMReportUserSymbolID
      (MainDataModule.dsetGetICMUserSymbolCopyGroupValue.Value, 10);
  end;
end;

// Select DBGrid Row and Get Selected Row Info
procedure TframeRapor1.GetSelectRecordDbGridByICMReportUserSymbolID
  (ICMReportUserSymbolIDs: string; setTime: Integer);
var
  I: Integer;
  currentRecNo: Integer;
  List: TStrings;
  _RecNo: Integer;
  JScript: string;
  rowInfo: string;
begin
  if NOT MainDataModule.dsetGetICMUserSymbolCopyGroupValue.IsNull then
  begin
    dbGridGetICMReportMaster.BeginUpdate;
    List := TStringList.Create;
    ICMReportUserSymbolIDToRecNos := FindICMReportUserSymbolIDToRec
      (ICMReportUserSymbolIDs);

    JScript := CONCAT('setTimeout(function(){ console.log("setTime"); },',
      setTime.ToString, ');');

    JScript := CONCAT(JScript, 'setTimeout(function(){ ',
      dbGridGetICMReportMaster.JSName,
      '.getSelectionModel().deselectAll()}, 50);');
    with MainDataModule.dsetGetICMReportMaster do
    begin
      ExtractStrings([','], [], PChar(ICMReportUserSymbolIDToRecNos), List);
      for I := 0 to List.count - 1 do
      begin
        _RecNo := StrToInt(List[I]);
        if (I = (List.count - 1)) then
        begin
          rowInfo := CONCAT(rowInfo, GetGridInfoWithRecNo(_RecNo));
        end
        else
        begin
          rowInfo := CONCAT(rowInfo, GetGridInfoWithRecNo(_RecNo), #13#10);
        end;
        if (_RecNo > 0) then
        begin
          _RecNo := _RecNo - 1;
        end;
        JScript := CONCAT(JScript, 'setTimeout(function(){ ',
          dbGridGetICMReportMaster.JSName, '.getSelectionModel().select(',
          dbGridGetICMReportMaster.JSName, '.getStore().data.indexOfKey(',
          IntToStr(_RecNo) + '), true)}, 50);');
      end;
      memoClipboard.Lines.Clear;
      memoClipboard.Lines.Add(rowInfo);

      UniSession.SendResponse(JScript);
      // UniSession.AddJS(JScript);
      FreeAndNil(List);
    end;
    dbGridGetICMReportMaster.EndUpdate;
  end;
end;

// Get Row Info
function TframeRapor1.GetGridInfoWithRecNo(RecordNo: Integer): string;
var
  size: Integer;
  Symbol: string;
  Side: Char;
  Quantity: string;
  Price: string;
  str: string;
  currentRecNo: Integer;
begin
  try
    if NOT MainDataModule.dsetGetICMReportMaster.IsEmpty then
    begin
      if (MainDataModule.dsetGetICMReportMaster.RecordCount > 0) then
      begin
        dbGridGetICMReportMaster.BeginUpdate;
        with MainDataModule.dsetGetICMReportMaster do
        begin
          currentRecNo := RecNo;
          RecNo := RecordNo;
          if (Trim(FindField('SideKey').AsString) = '1') then
          begin
            Side := '+';
          end
          else
          begin
            Side := '-';
          end;

          if (NOT FindField('Symbol').IsNull) then
            Symbol := FindField('Symbol').AsString;

          if (NOT FindField('QtyDiff').IsNull) then
            Quantity := FindField('QtyDiff').AsString;

          if (NOT FindField('AvgPxDiff').IsNull) then
            Price := FindField('AvgPxDiff').AsString;

          size := 0;
          if (Length(Symbol) > size) then
            size := Length(Symbol);
          if (Length(Quantity) > size) then
            size := Length(Quantity);
          if (Length(Price) > size) then
            size := Length(Price);
          size := size * 2;

          str := format(CONCAT('%s', SetSpace(size - Length(Symbol)), '%s',
            SetSpace(size - Length(Side)), '%s',
            SetSpace(size - Length(Quantity)), '%s'),
            [Symbol, Side, Quantity, Price]);
          str := str.Replace('.E', '');
          RecNo := currentRecNo;
        end;
        dbGridGetICMReportMaster.EndUpdate;
      end;
    end;
  finally
    Result := str;
  end;
end;

 

When I run with the above code the grid gets stuck in Loading mode!

what an I doing wrong?

Thank you for your help.
Best regards.

loading.png.6c27d1bcfa6e688c752e3c8694bacca5.png

Link to comment
Share on other sites

Hi,

Sample is attached.

 

Operation:

1. Choose the LookUpComboBox record
2. Select records on dbgrid when doing step 1
3. Press the Calculate button { Calculate button actions: DBUpdate, Refresh Grid, Selected Row (selected record in step 2) }

procedure TMainForm.dbGridGetICMReportMasterAfterLoad(Sender: TUniDBGrid);
begin
  if (isCalculateCopyGroup) then
  begin
    isCalculateCopyGroup := false;


    // dbgrid loading mode hang when this code working.

    // GetSelectRecordDbGridByICMReportUserSymbolID
    // (dmMain.dsetGetICMUserSymbolCopyGroupValue.Value);
  end;
end;

Thanks.

UniDBGridExample.zip

Link to comment
Share on other sites

On 2/18/2019 at 1:24 PM, UniterYazilim said:

Hi,
Thanks for your help @Sherzod.

I can solve the UniDBGrid -> OnAfterLoad event by basing a condition for this example.
But there are other similar situations. How can I build the callback structure for such cases?

procedure TframeRapor1.dbGridGetICMReportMasterAfterLoad(Sender: TUniDBGrid);
begin
  if (isCalculateCopyGroup) then
  begin
    isCalculateCopyGroup := false;

	// dbgrid loading mode hang when this below code working
    // UniSession.SendResponse('console.log(''1'');');
   
	// no problem this below code working
    // UniSession.AddJS('console.log(''1'');');
  end;
end;

What is the different?

why dbgrid loading mode hang when use UniSession.SendResponse?

thanks.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...