Newer
Older
EMS-WEB-3.0 / src / main / webapp / s / app / company / company-info-edit.js
zhangyingjie on 19 Dec 2020 5 KB 增加组织管理功能
function  companiseToList(companies) {
    let list = [];
    for (let company of companies) {
        let item = {}
        item.id = company.id
        item.title = company.companyName;
        item.parentCompanyId = company.parentCompanyId
        list.push(item)
    }
    return list;
}

function listToTreeList(list) { // 将普通列表转换为树结构的列表
    if (!list || !list.length) {
        return []
    }
    let treeListMap = {};
    for (let item of list) {
        treeListMap[item.id] = item
    }
    for (let i = 0; i < list.length; i++) {
        if ((list[i].parentCompanyId || list[i].parentCompanyId === 0) && treeListMap[list[i].parentCompanyId]) {
            if (!treeListMap[list[i].parentCompanyId].subs) {
                treeListMap[list[i].parentCompanyId].subs = []
            }
            treeListMap[list[i].parentCompanyId].subs.push(list[i]);
            list.splice(i, 1);
            i--
        }
    }
    return list
}

var CompanyInfoEdit = function () {
    return{

        initForms:function(){

            var form = $('#submit_form');
            var error = $('.alert-error', form);
            var success = $('.alert-success', form);

            var data = $("#companies").val();
            var val = eval("("+data+")");
            var list = companiseToList(val);
            var tree = listToTreeList(list);

            comboTree = $('#parentCompanySelect').comboTree({
                source : tree,
                isMultiple: false,
                cascadeSelect: true,
                collapse: false
            });
            comboTree.setSource(tree);
            if($("#parentCompanyId").val()){
                console.log('setSelection',$("#parentCompanyId").val())
                comboTree.setSelection([$("#parentCompanyId").val()])
            }
            comboTree.onChange(function(){
                var selectedIds = comboTree.getSelectedIds();
                $('#parentCompanyId').val(selectedIds)
                console.log(selectedIds)
            })

            $('#cancelBtn').live('click', function (e) {
                location.href = $("#context").val() + "/content/company/company-info-list.jsp";
            });

            form.validate({
                doNotHideMessage: true, //this option enables to show the error/success messages on tab switch.
                errorElement: 'span', //default input error message container
                errorClass: 'validate-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                rules: {
                    companyName: {
                        required: true
                    },
                    parentCompanyName: {
                        required: true
                    }
                },
                messages: { // custom messages for radio buttons and checkboxes
                    companyName: {
                        required: "请输入组织名称"
                    },
                    parentCompanyName:{
                        required: "请选择父级组织"
                    }
                },

                errorPlacement: function (error, element) { // render error placement for each input type
                    error.insertAfter(element); // for other inputs, just perform default behavoir
                },

                invalidHandler: function (event, validator) { //display error alert on form submit
                    success.hide();
                    error.show();
                    App.scrollTo(error, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.help-inline').removeClass('ok'); // display OK icon
                    $(element)
                        .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change dony by hightlight
                    $(element)
                        .closest('.control-group').removeClass('error'); // set error class to the control group
                },

                success: function (label) {
                    label
                        .addClass('valid ok') // mark the current input as valid and display OK icon
                        .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
                },

                submitHandler: function (form) {
                    var formdata = $('#submit_form').serialize();
                    console.log('submit', formdata);

                    $.ajax( {
                        type: "POST",
                        url: $('#context').val()+"/company/company-info-save.do",
                        data: $('#submit_form').serialize(),
                        success: function(data) {

                            var jData = eval(data);
                            if(jData.success==true) {

                                location.href = $("#context").val() + "/content/company/company-info-list.jsp";
                            }
                            else{

                                success.hide();
                                $("#errorMessage").html(jData.message);
                                error.show();
                            }
                        },
                        error:function(request){
                            success.hide();
                            error.show();
                        }
                    });

                }

            })

        }
    }
}();