QueryReadStore.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <style type="text/css">
  6. @import "../../../dojo/resources/dojo.css";
  7. @import "../../../dijit/themes/tundra/tundra.css";
  8. @import "../../../dijit/themes/tundra/tundra_rtl.css";
  9. </style>
  10. <title>Query read store</title>
  11. <script type="text/javascript" src="../../../dojo/dojo.js" djConfig="isDebug: true, parseOnLoad: true"></script>
  12. <script type="text/javascript" src="../../../dojo/data/util/simpleFetch.js"></script>
  13. <script type="text/javascript" src="../../../dojox/data/QueryReadStore.js"></script>
  14. <script type="text/javascript">
  15. dojo.require("dijit.form.ComboBox");
  16. dojo.require("dijit.form.FilteringSelect");
  17. dojo.require("dojox.data.QueryReadStore");
  18. dojo.provide("ComboBoxReadStore");
  19. dojo.declare("ComboBoxReadStore", dojox.data.QueryReadStore, {
  20. fetch:function(request) {
  21. // Copy the GET/POST parameters (request.query) we need into
  22. // request.serverQuery. We actually want to have
  23. // the query added to the URL like so: /url.php?q=<searchString>
  24. // The data in "queryOptions" are useless for our backend,
  25. // we ignore them, they are not sent to the server.
  26. // The combobox puts this into the request-parameter:
  27. // {
  28. // query: {name:<searchString>},
  29. // queryOptions: {ignoreCase:true, deep:true},
  30. // ...
  31. // }
  32. // We generate request.serverQuery to be this, since those values will
  33. // be sent to the server.
  34. // {
  35. // q:<searchString>}
  36. // }
  37. // This results in a xhr request to the following URL (in case of GET):
  38. // /url.php?q=<searchString>
  39. //
  40. request.serverQuery = {q:request.query.name};
  41. // If we wanted to send the queryOptions too, we could simply do:
  42. // request.serverQuery = {
  43. // q:request.query.name,
  44. // ignoreCase:request.queryOptions.ignoreCase,
  45. // deep:request.queryOptions.deep
  46. // };
  47. // This would then result in this URL, for ignoreCase and deep
  48. // assumed to be true:
  49. // /url.php?q=<searchString>&ignoreCase=true&deep=true
  50. return this.inherited("fetch", arguments);
  51. }
  52. });
  53. dojo.provide("ServerPagingReadStore");
  54. dojo.declare("ServerPagingReadStore", dojox.data.QueryReadStore, {
  55. fetch:function(request) {
  56. request.serverQuery = {q:request.query.name, start:request.start, count:request.count};
  57. return this.inherited("fetch", arguments);
  58. }
  59. });
  60. var testStore = new dojox.data.QueryReadStore({url:'stores/QueryReadStore.php'});;
  61. function doSearch() {
  62. var queryOptions = {};
  63. if (dojo.byId("ignoreCaseEnabled").checked) {
  64. queryOptions.ignoreCase = dojo.query("#fetchForm")[0].ignoreCase[0].checked;
  65. }
  66. if (dojo.byId("deepEnabled").checked) {
  67. queryOptions.deep = dojo.query("#fetchForm")[0].deep[0].checked;
  68. }
  69. var query = {};
  70. query.q = dojo.byId("searchText").value;
  71. var request = {query:query, queryOptions:queryOptions};
  72. request.start = parseInt(dojo.query("#fetchForm")[0].pagingStart.value);
  73. request.count = parseInt(dojo.query("#fetchForm")[0].pagingCount.value);
  74. var requestMethod = "get";
  75. var radioButtons = dojo.query("#fetchForm")[0].requestMethod;
  76. for (var i=0; i<radioButtons.length; i++){
  77. if (radioButtons[i].checked) {
  78. requestMethod = radioButtons[i].value;
  79. }
  80. }
  81. testStore.requestMethod = requestMethod;
  82. testStore.doClientPaging = dojo.query("#fetchForm")[0].doClientPaging.checked;
  83. if (!testStore.doClientPaging) {
  84. // We have to fill the serverQuery, since we also want to send the
  85. // paging data "start" and "count" along with what is in query.
  86. request.serverQuery = {q:request.query.q, start:request.start, count:request.count};
  87. }
  88. request.onComplete = function (items) {
  89. var s = "number of items: "+items.length+"<br /><br />";
  90. for (var i=0; i<items.length; i++) {
  91. s += i+": name: '"+testStore.getValue(items[i], "name")+"'<br />";
  92. }
  93. //s += "<pre>"+dojo.toJson(items)+"</pre>";
  94. dojo.byId("fetchOutput").innerHTML = s;
  95. };
  96. console.log(dojo.toJson(request));
  97. testStore.fetch(request);
  98. }
  99. </script>
  100. </head>
  101. <body class="tundra" style="margin:20px;">
  102. <div dojoType="ComboBoxReadStore" jsId="store" url="stores/QueryReadStore.php" requestMethod="get"></div>
  103. This is a ComboBox: <input id="cb" dojoType="dijit.form.ComboBox" store="store" pageSize="5" />
  104. <br /><br /><hr />
  105. This is a FilteringSelect: <input id="fs" dojoType="dijit.form.FilteringSelect" store="store" pageSize="5" />
  106. <br />
  107. <form id="filteringSelectForm">
  108. <input id="selectById" value="0" size="3" />
  109. <input type="button" value="set by id" onclick="dijit.byId('fs').setValue(dojo.byId('selectById').value)" />
  110. </form>
  111. <br /><br /><hr />
  112. This ComboBox uses a customized QueryReadStore, it prepares the query-string for the URL that
  113. way that the paging parameters "start" and "count" are also send.<br />
  114. <div dojoType="ServerPagingReadStore" jsId="serverPagingStore" url="stores/QueryReadStore.php" requestMethod="get" doClientPaging="false"></div>
  115. <input dojoType="dijit.form.ComboBox" store="serverPagingStore" pageSize="5" />
  116. <br />
  117. <a href="javascript://" onclick="var d = dojo.byId('pagingCode'); d.style.display= d.style.display=='none'?'block':'none';">Click here to see the code!</a>
  118. <div id="pagingCode" style="display:none;">
  119. The HTML might look like this, the important attribute: <em>doClientPaging="false"</em> this takes care that the same query is fired to the server
  120. and its not assumed that the client (the store) does the paging on the old data.
  121. <pre>
  122. &lt;div dojoType="ServerPagingReadStore" jsId="serverPagingStore" url="stores/QueryReadStore.php" requestMethod="get" doClientPaging="false"&gt;&lt;/div&gt;
  123. &lt;input dojoType="dijit.form.ComboBox" store="serverPagingStore" pageSize="10" /&gt;
  124. </pre>
  125. <pre>
  126. dojo.require("dojox.data.QueryReadStore");
  127. dojo.provide("ServerPagingReadStore");
  128. dojo.declare("ServerPagingReadStore", dojox.data.QueryReadStore, {
  129. fetch:function(request) {
  130. request.serverQuery = {q:request.query.name, start:request.start, count:request.count};
  131. return this.inherited("fetch", arguments);
  132. }
  133. });
  134. </pre>
  135. </div>
  136. <br /><br />
  137. <hr />
  138. <style>
  139. fieldset {
  140. border:1px solid black;
  141. display:inline;
  142. padding:10px;
  143. }
  144. div.disabled {
  145. opacity:0.1;
  146. }
  147. </style>
  148. <form id="fetchForm">
  149. <fieldset title="requestMethod">
  150. <legend>requestMethod</legend>
  151. get <input type="radio" value="get" checked="checked" name="requestMethod" />
  152. post <input type="radio" value="post" name="requestMethod" />
  153. </fieldset>
  154. <fieldset title="queryOptions">
  155. <legend>queryOptions</legend>
  156. <fieldset id="ignoreCaseFieldset">
  157. <legend><input type="checkbox" id="ignoreCaseEnabled" /> ignoreCase</legend>
  158. <div class="disabled">
  159. true <input type="radio" value="0" checked="checked" name="ignoreCase" />
  160. false <input type="radio" value="1" name="ignoreCase" />
  161. </div>
  162. </fieldset>
  163. <fieldset id="deepFieldset">
  164. <legend><input type="checkbox" id="deepEnabled" /> deep</legend>
  165. <div class="disabled">
  166. true <input type="radio" value="0" name="deep" />
  167. false <input type="radio" value="1" name="deep" checked="checked" />
  168. </div>
  169. </fieldset>
  170. </fieldset>
  171. <fieldset title="paging">
  172. <legend>paging</legend>
  173. start: <input id="pagingStart" value="0" size="3" />
  174. count: <input id="pagingCount" value="10" size="3" />
  175. <br /><br />
  176. do client paging: <input id="doClientPaging" type="checkbox" checked="checked" />
  177. </fieldset>
  178. <script>
  179. var fieldsets = ["ignoreCaseFieldset", "deepFieldset"];
  180. for (var i=0; i<fieldsets.length; i++) {
  181. dojo.connect(dojo.byId(fieldsets[i]), "onchange", toggleFieldset);
  182. }
  183. function toggleFieldset(el) {
  184. var divs = dojo.query("div", el.target.parentNode.parentNode);
  185. if (divs.length) {
  186. var div = divs[0];
  187. if (el.target.checked) {
  188. dojo.removeClass(div, "disabled");
  189. } else {
  190. dojo.addClass(div, "disabled");
  191. }
  192. }
  193. }
  194. </script>
  195. <br /><br />
  196. <input id="searchText" type="text" value="a">
  197. <input id="searchButton" type="button" value="store.fetch()" onclick="doSearch()" />
  198. </form>
  199. <div id="fetchOutput" style="background-color:#FFDDDD; margin-top:1em; float:left;"></div>
  200. </body>
  201. </html>