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

本文介绍了一种利用缓存数据字典实现字典值获取及下拉框选项过滤的方法。通过定义getDict函数来获取指定类型的关键字对应的字典值,并通过注册过滤器实现对输入值的转换及下拉框选项的动态填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//缓存数据字典
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;
    }
});
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值