aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/data/tests/QueryReadStore.html
blob: c1634a512806f623f0892f39a705f8a5ac0f1e7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<style type="text/css">
		@import "../../../dojo/resources/dojo.css";
		@import "../../../dijit/themes/tundra/tundra.css";
		@import "../../../dijit/themes/tundra/tundra_rtl.css";
	</style>

	<title>Query read store</title>

	<script type="text/javascript" src="../../../dojo/dojo.js" djConfig="isDebug: true, parseOnLoad: true"></script>
	<script type="text/javascript" src="../../../dojo/data/util/simpleFetch.js"></script>
	<script type="text/javascript" src="../../../dojox/data/QueryReadStore.js"></script>
	<script type="text/javascript">
		dojo.require("dijit.form.ComboBox");
		dojo.require("dijit.form.FilteringSelect");
		dojo.require("dojox.data.QueryReadStore");

		dojo.provide("ComboBoxReadStore");
		dojo.declare("ComboBoxReadStore", dojox.data.QueryReadStore, {
			fetch:function(request) {
				// Copy the GET/POST parameters (request.query) we need into
				// request.serverQuery. We actually want to have
				// the query added to the URL like so:  /url.php?q=<searchString>
				// The data in "queryOptions" are useless for our backend,
				// we ignore them, they are not sent to the server.
				// The combobox puts this into the request-parameter:
				//      {
				//          query: {name:<searchString>},
				//          queryOptions: {ignoreCase:true, deep:true},
				//          ...
				//      }
				// We generate request.serverQuery to be this, since those values will
				// be sent to the server.
				//      {
				//          q:<searchString>}
				//      }
				// This results in a xhr request to the following URL (in case of GET):
				//      /url.php?q=<searchString>
				//

				request.serverQuery = {q:request.query.name};
				// If we wanted to send the queryOptions too, we could simply do:
				//  request.serverQuery = {
				//          q:request.query.name,
				//          ignoreCase:request.queryOptions.ignoreCase,
				//          deep:request.queryOptions.deep
				//      };
				// This would then result in this URL, for ignoreCase and deep
				// assumed to be true:
				//      /url.php?q=<searchString>&ignoreCase=true&deep=true
				return this.inherited("fetch", arguments);
			}
		});

		dojo.provide("ServerPagingReadStore");
		dojo.declare("ServerPagingReadStore", dojox.data.QueryReadStore, {
			fetch:function(request) {
				request.serverQuery = {q:request.query.name, start:request.start, count:request.count};
				return this.inherited("fetch", arguments);
			}
		});

		var testStore = new dojox.data.QueryReadStore({url:'stores/QueryReadStore.php'});;
		function doSearch() {
			var queryOptions = {};
			if (dojo.byId("ignoreCaseEnabled").checked) {
				queryOptions.ignoreCase = dojo.query("#fetchForm")[0].ignoreCase[0].checked;
			}
			if (dojo.byId("deepEnabled").checked) {
				queryOptions.deep = dojo.query("#fetchForm")[0].deep[0].checked;
			}
		    
			var query = {};
			query.q = dojo.byId("searchText").value;
			var request = {query:query, queryOptions:queryOptions};
			request.start = parseInt(dojo.query("#fetchForm")[0].pagingStart.value);
			request.count = parseInt(dojo.query("#fetchForm")[0].pagingCount.value);

			var requestMethod = "get";
			var radioButtons = dojo.query("#fetchForm")[0].requestMethod;
			for (var i=0; i<radioButtons.length; i++){
				if (radioButtons[i].checked) {
					requestMethod = radioButtons[i].value;
				}
			}
			
			testStore.requestMethod = requestMethod;
			testStore.doClientPaging = dojo.query("#fetchForm")[0].doClientPaging.checked;
			
			if (!testStore.doClientPaging) {
				// We have to fill the serverQuery, since we also want to send the
				// paging data "start" and "count" along with what is in query.
				request.serverQuery = {q:request.query.q, start:request.start, count:request.count};
			}
			
			request.onComplete = function (items) {
				var s = "number of items: "+items.length+"<br /><br />";
				for (var i=0; i<items.length; i++) {
					s += i+": name: '"+testStore.getValue(items[i], "name")+"'<br />";
				}
				//s += "<pre>"+dojo.toJson(items)+"</pre>";
				dojo.byId("fetchOutput").innerHTML = s;
			};

			console.log(dojo.toJson(request));
			testStore.fetch(request);
		}
	</script>
