关于级联选择器el-cascader的踩坑及解决

1.vue报错:Error in callback for watcher “options“: “TypeError: Cannot read propertie ‘level‘ of null
我的报错使用场景:级联选择器是遍历出来的,数据也是遍历的数组里面的,报错的原因是我删除了option绑定的数组,因为后台接口不需要这个数据
解决:给级联选择器加key,key的值是:new Date().getTime(),在每次数据改变的时候更新key,即this.keyIndex = new Date().getTime()

2.使用自带的filterable检索,并自定义检索方法,:filter-method
在这里插入图片描述
但是这个方法是有默认参数的,当不满足自己的需要,需要传其他参树的时候,
可以使用以下方式:
1.闭包方式:
:filter-method=“filterProduct({everyIndex,itemIndex,list:every.list})”

filterProduct(param){
   
      //param----自定义传递的参数   node----节点    keyword----搜索关键词
      //param.list   当前级联选择器的option
      return (node,keyword)=>{
   
        console.log(param,"换了个写法-param")
        console.log(node,"换了个写法-node")
        console.log(keyword,"换了个写法-keyword")
        if (!!~node.label.toUpperCase().indexOf(keyword.toUpperCase())) {
   
          return true
        }
      }
    },

2.bind方法
:filter-method=“filterOrg.bind(this, param)”

 filterOrg(param, value, data, node) {
   
    // 在 filterOrg 方法中可以访问到传递的参数 param
    console.log(param);

    // 具体的过滤逻辑
    // ...

    // 返回过滤结果
    return true; // 或者根据条件返回 true 或 false
   }

3.自定义搜索方法后,使用slot自定义了节点内容

<span slot-scope="{ node,data }" class="custom-tree-node">
   <span>{
   {
    data.id}}--{
   {
    data.name}}</span>
 </span>

但是搜索出来后和自定义的不一样,只有label的那个字段,问题原因:
搜索的建议面板和原来的dom不是同一个dom
解决方式:
在获取到数据之后,对数据进行整体处理

function handleArr(arr) {
   
  arr.forEach((el) => {
   
    el.labelText =  el.orgId + '-'+el.orgName;
    handleArr(el.childrenList || []);
  });
}
getOrganizationWithCondition({
    templateId: this.templateNo,needChildren:1 }).then(
  (res) => {
   
    if(res.code == 0){
   
      let arr = res.data
      let b = arr
      handleArr(b);
      console.log(b,"机构处理好的数据")
      this.$set(every, "list", b);
        // this.$set(every, "list", res.data);
        // every.list = res.data;
    }
  }
);

处理完之后直接使用:

<span>{
   {
    data.labelText }}</span>```

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-07 02:12:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-07 02:12:06       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-07 02:12:06       82 阅读
  4. Python语言-面向对象

    2023-12-07 02:12:06       91 阅读

热门阅读

  1. 关于数据劫持原理(vue2和vue3)

    2023-12-07 02:12:06       55 阅读
  2. Docker在实际应用开发中的应用-AI生成

    2023-12-07 02:12:06       53 阅读
  3. mysql-binlog,redolog 和 undolog区别

    2023-12-07 02:12:06       55 阅读
  4. 深度学习常用指令(Anaconda、Python)

    2023-12-07 02:12:06       67 阅读
  5. 力扣 572. 另一棵树的子树

    2023-12-07 02:12:06       58 阅读
  6. rabbitmq技术

    2023-12-07 02:12:06       65 阅读
  7. React自定义Hook之useModel hook

    2023-12-07 02:12:06       68 阅读
  8. C++EasyX之跟随鼠标移动的小球

    2023-12-07 02:12:06       72 阅读