【study】多选框每次根据keyword返回结果,怎么根据enName匹配chName

76 阅读1分钟
  • 背景:有这么一个接口,可以返回公司员工的账号enName名字,现在需要根据enName匹配chName,接口每次根据keyWord返回模糊查询符合条件的值,并没有返回所有的员工值
  • 注:解决方法,就是把每次匹配出来的值保存下来,累加
原有数据:['tanglijianxue', 'xiongda']

需要数据:
[
  {enName:'tanglijianxue', chName: '棠梨煎雪'},
  {enName:'xiongda', chName: '熊大'},
]
data(){
  selectedUserInfo: [], // 选中的enName的值
}
methods: {
  change(val){
      this.changeUserName(val); // val: ['tanglijianxue', 'xiongda']
  },
  changeUserName(val) {
            if (val.length > this.selectedUserInfo.length) {
                val.forEach((i) => {
                    // this.options是接口返回的值
                    const item = this.options.filter((j) => i === j.value)[0];
                    if (item) {
                        this.selectedUserInfo.push(item);
                        // 累加之后,会有重复,所以要做一次去重处理
                        const hash = {};
                        this.selectedUserInfo = this.selectedUserInfo.reduce((preVal, curVal) => {
                            // eslint-disable-next-line no-unused-expressions
                            hash[curVal.id] ? ' ' : hash[curVal.id] = true && preVal.push(curVal);
                            return preVal;
                        }, []);
                    }
                });
            } else {
               // 这里是多选删除其中某一项的时候,需要val和已经选择好的selectedUserInfo进行对比,然后存到一个新数组arr中,在对selectedUserInfo重新赋值
                const arr = [];
                val.forEach((i) => {
                    this.selectedUserInfo.forEach((j) => {
                        if (i === j.mis) {
                            arr.push(j);
                        }
                    });
                });
                this.selectedUserInfo = arr;
            }
            this.$emit('changeUser', this.selectedUserInfo);
        },

}