</head>
<body class="tundra" style="margin:20px;">
	<div dojoType="ComboBoxReadStore" jsId="store" url="stores/QueryReadStore.php" requestMethod="get"></div>
	This is a ComboBox: <input id="cb" dojoType="dijit.form.ComboBox" store="store" pageSize="5" />
	<br /><br /><hr />

	This is a FilteringSelect: <input id="fs" dojoType="dijit.form.FilteringSelect" store="store" pageSize="5" />
	<br />
	<form id="filteringSelectForm">
		<input id="selectById" value="0" size="3" />
		<input type="button" value="set by id" onclick="dijit.byId('fs').setValue(dojo.byId('selectById').value)" />
	</form>
	
	<br /><br /><hr />

	This ComboBox uses a customized QueryReadStore, it prepares the query-string for the URL that
	way that the paging parameters "start" and "count" are also send.<br />
	<div dojoType="ServerPagingReadStore" jsId="serverPagingStore" url="stores/QueryReadStore.php" requestMethod="get"></div>
	<input dojoType="dijit.form.ComboBox" store="serverPagingStore" pageSize="5" />
	<br />
	<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>
<div id="pagingCode" style="display:none;">
	The HTML might look like this.
	<pre>
&lt;div dojoType="ServerPagingReadStore" jsId="serverPagingStore" url="stores/QueryReadStore.php" requestMethod="get"&gt;&lt;/div&gt;
&lt;input dojoType="dijit.form.ComboBox" store="serverPagingStore" pageSize="10" /&gt;
	</pre>
	<pre>
		dojo.require("dojox.data.QueryReadStore");
		dojo.provide("ServerPagingReadStore");
		dojo.declare("ServerPagingReadStore", dojox.data.QueryReadStore, {
			fetch:function(request) {
				request.serverQuery = {q:request.query.name, start:request.start, count:request.count};
				return this.inherited("fetch", arguments);
			}
		});
	</pre>
</div>
	<br /><br />
	
	<hr />
	
	<style>
	     fieldset {
	         border:1px solid black;
	         display:inline;
	         padding:10px;
	     }
	     div.disabled {
	         opacity:0.1;
	     }
	</style>
	<form id="fetchForm">
		<fieldset title="requestMethod">
		    <legend>requestMethod</legend>
			get <input type="radio" value="get" checked="checked" name="requestMethod" />
			post <input type="radio" value="post" name="requestMethod" />
		</fieldset>
		
		<fieldset title="queryOptions">
			<legend>queryOptions</legend>

			<fieldset id="ignoreCaseFieldset">
				<legend><input type="checkbox" id="ignoreCaseEnabled" /> ignoreCase</legend>
				<div class="disabled">
					true <input type="radio" value="0" checked="checked" name="ignoreCase" />
					false <input type="radio" value="1" name="ignoreCase" />
				</div>
			</fieldset>
			<fieldset id="deepFieldset">
				<legend><input type="checkbox" id="deepEnabled" /> deep</legend>
				<div class="disabled">
					true <input type="radio" value="0" name="deep" />
					false <input type="radio" value="1" name="deep" checked="checked" />
				</div>
			</fieldset>
		</fieldset>
		<fieldset title="paging">
			<legend>paging</legend>
			start: <input id="pagingStart" value="0" size="3" />
			count: <input id="pagingCount" value="10" size="3"  />
			<br /><br />
			do client paging: <input id="doClientPaging" type="checkbox" checked="checked" />
		</fieldset>
		<script>
		    var fieldsets = ["ignoreCaseFieldset", "deepFieldset"];
		    for (var i=0; i<fieldsets.length; i++) {
		        dojo.connect(dojo.byId(fieldsets[i]), "onchange", toggleFieldset);
		    }
		    function toggleFieldset(el) {
		        var divs = dojo.query("div", el.target.parentNode.parentNode);
		        if (divs.length) {
					var div = divs[0];
					if (el.target.checked) {
						dojo.removeClass(div, "disabled");
					} else {
						dojo.addClass(div, "disabled");
					}
				}
		    }
		</script>
		
		<br /><br />
		<input id="searchText" type="text" value="a">
		<input id="searchButton" type="button" value="store.fetch()" onclick="doSearch()" />
	</form>
	<div id="fetchOutput" style="background-color:#FFDDDD; margin-top:1em; float:left;"></div>
</body>
</html>