demodb.lpr 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. program demoxhr;
  2. uses SysUtils, JS, Web, DB, JSonDataset, ExtJSDataset, strutils, DBConst;
  3. Type
  4. { TForm }
  5. TForm = Class
  6. XHR : TJSXMLHttpRequest;
  7. Table,
  8. Panel,
  9. PanelContent,
  10. Button : TJSElement;
  11. DS : TExtJSJSONObjectDataSet;
  12. function onLoad(Event: TEventListenerEvent): boolean;
  13. Constructor create;
  14. function CreateTable: TJSElement;
  15. private
  16. function ButtonClick(Event: TJSMouseEvent): boolean;
  17. function CreateRow(AName: String; APopulation: NativeInt): TJSElement;
  18. end;
  19. function TForm.CreateRow(AName : String; APopulation : NativeInt) : TJSElement;
  20. Var
  21. C : TJSElement;
  22. begin
  23. Result:=document.createElement('TR');
  24. C:=document.createElement('TD');
  25. Result.Append(C);
  26. C.appendChild(Document.createTextNode(AName));
  27. C:=document.createElement('TD');
  28. Result.Append(C);
  29. C.AppendChild(document.createTextNode(IntToStr(APopulation)));
  30. end;
  31. function TForm.CreateTable : TJSElement;
  32. Var
  33. TH,R,H : TJSElement;
  34. begin
  35. Result:=document.createElement('TABLE');
  36. Result.className:='table table-striped table-bordered table-hover table-condensed';
  37. TH:=document.createElement('THEAD');
  38. Result.Append(TH);
  39. R:=document.createElement('TR');
  40. TH.Append(R);
  41. H:=document.createElement('TH');
  42. R.Append(H);
  43. H.AppendChild(document.createTextNode('Name'));
  44. H:=document.createElement('TH');
  45. R.Append(H);
  46. H.AppendChild(document.createTextNode('Population'));
  47. end;
  48. function TForm.onLoad(Event: TEventListenerEvent): boolean;
  49. var
  50. i : integer;
  51. C,J : TJSObject;
  52. N,TB : TJSElement;
  53. begin
  54. console.log('Result of call ',xhr.Status);
  55. { While (PanelContent.childNodes.length>0) do
  56. PanelContent.removeChild(PanelContent.childNodes.item(PanelContent.childNodes.length-1));}
  57. if (xhr.status = 200) then
  58. begin
  59. J:=TJSJSON.parseObject(xhr.responseText);
  60. DS.Metadata:=TJSObject(J.Properties['metaData']);
  61. DS.Rows:=TJSArray(J.Properties['Data']);
  62. DS.Open;
  63. Table:=CreateTable;
  64. Document.Body.append(Table);
  65. TB:=document.createElement('TBODY');
  66. Table.Append(TB);
  67. While not DS.EOF do
  68. begin
  69. TB.Append(CreateRow(DS.FieldByName('Name').AsString,DS.FieldByName('Population').AsInteger));
  70. DS.Next;
  71. end;
  72. end
  73. else
  74. begin
  75. N:=Document.CreateElement('div');
  76. N.appendChild(Document.createTextNode('Failed to load countries: '+IntToStr(xhr.Status)));
  77. PanelContent.append(N);
  78. end;
  79. Result := True;
  80. end;
  81. function TForm.ButtonClick(Event: TJSMouseEvent): boolean;
  82. begin
  83. xhr:=TJSXMLHttpRequest.New;
  84. xhr.addEventListener('load', @OnLoad);
  85. xhr.open('GET', 'countries.json', true);
  86. xhr.send;
  87. Result:=true;
  88. end;
  89. constructor TForm.create;
  90. begin
  91. Panel:=document.createElement('div');
  92. // attrs are default array property...
  93. Panel['class']:='panel panel-default';
  94. PanelContent:=document.createElement('div');
  95. PanelContent['class']:='panel-body';
  96. Button:=document.createElement('input');
  97. Button['id']:='Button1';
  98. Button['type']:='submit';
  99. Button.className:='btn btn-default';
  100. Button['value']:='Fetch countries';
  101. TJSHTMLElement(Button).onclick:=@ButtonClick;
  102. document.body.appendChild(panel);
  103. Panel.appendChild(PanelContent);
  104. PanelContent.appendChild(Button);
  105. DS:=TExtJSJSONObjectDataSet.Create(Nil);
  106. end;
  107. begin
  108. TForm.Create;
  109. end.