angularJs通过过滤器实现获取数据字典

//缓存数据字典
var dicMap =  JSON.parse(dictData);
/**
 * 获取字典值的方法
 * @param key  关键字
 * @param type 大类
 * @return 返回结果对像 success为true,则value为字典值
 */
arm.getDict = function(key, type)
{
    var result = {};
    result.success = false;
    var dictValue;
    if (dicMap[type] && dicMap[type][key])
    {
        dictValue = dicMap[type][key].label;
    }
    if (dictValue)
    {
        result.success = true;
        result.value = dictValue;
    }
    return result;
};


// 注册表格字典值过滤器
arm.filter('Dict', function($rootScope , $http , $q)
{
    return function(input , type)
    {
        var returnVal = input;
        if (type)
        {
            var result = arm.getDict(input, type);
            if (result.success)
            {
                returnVal = result.value;
            }
        }


        return returnVal;
    }
});




//注册下拉框表格字典值
arm.filter('dictOption', function()
{
    /**
     * data 循环的列表
     * key 对像的key属性的属性名
     * name 对像的显示属性的属性名
     * type 字典大类
     */
    return function(data , key , name , type)
    {
        if (data && type)
        {
            //循环对每个选项进行过滤,过滤的结题是按
            angular.forEach(data, function(item , index)
            {
                //如果有这2个属性
                if (item[key] && item[name])
                {
                    //如是取到字典值
                    var result = arm.getDict(item[key], type);
                    if (result.success)
                    {
                        item[name] = result.value;
                    }
                }
            });
        }
        return data;
    }
});


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部