demobootstraptable.lpr 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. program demobootstraptable;
  2. {$mode objfpc}
  3. uses
  4. JS, Classes, SysUtils, Web, libjquery, libbootstraptable, browserapp, DB, ExtJSDataset;
  5. Type
  6. { TDemo }
  7. TDemo = class(TBrowserApplication)
  8. Private
  9. xhr:TJSXMLHttpRequest;
  10. FDS : TExtJSJSONObjectDataSet;
  11. function CreateRow(AName: String; APopulation: NativeInt): TJSHTMLElement;
  12. procedure CreateTable;
  13. function onLoad(Event: TEventListenerEvent): boolean;
  14. Protected
  15. procedure DoRun; override;
  16. end;
  17. { TDemo }
  18. procedure TDemo.doRun;
  19. Var
  20. O : TBootstrapTableOptions;
  21. begin
  22. inherited doRun;
  23. // Using plain TJSObject
  24. JQuery('#table-prefilled').BootStrapTable(New(['search',true,'showColumns',true]));
  25. // Using TBootstrapTableOptions
  26. O:=TBootstrapTableOptions.new;
  27. O.search:=true;
  28. O.showColumns:=true;
  29. O.pagination:=true;
  30. O.showExtendedPagination:=True;
  31. O.url:='countries.json';
  32. O.sidePagination:='client';
  33. O.dataField:='Data';
  34. JQuery('#table-data').BootStrapTable(O);
  35. JQuery('#table-data').RefreshTable(New([]));
  36. // Using dataset
  37. FDS:=TExtJSJSONObjectDataSet.Create(Nil);
  38. xhr:=TJSXMLHttpRequest.New;
  39. xhr.addEventListener('load', @OnLoad);
  40. xhr.open('GET', 'countries.json', true);
  41. xhr.send;
  42. end;
  43. function TDemo.onLoad(Event: TEventListenerEvent): boolean;
  44. var
  45. i : integer;
  46. C,J : TJSObject;
  47. N,TB : TJSElement;
  48. begin
  49. if (xhr.status = 200) then
  50. begin
  51. J:=TJSJSON.parseObject(xhr.responseText);
  52. FDS.Metadata:=TJSObject(J.Properties['metaData']);
  53. FDS.Rows:=TJSArray(J.Properties['Data']);
  54. FDS.Open;
  55. CreateTable;
  56. end
  57. else
  58. begin
  59. N:=Document.CreateElement('div');
  60. N.appendChild(Document.createTextNode('Failed to load countries: '+IntToStr(xhr.Status)));
  61. document.GetElementByID('tab-manual').appendChild(N);
  62. end;
  63. end;
  64. procedure TDemo.CreateTable;
  65. Var
  66. TB,P,R,C : TJSHTMLElement;
  67. FName,FPop : TField;
  68. O : TBootstrapTableOptions;
  69. begin
  70. TB:=GetHTMLElement('table-manual');
  71. P:=CreateHTMLElement('THEAD');
  72. TB.AppendChild(P);
  73. R:=CreateHTMLElement('TR');
  74. P.AppendChild(R);
  75. C:=CreateHTMLElement('TH');
  76. C.InnerText:='Name';
  77. C.Dataset['field']:='Name';
  78. R.AppendChild(C);
  79. C:=CreateHTMLElement('TH');
  80. C.InnerText:='Pop.';
  81. C.Dataset['field']:='Population';
  82. R.AppendChild(C);
  83. P:=CreateHTMLElement('TBODY');
  84. TB.AppendChild(P);
  85. FName:=FDS.FieldByName('Name');
  86. FPop:=FDS.FieldByName('Population');
  87. While not FDS.EOF do
  88. begin
  89. P.AppendChild(CreateRow(FName.AsString,FPop.AsInteger));
  90. FDS.Next;
  91. end;
  92. O:=TBootstrapTableOptions.new;
  93. O.search:=true;
  94. O.showColumns:=true;
  95. O.pagination:=true;
  96. O.showExtendedPagination:=True;
  97. O.url:='countries.json';
  98. O.sidePagination:='client';
  99. O.dataField:='Data';
  100. JQuery(TB).BootStrapTable(O);
  101. end;
  102. function TDemo.CreateRow(AName : String; APopulation : NativeInt) : TJSHTMLElement;
  103. Var
  104. C : TJSHTMLElement;
  105. begin
  106. Result:=createHTMLElement('TR');
  107. C:=createHTMLElement('TD');
  108. Result.Append(C);
  109. C.appendChild(Document.createTextNode(AName));
  110. C:=createHTMLElement('TD');
  111. Result.Append(C);
  112. C.AppendChild(document.createTextNode(IntToStr(APopulation)));
  113. end;
  114. begin
  115. With TDemo.Create(Nil) do
  116. begin
  117. Initialize;
  118. Run;
  119. end;
  120. end.