| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 | /** * 版本管理相关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 = '<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);    });    // 添加    $(".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 = '<option value="' + tmp.id + '">' + tmp.name + '</option>';        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];}
 |