在vue2 项目中的src文件夹下新建filter目录,新建filter.js文件,然后再main.js文件中全局引入。 功能:主要用于将后台返回数据中的码值转换成汉字,适用于多维数组
// arr(被循环的数组), key(要被转换的码值),keyCode(被转换的码值的key),label(需要显示的汉字的key),children='children'(多维数组的时候子集合的key)
Vue.filter('codeToLabel', function(arr, key,keyCode,label,children='children') {
let name='';
recurrence(arr, key);
function recurrence(arr, key){
const length = arr.length
for (let i = 0; i < length; i++) {
const code = arr[i][keyCode]
if(code==key){
name=arr[i][label];
}else if(arr[i][children]){
recurrence(arr[i][children],key);
}
}
}
return name||key;
})
在组件中使用,首先在main.js中全局引入,然后使用
<span>
{{ arr|codeToLabel(scope.row.code,'code','label','child') }}
</span>