demoxhr.lpr 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. program demoxhr;
  2. uses sysutils, js, web, ajax;
  3. Type
  4. { TForm }
  5. TForm = Class
  6. XHR : TJSXMLHttpRequest;
  7. Table,
  8. Panel,
  9. PanelContent,
  10. Button : TJSElement;
  11. function onLoad(Event: TEventListenerEvent): boolean;
  12. Constructor create;
  13. function CreateTable: TJSElement;
  14. private
  15. function ButtonClick(Event: TJSMouseEvent): boolean;
  16. function CreateRow(AName: String; APopulation: NativeInt): TJSElement;
  17. end;
  18. function TForm.CreateRow(AName : String; APopulation : NativeInt) : TJSElement;
  19. Var
  20. C : TJSElement;
  21. begin
  22. Result:=document.createElement('TR');
  23. C:=document.createElement('TD');
  24. Result.Append(C);
  25. C.appendChild(Document.createTextNode(AName));
  26. C:=document.createElement('TD');
  27. Result.Append(C);
  28. C.AppendChild(document.createTextNode(IntToStr(APopulation)));
  29. end;
  30. function TForm.CreateTable : TJSElement;
  31. Var
  32. TH,R,H : TJSElement;
  33. begin
  34. Result:=document.createElement('TABLE');
  35. Result.className:='table table-striped table-bordered table-hover table-condensed';
  36. TH:=document.createElement('THEAD');
  37. Result.Append(TH);
  38. R:=document.createElement('TR');
  39. TH.Append(R);
  40. H:=document.createElement('TH');
  41. R.Append(H);
  42. H.AppendChild(document.createTextNode('Name'));
  43. H:=document.createElement('TH');
  44. R.Append(H);
  45. H.AppendChild(document.createTextNode('Population'));
  46. end;
  47. function TForm.onLoad(Event: TEventListenerEvent): boolean;
  48. var
  49. i : integer;
  50. C,J : TJSObject;
  51. A : TJSObjectDynArray;
  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. A:=TJSObjectDynArray(J.Properties['Data']);
  61. Table:=CreateTable;
  62. Document.Body.append(Table);
  63. TB:=document.createElement('TBODY');
  64. Table.Append(TB);
  65. for I:=0 to Length(A)-1 do
  66. begin
  67. C:=A[i];
  68. TB.Append(CreateRow(String(C.Properties['Name']),Integer(C.Properties['Population'])));
  69. end;
  70. end
  71. else
  72. begin
  73. N:=Document.CreateElement('div');
  74. N.appendChild(Document.createTextNode('Failed to load countries: '+IntToStr(xhr.Status)));
  75. PanelContent.append(N);
  76. end;
  77. Result := True;
  78. end;
  79. function TForm.ButtonClick(Event: TJSMouseEvent): boolean;
  80. begin
  81. xhr:=TJSXMLHttpRequest.New;
  82. xhr.addEventListener('load', @OnLoad);
  83. xhr.open('GET', 'countries.json', true);
  84. xhr.send;
  85. Result:=true;
  86. end;
  87. constructor TForm.create;
  88. begin
  89. Panel:=document.createElement('div');
  90. // attrs are default array property...
  91. Panel['class']:='panel panel-default';
  92. PanelContent:=document.createElement('div');
  93. PanelContent['class']:='panel-body';
  94. Button:=document.createElement('input');
  95. Button['id']:='Button1';
  96. Button['type']:='submit';
  97. Button.className:='btn btn-default';
  98. Button['value']:='Fetch countries';
  99. TJSHTMLElement(Button).onclick:=@ButtonClick;
  100. document.body.appendChild(panel);
  101. Panel.appendChild(PanelContent);
  102. PanelContent.appendChild(Button);
  103. end;
  104. begin
  105. TForm.Create;
  106. end.