metaConfig.html 12.4 KB
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>Yoho!Buy运营平台</title>
    <script src="/ufoPlatform/js/include.js"></script>
    <style type="text/css">
        .nav li {float:left; list-style:none;}
        .nav li a{float:left;text-decoration:none;padding:0.2em 1.6em;border-right:1px solid white;color:black; font-size:14px;}
    </style>
</head>
<body class="easyui-layout">
<div region="north" style="height:240px;">
    <script>
        document.write(addHead('订单管理', '配置管理'));
    </script>
    <br>
    <div style="margin-left: 10px;margin-top: 30px">
        <hr style="border:1px solid #ddd;"><br>
        <div style="border:1px solid #ddd;border-radius:5px 5px 5px 5px;">
            <div style="margin-left: 10px;margin-top: 20px;margin-bottom: 20px">
                <label>编号:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                <input id="code" type="text" class="easyui-textbox" style="width:150px">

                <label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名称:</label>
                <input id="title" type="text" class="easyui-textbox" style="width:150px"/>
                <a id="searchBtn" class="btn-info">查询</a>
                <a id="allBtn" class="btn-success">全部</a>
            </div>
        </div>
    </div>
</div>

<div id="orderList" region="center" style="margin-left: 10px;border:1px solid #ddd;border-radius:5px 5px 5px 5px;">
    <table id="configListTable"></table>
</div>

