unit MainClient;

interface

uses
  System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
  WEBLib.Forms, WEBLib.Dialogs, Data.DB, XData.Web.JsonDataset, WEBLib.DB,
  XData.Web.Dataset, XData.Web.Connection, Vcl.Grids, WEBLib.DBCtrls,
  WEBLib.ExtCtrls, Vcl.Controls, Vcl.StdCtrls, WEBLib.StdCtrls;

type
  TForm3 = class(TWebForm)
    WebPanel1: TWebPanel;
    WebPanel2: TWebPanel;
    WebSplitter1: TWebSplitter;
    WebPanel3: TWebPanel;
    WebPanel4: TWebPanel;
    WebDBGrid2: TWebDBGrid;
    XDataWebConnection1: TXDataWebConnection;
    wdsProvince: TXDataWebDataSet;
    wdsSchool: TXDataWebDataSet;
    dsProvince: TWebDataSource;
    dsSchool: TWebDataSource;
    wdsProvinceSchools: TXDataWebDatasetField;
    wdsProvinceprID: TIntegerField;
    wdsProvinceprName: TStringField;
    wdsSchoolscID: TIntegerField;
    wdsSchoolscName: TStringField;
    WebDBGrid1: TWebDBGrid;
    WebDBNavigator1: TWebDBNavigator;
    WebDBNavigator2: TWebDBNavigator;
    WebDBEdit1: TWebDBEdit;
    WebButton1: TWebButton;
    WebButton2: TWebButton;
    WebDBEdit2: TWebDBEdit;
    WebButton3: TWebButton;
    WebButton4: TWebButton;
    wdsSchoolCalcField: TStringField;
    procedure WebFormCreate(Sender: TObject);
    procedure wdsProvinceAfterScroll(DataSet: TDataSet);
    procedure WebButton1Click(Sender: TObject);
    procedure WebButton2Click(Sender: TObject);
    procedure WebButton3Click(Sender: TObject);
    procedure WebButton4Click(Sender: TObject);
    procedure wdsProvinceNewRecord(DataSet: TDataSet);
    procedure wdsSchoolNewRecord(DataSet: TDataSet);
    procedure wdsProvinceAfterApplyUpdates(Sender: TDataSet; Info: TResolveResults);
    procedure wdsSchoolCalcFields(DataSet: TDataSet);
    procedure wdsSchoolAfterDelete(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.wdsProvinceAfterApplyUpdates(Sender: TDataSet; Info: TResolveResults);
begin
  wdsProvince.Refresh;
end;

procedure TForm3.wdsProvinceAfterScroll(DataSet: TDataSet);
begin
  wdsSchool.Close;
  if dsProvince.State <> dsInsert then
  begin
    wdsSchool.SetJsonData(wdsProvinceSchools.Value);
    wdsSchool.Open;
  end;
end;

procedure TForm3.wdsProvinceNewRecord(DataSet: TDataSet);
begin
  wdsProvinceSchools.Value := TJSArray.new;
end;

procedure TForm3.wdsSchoolAfterDelete(DataSet: TDataSet);
var
  I: Integer;
  Arr: TJSArray;
begin
  Arr := TJSArray(wdsProvinceSchools.Value);
  I := 0;
  while I < Arr.Length do
    if JS.IsUndefined(Arr[I]) then
      Arr.splice(I, 1)
    else
      Inc(I);
end;

procedure TForm3.wdsSchoolCalcFields(DataSet: TDataSet);
begin
  try
    wdsSchool.FieldByName('CalcField').AsString := 'Test';
  except
  end;
end;

procedure TForm3.wdsSchoolNewRecord(DataSet: TDataSet);
begin
  TJSObject(wdsSchool.CurrentData)['scProvinceRef@xdata.ref'] :=
    Format('Province(%d)', [wdsProvinceprID.AsInteger]);
end;

procedure TForm3.WebButton1Click(Sender: TObject);
begin
  wdsProvince.Post;
end;

procedure TForm3.WebButton2Click(Sender: TObject);
begin
  wdsProvince.ApplyUpdates;
end;

procedure TForm3.WebButton3Click(Sender: TObject);
begin
  wdsSchool.Post;
end;

procedure TForm3.WebButton4Click(Sender: TObject);
begin
  wdsSchool.ApplyUpdates;
end;

procedure TForm3.WebFormCreate(Sender: TObject);
begin
  xDataWebConnection1.Connected := True;
  wdsProvince.Load;
end;

end.
