map方法处理json格式

//map方法
function Map(){
    /**
     * 存放键的数组
     * @type {Array}
     */
    this.keys = new Array();
    /**
     * 存放数据
     * @type {Object}
     */
    this.data = new Object();
    /**
     * 放入一个键值对
     * @param  {String} key
     * @param  {Object} value
     */
    this.put = function(key,value){
        if(this.data[key] == null){
            this.keys.push(key);
        }
        this.data[key] = value;
    };
    /**
     * 获取某键对应的值
     * @param  {String} key
     * @return {Object} value
     */
    this.get = function(key){
        return this.data[key];
    };
    /**
     * 删除一个键值对
     * @param  {String} key
     */
    this.remove = function(key){
        this.keys.remove(key);
        this.data[key] = null;
    };
    /**
     * 遍历Map,执行处理函数
     * @param  {Function}
     */
    this.each = function(fn){
        if(typeof fn != 'function'){
            return;
        }
        var len = this.keys.length;
        for(var i = 0;i < len;i++){
            var k = this.keys[i];
            fn(k,this.data[k],i);
        }
    };
    /**
     * 返回键值对数组(类似JavaentrySet)
     * @return {Object} 键值对对象{key,value}的数组
     */
    this.entrys = function(){
        var len = this.keys.length;
        var entrys = new Array(len);
        for(var i = 0;i < len;i++){
            var tmp = this.keys[i];
            entrys[i] = {
                key : tmp,
                value : this.data[tmp]
            };
        }
        return entrys;
    };
    /**
     * 判断Map是否为空
     * @return {Boolean}
     */
    this.isEmpty = function(){
        return this.keys.length == 0;
    };
    /**
     * 获取键值对数量
     * @return {Number}
     */
    this.size = function(){
        return this.keys.length;
    };
    /**
     * 重写toString
     * @return {String}
     */
    this.toString = function(){
        var s = "{";
        for(var i = 0;i < this.keys.length;i++,s+=','){
            var k = this.keys[i];
            s += k + "=" + this.data[k];
        }
        s += "}";
        return s;
    };
};
// addModal
app.controller('addcldetailModal', ['$rootScope','$http', '$modal','$scope', '$modalInstance', 'httpJesen','msg',function($rootScope, $http, $modal,$scope, $modalInstance, httpJesen,msg) {
    $scope.msg = msg;
    console.log($scope.msg);

    $scope.item =  {};
    // 清空表单
    $scope.reset = function(){
        $scope.item = {};
    };
    // 进入添加页 先清空上次添加表单信息
    $scope.reset();


    // 初始化弹出框信息
    var msgInfo ;
    // 操作时的弹出框
    $scope.open = function () {
        var modalInstance = $modal.open({
            templateUrl: '../tpl/modal.html',
            controller: 'Notice',
            size: 'sm',
            backdrop: 'static',
            resolve: {
                msg: function () {
                    return msgInfo;
                }
            }
        });
    };


    // 取消
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };
    //map方法
    function Map(){
        /**
         * 存放键的数组
         * @type {Array}
         */
        this.keys = new Array();
        /**
         * 存放数据
         * @type {Object}
         */
        this.data = new Object();
        /**
         * 放入一个键值对
         * @param  {String} key
         * @param  {Object} value
         */
        this.put = function(key,value){
            if(this.data[key] == null){
                this.keys.push(key);
            }
            this.data[key] = value;
        };
        /**
         * 获取某键对应的值
         * @param  {String} key
         * @return {Object} value
         */
        this.get = function(key){
            return this.data[key];
        };
        /**
         * 删除一个键值对
         * @param  {String} key
         */
        this.remove = function(key){
            this.keys.remove(key);
            this.data[key] = null;
        };
        /**
         * 遍历Map,执行处理函数
         * @param  {Function}
         */
        this.each = function(fn){
            if(typeof fn != 'function'){
                return;
            }
            var len = this.keys.length;
            for(var i = 0;i < len;i++){
                var k = this.keys[i];
                fn(k,this.data[k],i);
            }
        };
        /**
         * 返回键值对数组(类似JavaentrySet)
         * @return {Object} 键值对对象{key,value}的数组
         */
        this.entrys = function(){
            var len = this.keys.length;
            var entrys = new Array(len);
            for(var i = 0;i < len;i++){
                var tmp = this.keys[i];
                entrys[i] = {
                    key : tmp,
                    value : this.data[tmp]
                };
            }
            return entrys;
        };
        /**
         * 判断Map是否为空
         * @return {Boolean}
         */
        this.isEmpty = function(){
            return this.keys.length == 0;
        };
        /**
         * 获取键值对数量
         * @return {Number}
         */
        this.size = function(){
            return this.keys.length;
        };
        /**
         * 重写toString
         * @return {String}
         */
        this.toString = function(){
            var s = "{";
            for(var i = 0;i < this.keys.length;i++,s+=','){
                var k = this.keys[i];
                s += k + "=" + this.data[k];
            }
            s += "}";
            return s;
        };
    };
    var findMsg = {"categoryId": $scope.msg.cid};
    find = angular.toJson(findMsg);
    $scope.getItemMsg = function () {
        httpJesen.formPost('qcCheck/toCreateClaim', find, function (data) {
            console.log(data);
            var message = data.responseMsg;
            if (data.responseCode == 1) {
                var detialMsg = data.responseData.qcCheckCategory;
                $scope.item.categoryName = detialMsg.categoryName;
                console.log($scope.item.categoryName);
                $scope.itemList = data.responseData.itemList;
                console.log($scope.itemList);
            } else {
                msgInfo = {title: "提示", msg: message, pbtn: "确认", pshow: true, nbtn: "取消", nshow: true};
                $scope.open();
            }
        });
    };
    if($scope.msg.type == "add"){
        $scope.showFlag = 1;
        // 提交新增项
        $scope.getItemMsg();
        $scope.ok = function() {
            console.log($scope.itemList);
            var m = new Map();
            angular.forEach($scope.itemList,function (item,index) {
                if(item.itemValue == undefined){
                    item['itemValue'] = '';
                };
                var value = item.itemName + ',' + item.itemValue;
                m.put(index,value);
            });
            console.log(m.data);
            $scope.item.itemJson = JSON.stringify(m.data);
            $scope.item.categoryId = $scope.msg.cid;
            //$scope.item.customerName =document.getElementById("customerId").value; //$("#customerId option:selected").text();
            console.log($scope.item);
            var params = angular.toJson($scope.item);
            httpJesen.formPost('qcCheck/doCreateClaim',params, function (data) {
                var message = data.responseMsg;
                msgInfo = {title: "提示", msg: message, pbtn: "确认", pshow: true, nbtn: "取消", nshow: false};
                if (data.responseCode == 1) {
                    $scope.open();
                    $scope.query($scope.msg.cid);
                    $modalInstance.close();
                } else {
                    $scope.open();
                }
            });
        };
    }else if($scope.msg.type == "edit"){
        $scope.showFlag = 0;
        var find = {"id": $scope.msg.id};
        find = angular.toJson(find);
        $scope.getItemDetail = function () {
            httpJesen.formPost('qcCheck/toModifyClaim', find, function (data) {
                console.log(data);
                var message = data.responseMsg;
                if (data.responseCode == 1) {
                    data = data.responseData;
                    $scope.item = data;
                    console.log(JSON.parse(data.itemJson));
                    /*if($scope.item.customerId){
                        var params1 = angular.toJson({"id":$scope.item.customerId});
                        httpJesen.formPost('sysCustomerInfo/queryById',params1, function (data) {
                            console.log(data);
                            var name = data.responseData.SysCustomerInfo.customerName;
                            document.getElementById("customerId").value= name;
                        })
                    }*/
                    var eg = new Map();
                    var newitemList = [];
                    angular.forEach(JSON.parse(data.itemJson),function(item,index){
                        eg.put("itemName",item.split(',')[0]);
                        eg.put("itemValue",item.split(',')[1]);
                        console.log(eg.data.itemName);
                        console.log(eg.data.itemValue);
                        var addItem = {
                            itemName:eg.data.itemName,
                            itemValue:eg.data.itemValue
                        };
                        newitemList.push(addItem);
                    });
                    console.log(newitemList);
                    $scope.itemList = newitemList;
                } else {
                    msgInfo = {title: "提示", msg: message, pbtn: "确认", pshow: true, nbtn: "取消", nshow: true};
                    $scope.open();
                }
            });
        };
        $scope.getItemDetail();
        $scope.ok = function() {
            $scope.item.id = $scope.msg.id;
            $scope.item.categoryId = $scope.msg.cid;
            var m = new Map();
            angular.forEach($scope.itemList,function (item,index) {
                if(item.itemValue == undefined){
                    item['itemValue'] = '';
                };
                var value = item.itemName + ',' + item.itemValue;
                m.put(index,value);
            });
            console.log(m.data);
            $scope.item.itemJson = JSON.stringify(m.data);
            console.log($scope.item);
            var params = angular.toJson($scope.item);
            httpJesen.formPost('qcCheck/doModifyClaim',params, function (data) {
                var message = data.responseMsg;
                msgInfo = {title: "提示", msg: message, pbtn: "确认", pshow: true, nbtn: "取消", nshow: false};
                if (data.responseCode == 1) {
                    $scope.open();
                    $scope.query($scope.msg.cid);
                    $modalInstance.close();
                } else {
                    $scope.open();
                }
            });
        };
    }

}]);


猜你喜欢

转载自blog.csdn.net/yaya1286249672/article/details/78235189
今日推荐