version.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * 版本管理相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/28
  6. * @version
  7. */
  8. $(document).ready(function() {
  9. let isAdding = false;
  10. // 初始化数据
  11. initVersion();
  12. // 新增版本
  13. $("#add-version").click(function() {
  14. try {
  15. let [name, standardBill, rationLib, standardBillString, rationLibString] = getAndValidData();
  16. $.ajax({
  17. url: '/version/add',
  18. type: 'post',
  19. data: {name: name,standard_bill_id: standardBill, ration_lib_id: rationLib,
  20. standard_bill: standardBillString, ration_lib: rationLibString},
  21. error: function() {
  22. isAdding = false;
  23. },
  24. beforeSend: function() {
  25. isAdding = true;
  26. },
  27. success: function(response) {
  28. isAdding = false;
  29. if (response.err === 0) {
  30. window.location.reload();
  31. } else {
  32. let msg = response.msg === undefined ? '未知错误' : response.msg;
  33. alert(msg);
  34. }
  35. }
  36. });
  37. } catch (error) {
  38. alert(error);
  39. }
  40. });
  41. // 选择省份后读取数据
  42. $("select[name='standard_bill_province'],select[name='ration_lib_province']").change(function() {
  43. let name = $(this).attr('name');
  44. let billListData = billList === undefined ? [] : JSON.parse(billList);
  45. let rationLibData = rationList === undefined ? [] : JSON.parse(rationList);
  46. if (billListData.length <= 0 || rationLibData.length <= 0) {
  47. return false;
  48. }
  49. let sourceData = name === 'standard_bill_province' ? billListData : rationLibData;
  50. let selectedId = $(this).val();
  51. if (sourceData[selectedId] === undefined) {
  52. return false;
  53. }
  54. let defaultString = name === 'standard_bill_province' ? '请选择标准清单' : '请选择定额库';
  55. let html = '<option value="">' + defaultString + '</option>';
  56. for(let tmp of sourceData[selectedId]) {
  57. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  58. html += tmpHtml;
  59. }
  60. // 渲染
  61. let targetSelector = name === 'standard_bill_province' ? $("select[name='standard_bill']") : $("select[name='ration_lib']");
  62. targetSelector.children('option').remove();
  63. targetSelector.html(html);
  64. });
  65. });
  66. /**
  67. * 初始化
  68. *
  69. * @return {void|boolean}
  70. */
  71. function initVersion() {
  72. if (province === undefined) {
  73. alert('初始化失败!');
  74. return false;
  75. }
  76. province = JSON.parse(province);
  77. if (province.length <= 0) {
  78. alert('省份数据加载失败!');
  79. return false;
  80. }
  81. let billProvinceElement = $("select[name='standard_bill_province']");
  82. let rationProvinceElement = $("select[name='ration_lib_province']");
  83. let html = '';
  84. for (let tmp of province) {
  85. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  86. html += tmpHtml;
  87. }
  88. billProvinceElement.children('option').first().after(html);
  89. rationProvinceElement.children('option').first().after(html);
  90. }
  91. /**
  92. * 校验数据
  93. *
  94. * @return {Array}
  95. */
  96. function getAndValidData() {
  97. let name = $("input[name='version_name']").val();
  98. let standardBill = $("select[name='standard_bill']").val();
  99. let rationLib = $("select[name='ration_lib']").val();
  100. if (name === '') {
  101. throw '版本名字不能为空';
  102. }
  103. if (standardBill === '' || standardBill === undefined) {
  104. throw '请选择标准清单库';
  105. }
  106. if (rationLib === '' || rationLib === undefined) {
  107. throw '请选择定额库';
  108. }
  109. let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
  110. let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
  111. return [name, standardBill, rationLib, standardBillString, rationLibString];
  112. }