/** * 版本管理相关js * * @author CaiAoLin * @date 2017/7/28 * @version */ $(document).ready(function() { let isAdding = false; let model = ''; // 初始化数据 initVersion(); // 新增版本 $("#add-version").click(function() { try { let [name, standardBill, rationLib, standardBillString, rationLibString] = getAndValidData(model); let id = $("#version-id").val(); let postData = {}; let url = '/version/add'; switch (model) { case 'all': postData = {model: model, name: name, standard_bill_id: standardBill, ration_lib_id: rationLib, standard_bill: standardBillString, ration_lib: rationLibString}; break; case 'bill': url = '/version/addLib'; postData = {model: model, standard_bill_id: standardBill, standard_bill: standardBillString, id: id}; break; case 'ration': url = '/version/addLib'; postData = {model: model, ration_lib_id: rationLib, ration_lib: rationLibString, id: id}; break; } $.ajax({ url: url, type: 'post', data: postData, 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 = ''; for(let tmp of sourceData[selectedId]) { let tmpHtml = ''; html += tmpHtml; } // 渲染 let targetSelector = name === 'standard_bill_province' ? $("select[name='standard_bill']") : $("select[name='ration_lib']"); targetSelector.children('option').remove(); targetSelector.html(html); }); // 添加 $(".add-version").click(function() { model = $(this).data('model'); switch (model) { case 'all': $("#name-area").show(); $("#bill-area").show(); $("#ration-area").show(); $("#add-version-title").text('添加新版本'); break; case 'bill': $("#name-area").hide(); $("#bill-area").show(); $("#ration-area").hide(); $("#add-version-title").text('添加标准清单'); break; case 'ration': $("#name-area").hide(); $("#bill-area").hide(); $("#ration-area").show(); $("#add-version-title").text('添加定额库'); break; } $("#addversion").modal('show'); }); // 移除操作 let isDeleting = false; $(".remove-version").click(function() { let model = $(this).data('model'); let id = $("#version-id").val(); let deleteId = $(this).data('id'); deleteId = parseInt(deleteId); if (model === undefined || model === '' || isNaN(deleteId)) { return false; } if (isDeleting) { return false; } $.ajax({ url: '/version/deleteLib', type: 'post', data: {id: id, model: model, delete_id: deleteId}, error: function() { isDeleting = false; }, beforeSend: function() { isDeleting = true; }, success: function(response) { isDeleting = false; if (response.err === 0) { window.location.reload(); } else { let msg = response.msg === undefined ? '未知错误' : response.msg; alert(msg); } } }); }); }); /** * 初始化 * * @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 = ''; html += tmpHtml; } billProvinceElement.children('option').first().after(html); rationProvinceElement.children('option').first().after(html); } /** * 校验数据 * * @param {String} model * @return {Array} */ function getAndValidData(model) { let name = $("input[name='version_name']").val(); let standardBill = $("select[name='standard_bill']").val(); let rationLib = $("select[name='ration_lib']").val(); if (name === '' && model === 'all') { throw '版本名字不能为空'; } if ((model === 'all' || model === 'bill') && (standardBill === '' || standardBill === undefined)) { throw '请选择标准清单库'; } if ((model === 'all' || model === 'ration') && (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]; }