123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- /**
- * 版本管理相关js
- *
- * @author CaiAoLin
- * @date 2017/7/28
- * @version
- */
- $(document).ready(function() {
- let isAdding = false;
- // 初始化数据
- initVersion();
- // 新增版本
- $("#add-version").click(function() {
- try {
- let [name, standardBill, rationLib, standardBillString, rationLibString] = getAndValidData();
- $.ajax({
- url: '/version/add',
- type: 'post',
- data: {name: name,standard_bill_id: standardBill, ration_lib_id: rationLib,
- standard_bill: standardBillString, ration_lib: rationLibString},
- error: function() {
- isAdding = false;
- },
- beforeSend: function() {
- isAdding = true;
- },
- success: function(response) {
- isAdding = false;
- if (response.err === 0) {
- window.location.reload();
- } else {
- let msg = response.msg === undefined ? '未知错误' : response.msg;
- alert(msg);
- }
- }
- });
- } catch (error) {
- alert(error);
- }
- });
- // 选择省份后读取数据
- $("select[name='standard_bill_province'],select[name='ration_lib_province']").change(function() {
- let name = $(this).attr('name');
- let billListData = billList === undefined ? [] : JSON.parse(billList);
- let rationLibData = rationList === undefined ? [] : JSON.parse(rationList);
- if (billListData.length <= 0 || rationLibData.length <= 0) {
- return false;
- }
- let sourceData = name === 'standard_bill_province' ? billListData : rationLibData;
- let selectedId = $(this).val();
- if (sourceData[selectedId] === undefined) {
- return false;
- }
- let defaultString = name === 'standard_bill_province' ? '请选择标准清单' : '请选择定额库';
- let html = '<option value="">' + defaultString + '</option>';
- for(let tmp of sourceData[selectedId]) {
- let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
- html += tmpHtml;
- }
- // 渲染
- let targetSelector = name === 'standard_bill_province' ? $("select[name='standard_bill']") : $("select[name='ration_lib']");
- targetSelector.children('option').remove();
- targetSelector.html(html);
- });
- });
- /**
- * 初始化
- *
- * @return {void|boolean}
- */
- function initVersion() {
- if (province === undefined) {
- alert('初始化失败!');
- return false;
- }
- province = JSON.parse(province);
- if (province.length <= 0) {
- alert('省份数据加载失败!');
- return false;
- }
- let billProvinceElement = $("select[name='standard_bill_province']");
- let rationProvinceElement = $("select[name='ration_lib_province']");
- let html = '';
- for (let tmp of province) {
- let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
- html += tmpHtml;
- }
- billProvinceElement.children('option').first().after(html);
- rationProvinceElement.children('option').first().after(html);
- }
- /**
- * 校验数据
- *
- * @return {Array}
- */
- function getAndValidData() {
- let name = $("input[name='version_name']").val();
- let standardBill = $("select[name='standard_bill']").val();
- let rationLib = $("select[name='ration_lib']").val();
- if (name === '') {
- throw '版本名字不能为空';
- }
- if (standardBill === '' || standardBill === undefined) {
- throw '请选择标准清单库';
- }
- if (rationLib === '' || rationLib === undefined) {
- throw '请选择定额库';
- }
- let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
- let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
- return [name, standardBill, rationLib, standardBillString, rationLibString];
- }
|