级联选择器拼音搜索

级联选择器拼音搜索

1、安装插件pinyin-pro

cnpm install pinyin-pro

2、核心代码如下

<el-cascader
	ref="productManagerRef"
	:props="productManagerProps"
	filterable
	:filter-method="filterMethod"
	v-model="productManagerAssign"
	:options="productManagerList"
	:show-all-levels="false"
	@change="productInfo"
></el-cascader>
import { pinyin } from 'pinyin-pro';
function filterMethod(node: any, keyword: string) {
   if (node.data.roleName.includes(keyword)) {
     return true;
   }
   if (!node.data.pinyin) {
     try {
       node.data.pinyin = pinyin(node.data.roleName, { toneType: 'none' }).replace(/ /g, '');
     } catch (error) {
       node.data.pinyin = '';
     }
   }
   return node.data.pinyin.includes(keyword);
 }

3、注意:在change方法中清空搜索值,需要在获取的展示数据中添加pin对应字段

function getProductManager() {
let params = {
	nickName: null,
};
findAllRoleInfosApi(params).then((res: any) => {
	let { secondRoleTypes, firstRoleTypes } = res.data;
	let allRoles = res.data.allRoles;
	for (let i = 0; i < firstRoleTypes.length; i++) {
			firstRoleTypes[i].children = [];
			for (let j = 0; j < secondRoleTypes.length; j++) {
				if (firstRoleTypes[i].roleId === secondRoleTypes[j].parentRoleId) {
					firstRoleTypes[i].children.push(secondRoleTypes[j]);
				}
				secondRoleTypes[j].children = [];
				for (let k = 0; k < allRoles.length; k++) {
					if ( secondRoleTypes[j].roleId === allRoles[k].parentRoleId ) {
					// 重点在这!!!!
						allRoles[k].pinyin = pinyin(allRoles[k].roleName, { toneType: 'none' }).replace(/ /g, '');
						secondRoleTypes[j].children.push(allRoles[k]);
					}
				}
			}
	}
	state.productManagerList = firstRoleTypes;
});

change方法:中添加productManagerRef.value.searchInputValue = '';

function productInfo() {
	// 清空搜索值
	productManagerRef.value.searchInputValue = '';
	// 用来关闭cascader下拉框
	productManagerRef.value.handleInput();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值