pestindex.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. var SUCCESS = 'success';
  2. var ERROR = 'error';
  3. var serverErrorMessage = 'Oops, something wrong :(';
  4. $(document).ready(function () {
  5. $('#pestDataTable').dataTable({
  6. "ajax": {
  7. "url": "/list/",
  8. "dataType": "json",
  9. },
  10. dom: 'Bfrtip',
  11. buttons: [
  12. {
  13. "extend": 'excel',//使用 excel扩展
  14. "text": '导出',// 显示文字
  15. "exportOptions": {
  16. //自定义导出选项
  17. //如:可自定义导出那些列,那些行
  18. //TODO...
  19. 'modifier': {
  20. //'page': 'current'
  21. selected: true
  22. }
  23. }
  24. }
  25. ],
  26. selected: false,
  27. "columns": [
  28. {
  29. data: "pk",
  30. "render": function (data) {
  31. return '<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline"><input type="checkbox" class="checkbox" value="' + data + '" /><span></span></label>';
  32. }
  33. },
  34. {"data": "fields.pestname"},
  35. {"data": "fields.date"},
  36. {"data": "fields.adressid"},
  37. {"data": "fields.researchpeople"},
  38. {"data": "fields.remark", visible:false},
  39. {
  40. data: "pk",
  41. "render": function (data) {
  42. return '<i id=" ' + data + ' " class="edit-button glyphicon glyphicon-edit cursorPointer" ></i>';
  43. }
  44. },
  45. {
  46. data: "pk",
  47. "render": function (data) {
  48. return '<i id=" ' + data + ' " class="remove-button glyphicon glyphicon-trash cursorPointer"></i>';
  49. }
  50. }
  51. ]
  52. });
  53. var tableTree = $('#pestDataTable').DataTable();
  54. $("body").on('checked','.mt-checkbox',function () {
  55. tableTree.rows( { selected: true } ).data();
  56. });
  57. // Delete employee event
  58. $("body").on('click', '.remove-button', function () {
  59. var currentRow = $(this);
  60. var treeId = $(this).attr('id').trim();
  61. bootbox.confirm("Are you sure?", function (result) {
  62. if (result) {
  63. $.ajax({
  64. url: "/del_pest/" + treeId,
  65. type: "GET",
  66. data: {Id: treeId},
  67. success: function (response) {
  68. if (response.status == SUCCESS) {
  69. showSuccessAlert(response.msg);
  70. tableTree.row(currentRow.parents('tr')).remove().draw();
  71. } else {
  72. showErrorAlert(serverErrorMessage);
  73. }
  74. },
  75. error: function () {
  76. showErrorAlert(serverErrorMessage);
  77. }
  78. });
  79. } else {
  80. //
  81. }
  82. });
  83. });
  84. // Edit employee event
  85. $("body").on('click', '.edit-button', function () {
  86. var treeId = $(this).attr('id').trim();
  87. $('#gsmmEditForm input[id=file]').val("")
  88. $('#gsmmEditModal').modal('show');
  89. $.ajax({
  90. url: "/pestdic/0",
  91. type: "GET",
  92. "dataType": "json",
  93. async: false,
  94. success: function (response) {
  95. $('#gsmmEditForm select[name=pestname]').empty();
  96. $('#gsmmEditForm select[name=pestname]').append($('<option>').text("-请选择-").attr('value', 0));
  97. $.each(response.data, function (i, obj) {
  98. $('#gsmmEditForm select[name=pestname]').append($('<option>').text(obj.fields.pestname).attr('value', obj.fields.id));
  99. });
  100. },
  101. error: function () {
  102. showErrorAlert(serverErrorMessage);
  103. }
  104. });
  105. $.ajax({
  106. url: "/researchpeople/0",
  107. type: "GET",
  108. "dataType": "json",
  109. async: false,
  110. success: function (response) {
  111. $('#gsmmEditForm select[name=researchperson]').empty();
  112. $('#gsmmEditForm select[name=researchperson]').append($('<option>').text("-请选择-").attr('value', 0));
  113. $.each(response.data, function (i, obj) {
  114. $('#gsmmEditForm select[name=researchperson]').append($('<option>').text(obj.fields.username).attr('value', obj.fields.id));
  115. });
  116. },
  117. error: function () {
  118. showErrorAlert(serverErrorMessage);
  119. }
  120. });
  121. $.ajax({
  122. url: "/pestlist/" + treeId,
  123. type: "GET",
  124. data: {id: treeId},
  125. success: function (response) {
  126. $('#gsmmEditForm input[name=id]').val(treeId);
  127. $.each(response.data[0].fields, function (key, value) {
  128. if (key == "date") {
  129. $('#gsmmEditForm input[name="' + key + '"]').val(value);
  130. }
  131. else if (key == "adressid") {
  132. $('#gsmmEditForm input[name="' + key + '"]').val(value);
  133. } else if (key == "pestname") {
  134. $('#gsmmEditForm select[name="' + key + '"]').val(value);
  135. } else if (key == "pestfirstgrade") {
  136. $('#gsmmEditForm select[name="' + key + '"]').val(value);
  137. } else if (key == "pestsecondgrade") {
  138. $('#gsmmEditForm select[name="' + key + '"]').val(value);
  139. } else if (key == "researchpeople") {
  140. $('#gsmmEditForm select[name=researchperson]').val(value);
  141. } else if (key == "remark") {
  142. $('#gsmmEditForm textarea[name="' + key + '"]').val(value);
  143. } else if (key == "longtitude") {
  144. $('#gsmmEditForm input[name="' + key + '"]').val(value);
  145. } else if (key == "latitude") {
  146. $('#gsmmEditForm input[name="' + key + '"]').val(value);
  147. } else {
  148. $('#gsmmEditForm input[name="' + key + '"]').val(value);
  149. }
  150. });
  151. var picurl1 = $('#gsmmEditForm input[name=pestpicurl1]').val()
  152. $("#gsmmEditForm img[id=pestpic1]").attr("src", picurl1);
  153. $("#pestpic1").show();
  154. var picurl2 = $('#gsmmEditForm input[name=pestpicurl2]').val()
  155. $("#gsmmEditForm img[id=pestpic2]").attr("src", picurl2);
  156. $("#pestpic2").show();
  157. },
  158. error: function () {
  159. showErrorAlert(serverErrorMessage);
  160. }
  161. });
  162. });
  163. $('#gsmmModal').on('shown.bs.modal', function () {
  164. $('#gsmmForm').trigger("reset");
  165. $("#gsmmForm img[id=pestpic1]").attr("src", "");
  166. $("#gsmmForm img[id=pestpic2]").attr("src", "");
  167. $("#gsmmForm img[id=pestpic1]").hide();
  168. $("#gsmmForm img[id=pestpic2]").hide();
  169. $.ajax({
  170. url: "/pestdic/0",
  171. type: "GET",
  172. "dataType": "json",
  173. success: function (response) {
  174. $('#gsmmForm select[name=pestname]').empty();
  175. $('#gsmmForm select[name=pestname]').append($('<option>').text("-请选择-").attr('value', "0"));
  176. $.each(response.data, function (i, obj) {
  177. $('#gsmmForm select[name=pestname]').append($('<option>').text(obj.fields.pestname).attr('value', obj.fields.id));
  178. });
  179. },
  180. error: function () {
  181. showErrorAlert(serverErrorMessage);
  182. }
  183. });
  184. $.ajax({
  185. url: "/researchpeople/0",
  186. type: "GET",
  187. "dataType": "json",
  188. success: function (response) {
  189. $('#gsmmForm select[name=researchperson]').empty();
  190. $('#gsmmForm select[name=researchperson]').append($('<option>').text("-请选择-").attr('value', 0));
  191. $.each(response.data, function (i, obj) {
  192. $('#gsmmForm select[name=researchperson]').append($('<option>').text(obj.fields.username).attr('value', obj.fields.id));
  193. });
  194. },
  195. error: function () {
  196. showErrorAlert(serverErrorMessage);
  197. }
  198. });
  199. });
  200. //增加页面select联动事件
  201. $('#pestname').change(function () {
  202. var keId = $("#pestname").find("option:selected").val().toString();
  203. $.ajax({
  204. url: "/pestdic/" + keId,
  205. type: "GET",
  206. data: {id: keId},
  207. "dataType": "json",
  208. success: function (response) {
  209. $('#pestfirstgrade').empty();
  210. $('#pestfirstgrade').append($('<option>').text("-请选择-").attr('value', 0));
  211. $.each(response.data, function (i, obj) {
  212. $('#pestfirstgrade').append($('<option>').text(obj.fields.pestfirstgrade).attr('value', obj.fields.id));
  213. });
  214. },
  215. error: function () {
  216. showErrorAlert(serverErrorMessage);
  217. }
  218. });
  219. });
  220. $('#pestfirstgrade').change(function () {
  221. var keId = $("#pestfirstgrade").find("option:selected").val().toString();
  222. $.ajax({
  223. url: "/pestdic/" + keId,
  224. type: "GET",
  225. data: {id: keId},
  226. "dataType": "json",
  227. success: function (response) {
  228. $('#pestsecondgrade').empty();
  229. $('#pestsecondgrade').append($('<option>').text("-请选择-").attr('value', 0));
  230. $.each(response.data, function (i, obj) {
  231. $('#pestsecondgrade').append($('<option>').text(obj.fields.pestsecondgrade).attr('value', obj.fields.id));
  232. });
  233. },
  234. error: function () {
  235. showErrorAlert(serverErrorMessage);
  236. }
  237. });
  238. });
  239. $('#gsmmForm input[id=picupload1]').click(function (event) {
  240. event.preventDefault();
  241. var imgVal = $('#gsmmForm input[id=file]').val();
  242. /*if (imgVal.empty()) {
  243. alert("图片为空,请选择后上传!");
  244. }*/
  245. /*else {*/
  246. var file1 = $('#gsmmForm input[id=file]').get(0).files[0];
  247. var formData = new FormData();
  248. formData.append('file', file1);
  249. $.ajax({
  250. url: '/uploadpic/',
  251. data: formData,
  252. type: 'POST',
  253. contentType: false,
  254. processData: false,
  255. success: function (data) {
  256. alert("图片上传成功");
  257. $('#gsmmForm input[id=pestpicurl1]').val(data);
  258. $("#gsmmForm img[id=pestpic1]").attr("src", data);
  259. $("#gsmmForm img[id=pestpic1]").show();
  260. },
  261. error: function (jqXHR, textStatus, errorThrown) {
  262. alert(textStatus + ': ' + errorThrown);
  263. }
  264. });
  265. /* }*/
  266. });
  267. $('#gsmmForm input[id=picupload2]').click(function (event) {
  268. event.preventDefault();
  269. var file1 = $('#gsmmForm input[id=file]').get(0).files[0];
  270. var formData = new FormData();
  271. formData.append('file', file1);
  272. $.ajax({
  273. url: '/uploadpic/',
  274. data: formData,
  275. type: 'POST',
  276. contentType: false,
  277. processData: false,
  278. success: function (data) {
  279. alert("图片上传成功");
  280. $('#gsmmForm input[id=pestpicurl2]').val(data);
  281. $("#gsmmForm img[id=pestpic2]").attr("src", data);
  282. $("#gsmmForm img[id=pestpic2]").show();
  283. },
  284. error: function (jqXHR, textStatus, errorThrown) {
  285. alert(textStatus + ': ' + errorThrown);
  286. }
  287. });
  288. });
  289. //更新页面select联动事件
  290. $('#gsmmEditForm select[name=pestname]').change(function () {
  291. var keId = $('#gsmmEditForm select[name=pestname]').find("option:selected").val().toString();
  292. $.ajax({
  293. url: "/pestdic/" + keId,
  294. type: "GET",
  295. data: {id: keId},
  296. "dataType": "json",
  297. success: function (response) {
  298. $('#gsmmEditForm select[name=pestfirstname]').empty();
  299. $('#gsmmEditForm select[name=pestfirstname]').append($('<option>').text("-请选择-").attr('value', 0));
  300. $.each(response.data, function (i, obj) {
  301. $('#gsmmEditForm select[name=pestfirstname]').append($('<option>').text(obj.fields.pestfirstname).attr('value', obj.fields.id));
  302. });
  303. },
  304. error: function () {
  305. showErrorAlert(serverErrorMessage);
  306. }
  307. });
  308. });
  309. $('#gsmmEditForm select[name=pestfirstgrade]').change(function () {
  310. var shuId = $('#gsmmEditForm select[name=pestfirstgrade]').find("option:selected").val().toString();
  311. $.ajax({
  312. url: "/pestdic/" + shuId,
  313. type: "GET",
  314. data: {id: shuId},
  315. "dataType": "json",
  316. success: function (response) {
  317. $('#gsmmEditForm select[name=pestsecondgrade]').empty();
  318. $('#gsmmEditForm select[name=pestsecondgrade]').append($('<option>').text("-请选择-").attr('value', 0));
  319. $.each(response.data, function (i, obj) {
  320. $('#gsmmEditForm select[name=pestsecondgrade]').append($('<option>').text(obj.fields.pestsecondgrade).attr('value', obj.fields.id));
  321. });
  322. },
  323. error: function () {
  324. showErrorAlert(serverErrorMessage);
  325. }
  326. });
  327. });
  328. $('#gsmmEditForm input[id=picupload1]').click(function (event) {
  329. event.preventDefault();
  330. var imgVal = $('#gsmmEditForm input[id=file]').val();
  331. /*if (imgVal.empty()) {
  332. alert("图片为空,请选择后上传!");
  333. }*/
  334. /*else {*/
  335. var file1 = $('#gsmmEditForm input[id=file]').get(0).files[0];
  336. var formData = new FormData();
  337. formData.append('file', file1);
  338. $.ajax({
  339. url: '/uploadpic/',
  340. data: formData,
  341. type: 'POST',
  342. contentType: false,
  343. processData: false,
  344. success: function (data) {
  345. alert("图片上传成功");
  346. $('#gsmmEditForm input[id=pestpicurl1]').val(data);
  347. $("#gsmmEditForm img[id=pestpic1]").attr("src", data);
  348. $("#pestpic1").show();
  349. },
  350. error: function (jqXHR, textStatus, errorThrown) {
  351. alert(textStatus + ': ' + errorThrown);
  352. }
  353. });
  354. /* }*/
  355. });
  356. $('#gsmmEditForm input[id=picupload2]').click(function (event) {
  357. event.preventDefault();
  358. var file1 = $('#gsmmEditForm input[id=file]').get(0).files[0];
  359. var formData = new FormData();
  360. formData.append('file', file1);
  361. $.ajax({
  362. url: '/uploadpic/',
  363. data: formData,
  364. type: 'POST',
  365. contentType: false,
  366. processData: false,
  367. success: function (data) {
  368. alert("图片上传成功");
  369. $('#gsmmEditForm input[id=pestpicurl2]').val(data);
  370. $("#gsmmEditForm img[id=pestpic2]").attr("src", data);
  371. $("#pestpic2").show();
  372. },
  373. error: function (jqXHR, textStatus, errorThrown) {
  374. alert(textStatus + ': ' + errorThrown);
  375. }
  376. });
  377. });
  378. // Show success alert message
  379. var showSuccessAlert = function (message) {
  380. $.toaster({priority: 'success', title: 'Success', message: message});
  381. }
  382. // Show error alert message
  383. var showErrorAlert = function (message) {
  384. $.toaster({priority: 'danger', title: 'Error', message: message});
  385. }
  386. // Convert form data in JSON format
  387. $.fn.serializeObject = function () {
  388. var o = {};
  389. var a = this.serializeArray();
  390. $.each(a, function () {
  391. if (o[this.name] !== undefined) {
  392. if (!o[this.name].push) {
  393. o[this.name] = [o[this.name]];
  394. }
  395. o[this.name].push(this.value || '');
  396. } else {
  397. if (this.name == 'id' || this.name == 'city' || this.name == 'county' || this.name == 'ke' || this.name == 'shu' || this.name == 'cname' || this.name == 'treelevel') {
  398. o[this.name] = parseInt(this.value) || 0;
  399. } else {
  400. o[this.name] = this.value || '';
  401. }
  402. }
  403. });
  404. return JSON.stringify(o);
  405. };
  406. // Handling form submission for create new employee
  407. $('#gsmmForm').on('submit', function (e) {
  408. var pestname = $('#gsmmForm select[name=pestname]').find("option:selected").val();
  409. var researchperson = $('#gsmmForm select[name=researchperson]').find("option:selected").val();
  410. if (pestname == 0 || researchperson == 0) {
  411. alert("虫种和调查人均不能为空,请选择后再保存数据")
  412. return false
  413. } else {
  414. var pestData = $("#gsmmForm").serializeObject();
  415. var formData = JSON.parse(pestData)
  416. e.preventDefault();
  417. $.ajax({
  418. url: "/create/",
  419. type: 'POST',
  420. data: formData,
  421. dataType: 'JSON',
  422. success: function (arg) {
  423. if (arg.status) {
  424. $('#gsmmModal').modal('hide');
  425. showSuccessAlert("添加信息成功");
  426. } else {
  427. $('#gsmmpModal').modal('hide');
  428. showErrorAlert(arg.message);
  429. }
  430. },
  431. error: function () {
  432. $('#gsmmModal').modal('hide');
  433. showErrorAlert(serverErrorMessage);
  434. }
  435. });
  436. return false;
  437. }
  438. });
  439. // Handling form submission for update employee
  440. $('#gsmmEditForm').on('submit', function (e) {
  441. var pestname = $('#gsmmEditForm select[name=pestname]').find("option:selected").val();
  442. var researchperson = $('#gsmmEditForm select[name=researchperson]').find("option:selected").val();
  443. if (pestname == 0 || researchperson == 0) {
  444. alert("虫种和调查人均不能为空,请选择后再保存数据")
  445. return false
  446. } else {
  447. var pestData = $("#gsmmEditForm").serializeObject();
  448. var formData = JSON.parse(pestData)
  449. e.preventDefault();
  450. $.ajax({
  451. url: '/update/',
  452. type: 'POST',
  453. data: formData,
  454. dataType: 'JSON',
  455. success: function (arg) {
  456. if (arg.code == 1000) {
  457. alert("信息更改成功!!")
  458. showSuccessAlert("信息更改成功!!");
  459. window.location.reload();
  460. } else {
  461. alert(arg.message);
  462. }
  463. }
  464. });
  465. return false;
  466. }
  467. });
  468. });