room.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. room_parameter = {};
  2. function room_grid(items) {
  3. var str = '<table border="1" width="100%"><tbody>';
  4. str += '<tr><th>id</th><th>name</th><th>address</th><th>operate</th></tr>';
  5. if (items) {
  6. $.each(items, function(index, item) {
  7. str += '<tr>';
  8. str += '<td>' + item.name + '</td><td colspan="2">';
  9. if (item.photo && item.photo != '') {
  10. str += imageBase64(item.photo);
  11. } else {
  12. str += '&nbsp;';
  13. }
  14. str += '</td><td>';
  15. str += '<a href="#" onclick="room_put_init(\'' + item.id + '\')">edit</a>&nbsp;';
  16. str += '<a href="#" onclick="room_delete(\'' + item.id + '\')">delete</a>&nbsp;';
  17. str += '<a href="#" onclick="room_update_photo_init(\'' + item.id + '\')">photo</a>';
  18. str += '</td>';
  19. str += '</tr>';
  20. });
  21. }
  22. str += '</tobdy></table>';
  23. $('#content').html(str);
  24. }
  25. function room_list() {
  26. $('#result').html('');
  27. $.ajax({
  28. type : 'get',
  29. dataType : 'json',
  30. url : '../jaxrs/room/list',
  31. xhrFields : {
  32. 'withCredentials' : true
  33. },
  34. crossDomain : true
  35. }).done(function(json) {
  36. $('#result').html(JSON.stringify(json, null, 4));
  37. if (json.type == 'success') {
  38. room_grid(json.data, true);
  39. }
  40. });
  41. }
  42. function room_get_select() {
  43. $('#result').html('');
  44. var str = '<table border="1" width="100%">';
  45. str += '<tr><td colspan="2"><a href="#" id="get">get</a></td></tr>';
  46. str += '</table>';
  47. $('#content').html(str);
  48. $('#get').click(function() {
  49. room_delete(id);
  50. });
  51. }
  52. function room_get(id) {
  53. $('#result').html('');
  54. $.ajax({
  55. type : 'get',
  56. dataType : 'json',
  57. url : '../jaxrs/room/' + id,
  58. xhrFields : {
  59. 'withCredentials' : true
  60. },
  61. crossDomain : true
  62. }).done(function(json) {
  63. $('#result').html(JSON.stringify(json, null, 4));
  64. });
  65. }
  66. function room_post_select() {
  67. $('#result').html('');
  68. var str = '<table border="1" width="100%">';
  69. str += '<tr><td colspan="2"><a href="#" id="post">post</a></td></tr>';
  70. str += '<tr><td>name:</td><td><input type="text" id="name" style="width:95%"/></td></tr>';
  71. str += '<tr><td>building:</td><td><input type="text" id="building" style="width:95%"/></td></tr>';
  72. str += '<tr><td>floor:</td><td><input type="text" id="floor" style="width:95%"/></td></tr>';
  73. str += '<tr><td>roomNumber:</td><td><input type="text" id="roomNumber" style="width:95%"/></td></tr>';
  74. str += '<tr><td>capacity:</td><td><input type="text" id="capacity" style="width:95%"/></td></tr>';
  75. str += '<tr><td>auditor:</td><td><input type="text" id="auditor" style="width:95%"/></td></tr>';
  76. str += '<tr><td>available:</td><td><select id="available"><option value="true">true</option><option value="false">false</option></select></td></tr>';
  77. str += '<tr><td>device:</td><td><textarea id="device" style="width:95%;height:500px"/></td></tr>';
  78. str += '</table>';
  79. $('#content').html(str);
  80. $('#post').click(function() {
  81. room_post();
  82. });
  83. }
  84. function room_post() {
  85. $('#result').html('');
  86. $.ajax({
  87. type : 'post',
  88. dataType : 'json',
  89. url : '../jaxrs/room',
  90. contentType : 'application/json; charset=utf-8',
  91. data : JSON.stringify({
  92. name : $('#name').val(),
  93. building : $('#building').val(),
  94. floor : $('#floor').val(),
  95. roomNumber : $('#roomNumber').val(),
  96. capacity : $('#capacity').val(),
  97. auditor : $('#auditor').val(),
  98. available : $('#available').val(),
  99. device : $('#device').val()
  100. }),
  101. xhrFields : {
  102. 'withCredentials' : true
  103. },
  104. crossDomain : true
  105. }).done(function(json) {
  106. $('#result').html(JSON.stringify(json, null, 4));
  107. });
  108. }
  109. function room_put_select() {
  110. $('#result').html('');
  111. var str = '<table border="1" width="100%">';
  112. str += '<tr><td colspan="2"><a href="#" id="select">select</a></td></tr>';
  113. str += '</table>';
  114. $('#content').html(str);
  115. $('#select').click(function() {
  116. room_put_init(id);
  117. });
  118. }
  119. function room_put_init(id) {
  120. $('#result').html('');
  121. var str = '<table border="1" width="100%">';
  122. str += '<tr><td colspan="2"><a href="#" id="put">put</a></td></tr>';
  123. str += '<tr><td>id:</td><td id="id"></td></tr>';
  124. str += '<tr><td>name:</td><td><input type="text" id="name" style="width:95%"/></td></tr>';
  125. str += '<tr><td>building:</td><td><input type="text" id="building" style="width:95%"/></td></tr>';
  126. str += '<tr><td>floor:</td><td><input type="text" id="floor" style="width:95%"/></td></tr>';
  127. str += '<tr><td>roomNumber:</td><td><input type="text" id="roomNumber" style="width:95%"/></td></tr>';
  128. str += '<tr><td>capacity:</td><td><input type="text" id="capacity" style="width:95%"/></td></tr>';
  129. str += '<tr><td>auditor:</td><td><input type="text" id="auditor" style="width:95%"/></td></tr>';
  130. str += '<tr><td>available:</td><td><select id="available"><option value="true">true</option><option value="false">false</option></select></td></tr>';
  131. str += '<tr><td>device:</td><td><textarea id="device" style="width:95%;height:500px"/></td></tr>';
  132. str += '<tr><td>photo:</td><td id="photo">&nbsp;</td></tr>';
  133. str += '</table>';
  134. $('#content').html(str);
  135. $('#put').click(function() {
  136. room_put(id);
  137. });
  138. $.ajax({
  139. type : 'get',
  140. dataType : 'json',
  141. url : '../jaxrs/room/' + id,
  142. xhrFields : {
  143. 'withCredentials' : true
  144. },
  145. crossDomain : true
  146. }).done(function(json) {
  147. $('#result').html(JSON.stringify(json, null, 4));
  148. if (json.type == 'success') {
  149. $('#id').html(json.data.id);
  150. $('#name').val(json.data.name);
  151. $('#building').val(json.data.building);
  152. $('#floor').val(json.data.floor);
  153. $('#roomNumber').val(json.data.roomNumber);
  154. $('#capacity').val(json.data.capacity);
  155. $('#auditor').val(json.data.auditor);
  156. $('#available').val(json.data.available + '');
  157. $('#device').val(json.data.device);
  158. }
  159. });
  160. }
  161. function room_put(id) {
  162. $('#result').html('');
  163. $.ajax({
  164. type : 'put',
  165. dataType : 'json',
  166. url : '../jaxrs/room/' + id,
  167. contentType : 'application/json; charset=utf-8',
  168. data : JSON.stringify({
  169. name : $('#name').val(),
  170. building : $('#building').val(),
  171. floor : $('#floor').val(),
  172. roomNumber : $('#roomNumber').val(),
  173. capacity : $('#capacity').val(),
  174. auditor : $('#auditor').val(),
  175. available : $('#available').val(),
  176. device : $('#device').val()
  177. }),
  178. xhrFields : {
  179. 'withCredentials' : true
  180. },
  181. crossDomain : true
  182. }).done(function(json) {
  183. $('#result').html(JSON.stringify(json, null, 4));
  184. });
  185. }
  186. function room_delete_select() {
  187. $('#result').html('');
  188. var str = '<table border="1" width="100%">';
  189. str += '<tr><td colspan="2"><a href="#" id="delete">delete</a></td></tr>';
  190. str += '</table>';
  191. $('#content').html(str);
  192. $('#delete').click(function() {
  193. room_delete(id);
  194. });
  195. }
  196. function room_delete(id) {
  197. $('#result').html('');
  198. $.ajax({
  199. type : 'delete',
  200. dataType : 'json',
  201. url : '../jaxrs/room/' + id,
  202. xhrFields : {
  203. 'withCredentials' : true
  204. },
  205. crossDomain : true
  206. }).done(function(json) {
  207. $('#result').html(JSON.stringify(json, null, 4));
  208. });
  209. }
  210. function room_put_photo_select(id) {
  211. $('#result').html('');
  212. var str = '<table border="1" width="100%">';
  213. str += '<tr><td colspan="2"><a href="#" id="put">put</a></td></tr>';
  214. str += '<tr><td>photo:</td><td><input type="file" id="file" style="width:95%" ></td></tr>';
  215. str += '</table>';
  216. $('#content').html(str);
  217. $('#put').click(function() {
  218. room_put_photo(id);
  219. });
  220. }
  221. function room_put_photo(id) {
  222. $('#result').html('');
  223. var formData = new FormData();
  224. $.each($('input[type=file]'), function(index, item) {
  225. formData.append('file', item.files[0]);
  226. });
  227. $.ajax({
  228. type : 'POST',
  229. url : '../servlet/room/' + id + '/photo',
  230. data : formData,
  231. contentType : false,
  232. processData : false,
  233. xhrFields : {
  234. 'withCredentials' : true
  235. },
  236. crossDomain : true
  237. });
  238. }
  239. function room_search_select() {
  240. $('#result').html('');
  241. var str = '<table border="1" width="100%">';
  242. str += '<tr><td colspan="2">';
  243. str += '<a href="#" id="listPinyinInitial">listPinyinInitial</a>';
  244. str += '&nbsp;';
  245. str += '<a href="#" id="listLike">listLike</a>';
  246. str += '&nbsp;';
  247. str += '<a href="#" id="listLikePinyin">listLikePinyin</a>';
  248. str += '</td></tr>';
  249. str += '<tr><td>key:</td><td><input type="text" id="key" style="width:95%"/></td></tr>';
  250. str += '</table>';
  251. $('#content').html(str);
  252. $('#listPinyinInitial').click(function() {
  253. room_listPinyinInitial($('#key').val());
  254. });
  255. $('#listLike').click(function() {
  256. room_listLike($('#key').val());
  257. });
  258. $('#listLikePinyin').click(function() {
  259. room_listLikePinyin($('#key').val());
  260. });
  261. }
  262. function room_listPinyinInitial(key) {
  263. $('#result').html('');
  264. $.ajax({
  265. type : 'get',
  266. dataType : 'json',
  267. url : '../jaxrs/room/list/pinyininitial/' + key,
  268. xhrFields : {
  269. 'withCredentials' : true
  270. },
  271. crossDomain : true
  272. }).done(function(json) {
  273. $('#result').html(JSON.stringify(json, null, 4));
  274. room_grid(json.data);
  275. });
  276. }
  277. function room_listLike(key) {
  278. $('#result').html('');
  279. $.ajax({
  280. type : 'get',
  281. dataType : 'json',
  282. url : '../jaxrs/room/list/like/' + key,
  283. xhrFields : {
  284. 'withCredentials' : true
  285. },
  286. crossDomain : true
  287. }).done(function(json) {
  288. $('#result').html(JSON.stringify(json, null, 4));
  289. room_grid(json.data);
  290. });
  291. }
  292. function room_listLikePinyin(key) {
  293. $('#result').html('');
  294. $.ajax({
  295. type : 'get',
  296. dataType : 'json',
  297. url : '../jaxrs/room/list/like/pinyin/' + key,
  298. xhrFields : {
  299. 'withCredentials' : true
  300. },
  301. crossDomain : true
  302. }).done(function(json) {
  303. $('#result').html(JSON.stringify(json, null, 4));
  304. room_grid(json.data);
  305. });
  306. }