解決Element-ui中 el-cascader 級聯選擇器,最后一級數據為空顯示暫無數據問題


1、出現問題bug: el-cascader控件,最后一級出現空白 暫無數據

2、問題原因分析

經過調試分析出現空級聯原因是:由於數據是從后台傳遞過來的,當后端的哥們使用遞歸算出菜單,然后轉換成json傳遞到前端的時候。就會出現 最底層 的子項中 的 children 為空數組,這樣就會造成,空級聯 的bug存在。

 3、解決辦法: 使用遞歸的方式,將最底層中的 children設為undefined

3.1 html 代碼

<el-cascader :options="typeOpt" v-model="formInfo.TypeList" placeholder="請選擇文章分類"></el-cascader>

3.2 JS處理代碼

<script>
export default {
  data() {
    return {
      typeOpt: [],       // 分類列表
      subCatValue: [],   // 分類值
    }
  },
  methods: {
    // 獲取分類列表
    getTypelist() {
      this.$http('/admin/articletype/getselectlist').then(res => {
        if (res.status) {
          this.typeOpt = this.formatData(res.data);
        }
      })
    },
    // 格式化數據,遞歸將空的children置為undefined
    formatData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          // children若為空數組,則將children設為undefined
          data[i].children = undefined;
        } else {
          // children若不為空數組,則繼續 遞歸調用 本方法
          this.formatData(data[i].children)
        }
      }
      return data;
    }
  }
}
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM