字典方法
字典相关的方法,以下方法均为promise异步的方法。
getLabelByCode
通过字典值获取字典标签
js
/**
* val: 字典值
* dictType: 字典类型
* defaultVal: 找不到时的默认值 默认值为空字符串
*/
getLabelByCode( val, dictType, defaultVal = "")getCodeByLabel
通过字典标签获取字典值
js
/**
* label: 标签值
* dictType: 字典类型
* defaultVal: 找不到时的默认值 默认值为空字符串
*/
getCodeByLabel( label, dictType, defaultVal = "")getLabelByCodes
通过字典值数组或字符串获取字典标签数组
js
// 默认配置
const defaultOptions = {
defaultVal: "", // 找不到时的默认值 默认值为空字符串
// 格式化显示 arr为获取到的字典对象数组,format为字典包配置的format
formatFun: (arr,
format) => {
return arr
.map((item) => {
return item[format.label]
})
.join(",")
},
// 当vals是字符串时 使用spacer隔开来表示多个 默认为 英文逗号隔开
spacer: ","
}
/**
* vals: 字典值数组或字符串
* dictType: 字典类型
* options: 配置项
*/
getLabelByCodes(vals, dictType, options)getCodeByLabels
通过字典标签数组或字符串获取字典值数组
js
// 默认配置
const defaultOptions = {
defaultVal: "", // 找不到时的默认值 默认值为空字符串
// 格式化显示 arr为获取到的字典对象数组,format为字典包配置的format
formatFun: (arr,
format) => {
return arr
.map((item) => {
return item[format.value]
})
.join(",")
},
// 当labels是字符串时 使用spacer隔开来表示多个 默认为 英文逗号隔开
spacer: ","
}
/**
* labels: 字典值数组或字符串
* dictType: 字典类型
* options: 配置项
*/
getCodeByLabels(labels, dictType, options)getDictObjByDictTypes
通过字典类型数组或字符串获取字典数据对象
js
/*
* dictTypes: 字典类型数组或字符串(多个时使用英文逗号隔开)
**/
getDictObjByDictTypes(dictTypes)
// 假设参数为 "dictType1,dictType2" 或 ["dictType1", "dictType2"] 返回数据示例
{
"dictType1": [],
"dictType2": []
}getItemByCode
通过字典值获取字典数据对象
js
/**
* val: 字典值
* dictType: 字典类型
* defaultVal: 找不到时的默认值 默认值为空字符串
* */
getItemByCode( val, dictType, defaultVal = "")温馨提示
上述方法均为promise异步模式,不可在页面上(即template模板里面)直接使用此方法来显示数据。
可在页面上使用的方法
字典包提供了一些方法,可用于在页面上直接使用。
如需在页面上(即template模板里面)使用,请使用以下方法:
getLabelByCodeFilter 代替 getLabelByCode,参数一致
getCodeByLabelFilter 代替 getCodeByLabel,参数一致
getLabelByCodesFilter 代替 getLabelByCodes,参数一致
getCodeByLabelsFilter 代替 getCodeByLabels,参数一致