<script>
    var code;
    var keyValueMap = {};
    // 值是比例(0~1四位小数)的键
    var keyScaleList =[];
    // 值是整数的键
    var keyIntList =[];
    // 值是浮点型的键
    var keyDoubleList =[];
    $(function() {

        $("#searchBtn").linkbutton({
            iconCls : "icon-search",
            onClick : function() {
                $("#configListTable").datagrid("load", {
                    code : $("#code").val(),
                    title : $("#title").val(),
                });
            }
        });

        //全部按钮
        $("#allBtn").linkbutton({
            iconCls: "icon-import",
            onClick: function () {
                $("#code").textbox('setValue','');
                $("#title").textbox('setValue','');
                $("#configListTable").datagrid("load", {
                });
            }
        });

        getConfigList();
    });

    function getConfigList(){
        $("#configListTable").myDatagrid({
            fit: true,
            fitColumns: true,
            striped: true,
            url: contextPath + "/metaConfig/list",
            method: 'POST',
            queryParams: {
            },
            loadFilter: function (data) {
                var temp = defaultLoadFilter(data);
                temp=null==temp?[]:temp;
                temp.rows = temp.list;
                return temp;
            },

            columns: [[{
                title: "编号",
                field: "code",
                width: 20
            },  {
                title: "标题",
                field: "title",
                align: "center",
                width: 30
            },{
                title: "功能描述",
                field: "desc",
                align: "center",
                width: 50,
            }, {
                title: "创建时间",
                field: "createTimeStr",
                align: "center",
                width: 20,
            }, {
                title: "操作",
                field: "operations",
                align: "center",
                width: 30,
                formatter: function (value, rowData) {
                    var str = "<a role='detail' dataId='"+ rowData.code + "' style='margin-left:10px;background-color: #5cb85c !important;'>编辑</a>";
                    if(rowData.code == "super_enter_stage_func"){
                        str = "<a role='detail' dataId='"+ rowData.code + "' style='margin-left:10px;opacity:0.5;background-color: #5cb85c !important;'>编辑</a>";
                    }
                    return str;
                }}
                ]],
            cache: false,
            pagination: true,
            pageSize: 10,
            idField: "id",
            singleSelect: true,
            onLoadSuccess: function () {
                var me = $(this);
                me.datagrid("getPanel").find("a[role='detail']").linkbutton({
                    onClick: function () {
                        code = $(this).attr("dataId");
                        if(code == "super_enter_stage_func"){
                            return false;
                        }
                        editRow()
                    }
                });
            }
        });
    }

    function editRow() {
        var div = $("<div>").appendTo($(document.body));
        var title = "编辑";
        var message = "确认修改吗?";
        $(div).myDialog({
            width: "780px",
            height: "550px",
            title: title,
            href: contextPath + "/html/orderManage/editConfig.html?time_version=" + new Date().getTime(),
            modal: true,
            collapsible: true,
            cache: false,
            buttons: [{
                id: "saveBtn",
                text: "保存",
                handler: function () {
                    $.messager.confirm("确认", message, function (flag) {
                        if (flag) {
                            var url = contextPath + "/metaConfig/update";
                            //debugger;
                            // 校验整数
                            if(keyIntList.length > 0){
                                for(var i=0; i< keyIntList.length; i++){
                                    var val = $("#metaConfigEditForm [name = '"+keyIntList[i]+"']").val();
                                    var reg = /^\d+$/;
                                    if(!reg.test(val)){
                                        $.messager.alert("提示", keyValueMap[keyIntList[i]] + '字段必须为非负整数!', "error");
                                        return false;
                                    }
                                }
                            }

                            // 校验比例(0~1两位四位小数)
                            if(keyScaleList.length > 0){
                                for(var i=0; i< keyScaleList.length; i++){
                                    var val = $("#metaConfigEditForm [name = '"+keyScaleList[i]+"']").val();
                                    var reg = /^(0(.\d{1,4})?)$|^(1(.0{1,2})?)$/;
                                    if(!reg.test(val)){
                                        $.messager.alert("提示", keyValueMap[keyScaleList[i]] + '字段必须为0~1之间的小数(最多保留四位)!', "error");
                                        return false;
                                    }
                                }
                            }

                            // 校验浮点数
                            if(keyDoubleList.length > 0){
                                for(var i=0; i< keyDoubleList.length; i++){
                                    var reg = /^[0-9]+(.[0-9]{1,4})?$/;
                                    var val = $("#metaConfigEditForm [name = '"+keyDoubleList[i]+"']").val();
                                    if(!reg.test(val)){
                                        $.messager.alert("提示", keyValueMap[keyDoubleList[i]] + '字段必须为数字(最多保留四位小数)!', "error");
                                        return false;
                                    }
                                }
                            }

                            //把form转化成json
                            var a = toJSON();
                            delete a.id;
                            delete a.code;
                            delete a.title;
                            delete a.desc;
                            delete a.value;
                            $("#metaConfigEditForm #value").val(JSON.stringify(a));

                            $("#metaConfigEditForm").form("submit", {
                                url: url,
                                onSubmit: function () {
                                    if (!$("#metaConfigEditForm").form("validate")) {
                                        return false;
                                    }
                                    $.messager.progress({
                                        title: "正在执行",
                                        msg: "正在执行,请稍后..."
                                    });
                                    return true;
                                },
                                success: function (data) {
                                    $.messager.progress("close");
                                    data = JSON.parse(data);
                                    if (data.code == 200) {
                                        $(div).dialog("close");
                                        $("#configListTable").myDatagrid("reload");
                                        $.messager.show({
                                            title: "提示",
                                            msg: title + "成功!",
                                            height: 120
                                        });
                                    } else {
                                        $.messager.alert("失败", data.message, "error");
                                    }
                                }
                            });
                        }
                    });
                }
            }, {
                text: "关闭",
                iconCls: "icon-cancel",
                handler: function () {
                    $(div).dialog("close");
                }
            }]
        });
    }

    /**
     * form表单转JSON格式
     */
    function toJSON() {
        var keyValues = $('#metaConfigEditForm').serializeArray();
        var pattern=/\[(\d+)\]/;
        var filter_keys = [],
            normal_keys = keyValues.filter(function (v, i) {
                if (~v.name.indexOf('.')||~pattern.test(v.name)) {
                    filter_keys.push(v);
                    return false
                }
                return true;
            })

        var resultJson = {};
        filter_keys.forEach(function (v, i, ary) {
            var v_ary = v.name.split('.'),
                v_last = v_ary.length - 1;

            var deal = function (obj, j, array) {
                var me = arguments.callee,
                    islast = j === v_last ? true : false;
                if (islast&&!pattern.test(array[j])) {
                    if(typeof obj =='array'){
                        return obj.push(v.value);
                    }else{
                        if(obj[array[j]]){
                            obj[array[j]]+=','+v.value||'';
                        }else{
                            obj[array[j]] = v.value || '';
                        }
                        return;
                    }
                }else if(islast&&pattern.test(array[j])){
                    var name = array[j].substr(0, array[j].indexOf('['));
                    var index = array[j].match(pattern)[1];
                    if (!obj[name]) {
                        obj[name] = [];
                    }
                    if (obj[name].length<= index) {
                        return obj[name].push(v.value||'');
                    }
                }
                var nextObj;
                if (pattern.test(array[j])) {
                    var name = array[j].substr(0, array[j].indexOf('['));
                    var index = array[j].match(pattern)[1];
                    if (!obj[name]) {
                        obj[name] = [];
                    }
                    if (obj[name].length<= index) {
                        nextObj={};
                        obj[name].push(nextObj);
                    }else{
                        nextObj=obj[name][index];
                    }
                } else {
                    if (!obj[array[j]]) {
                        obj[array[j]] = {};
                    }
                    nextObj=obj[array[j]];
                }
                return me(nextObj, j + 1, array);
            }
            deal(resultJson, 0, v_ary);
        });
        normal_keys.forEach(function (v) {
            resultJson[v.name] = v.value;
        });
        return resultJson;
    }
</script>
</body>
</html>