欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue2+elementUI的el-tree的懶加載功能

 更新時(shí)間:2022年09月23日 16:48:22   作者:懶啦  
這篇文章主要介紹了vue2+elementUI的el-tree的懶加載,文中給大家提到了element?ui?中?el-tree?實(shí)現(xiàn)懶加載的方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

lazy 屬性為true 時(shí)生效
lazy ----> 是否懶加載子節(jié)點(diǎn),需與 load 方法結(jié)合使用

isLeaf可以提前告知 Tree 某個(gè)節(jié)點(diǎn)是否為葉子節(jié)點(diǎn),從而避免在葉子節(jié)點(diǎn)前渲染下拉按鈕。

HTML部分

<el-tree
          class="filter-tree"
          ref="tree"
          accordion
          :data="leftData"
          @node-click="handleNodeClick"
          node-key="listId"
          :current-node-key="currentNodeKey"
          :highlight-current="true"
          :props="defaultProps"
          :default-expanded-keys="idArr"
          lazy
          :load="loadNode"
        >
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <el-tooltip
              class="item"
              effect="dark"
              :content="node.label"
              placement="top-start"
            >
              <span :id="data.listId">{{ node.label }}</span>
            </el-tooltip>
          </span>
 </el-tree>

JS部分

import {
  getMenuList,
  getNodeMenuList,
  getDataList,
} from "@/api/index";
export default {
data() {
    return {
      defaultProps: {
        children: "children",
        label: "name",
        isLeaf: "isLeaf",
      },
      currentNodeKey: "",
      leftData:[],
    }
  },
  methods:{
	// 懶加載獲取數(shù)據(jù)
    loadNode(node, resolve) {
      if (node.level === 0) {// 第一層數(shù)據(jù)
      getMenuList({}).then((res) => {
      //左面導(dǎo)航欄省份信息
      if (res.data.resp_code == "200") {
        let data = res.data.datas;
        for (let key in data) {
          this.leftData.push({
            name: key,
            listId: key,
          });
        }
      }
    });
        return resolve(this.leftData);
      }
      if (node.level === 1) {// 第二層數(shù)據(jù)
        let provinceName = {
          provinceName: node.label, //上海
        };
        let twoList = [];
        getNodeMenuList(provinceName).then((res) => {
          res.data.datas.forEach((items) => {
            twoList.push({
              listId: items.listID,
              name: items.nodeName,
              provinceName: items.provinceName,
              children: [],
            });
            twoList.isLeaf = true;
          });
        });
        setTimeout(() => {
          resolve(twoList);
        }, 1000);
      }
      if (node.level == 2) {//第三層數(shù)據(jù)
        let obj = {
          nodeName: node.data.name,
          provinceName: node.data.provinceName,
        };
        let children = [];
        getDataList(obj).then((res) => {
          if (res.data.resp_code == "200") {
            res.data.datas.forEach((item, index) => {
              node.data.children.push({
                description: item.description,
                links: item.links,
                listId: item.listId,
                name: item.name,
                provinceName: item.provinceName,
                isLeaf: true,// 判斷是不是子節(jié)點(diǎn)(最后一層數(shù)據(jù) 是否顯示下拉圖標(biāo)) 如果不顯示下拉圖標(biāo)為true 如果顯示就可以不寫
              });
            });
          }
        });
        resolve(node.data.children);
      }
      if (node.level > 2) {
        resolve([]);
      }
    },
}
}

element ui 中 el-tree 實(shí)現(xiàn)懶加載

在上次 element UI 中的 el-tree 實(shí)現(xiàn) checkbox 單選框功能,及 bus 傳遞參數(shù)的方法 篇章中有提到 le-tree 的懶加載的功能,正好今天有時(shí)間來(lái)補(bǔ)充一下,讓我們嗨起來(lái) ??

html 部分

下面 el-tree 標(biāo)簽中屬性的介紹這里只介紹 props 、lazy、load屬性 ,其他屬性在上面鏈接的文章中有說(shuō)明
1、props:綁定你定義的 props 變量,這里的 props變量是個(gè)對(duì)象,里面有幾個(gè)鍵值:label、children、disabled(只有tree中帶有checkbox才需要)、isLeaf(分別代表什么意思,下面的 data 部分有說(shuō)明)。
2、lazy:為是否啟用來(lái)加載的樹型結(jié)構(gòu),true為啟用,false為禁用,默認(rèn)true。
3、load:lazy為true的時(shí)候生效,用來(lái)懶加載加載節(jié)點(diǎn)的方法,請(qǐng)求樹的數(shù)據(jù)和邏輯處理都在這個(gè)方法匯總進(jìn)行,下面的事件方法部分會(huì)有說(shuō)明。

 <el-tree
   :props="props"
   :load="loadNode"
   lazy
   node-key="id"
   :expand-no-click-node="true"
   ref="tree"
   show-checkbox
   :check-strictly="true"
   @check="checkClick"
 >
 </el-tree>

data 部分

注:
1、下面的提到的屬性在后面事件方法部都會(huì)講到具體怎樣使用。
2、這里沒有定義 disabled 屬性,因?yàn)檫@里沒有必要定義,事件中直接使用就行,后面事件方法部會(huì)有說(shuō)明。

<script>
export default {
  data(){
    return{
      props:{
        // 用來(lái)控制展示節(jié)點(diǎn)內(nèi)容的字段,根據(jù)后端數(shù)據(jù)自定義 label 值是什么
        label:'text',
        // 子級(jí)存放的位置,拼接數(shù)據(jù)時(shí)候把子級(jí)放到這里定義 childNodes 的字段中,字段名稱自定義 注意:childNodes 是個(gè)數(shù)組。
        children:'childNodes',
        // 用來(lái)判斷是否為最后一級(jí)子節(jié)點(diǎn)
        isLeaf:'lastNode',
        // 這里的 id 是用來(lái)關(guān)聯(lián)上面 node-key="id" 因?yàn)檫@里的樹使用 checkbox 所以用到了 node-key="id" 和 id, 正常的只是單純加載tree的話是不需要的,看需求嘍
        id:"id"
      }
    }
  }
};
</script>

方法事件部分

methods: {
    // 功能講解
    /* 默認(rèn)參數(shù):
       node:每加載一次當(dāng)前加載的節(jié)點(diǎn)的所有數(shù)據(jù)
       reslove:渲染數(shù)據(jù)的方法,把得到該節(jié)點(diǎn)的所有數(shù)據(jù)(數(shù)組)放到該方法中 return 出去,節(jié)點(diǎn)就能渲染出來(lái)了。
     */
    async loadNode(node, reslove) {
      /* 
        判斷是跟節(jié)點(diǎn)還是子節(jié)點(diǎn),
        當(dāng) node.level == 0 的時(shí)候就是 tree 的跟節(jié)點(diǎn)(第一層;
        當(dāng)  node.level 不等于 0 的情況就是每一層的節(jié)點(diǎn)了,沒必要一直判斷 level 是多少級(jí)別,除非有特殊需求
       */
      if (node.level == 0) {
        // 這里是請(qǐng)求接口,根據(jù)自己的項(xiàng)目請(qǐng)求接口就可以。
        let { resultValue } = await getTree(`pwi81cf45a5d07801452018091900001,${this.$store.state.userInfo.cityId}/`)
        // 這里就提到了 props 對(duì)象中的 disabled 屬性的用法,通過找到請(qǐng)求接口找到滿足條件的數(shù)據(jù),然后這個(gè)給滿足條件的數(shù)據(jù)添加一個(gè) disabled = true 就可以實(shí)現(xiàn) checkbox 禁用的效果了。
        resultValue.nodes[0].exValue == "" || resultValue.nodes[0].exValue == 'DYDJ' ? resultValue.nodes[0].disabled = true : null
        // 將請(qǐng)求回來(lái)的數(shù)據(jù)放到 reslove() 方法中渲染出來(lái)。
        return reslove(resultValue.nodes)
      } else {
        let endType = node.data.itemType.split('#')[1];
        let { resultValue } = await getTree(`pwi81cf45a5d07801452018091900001,${node.data.id}/${endType}`)
        /* 
          這里就是 props 對(duì)象中 isLeaf 屬性的用法,上看我們定義的 isLeaf=‘lastNode',
          通過循環(huán)請(qǐng)求回來(lái)的數(shù)據(jù),hasChildren 值為 flase (這個(gè)值一般都是后端設(shè)置數(shù)據(jù)的時(shí)候有帶,至于是什么字段就得看后端是怎么定義的),用來(lái)判斷是否還有下一級(jí)別, 
          當(dāng) hasChildren 值為 flase 的時(shí)候就可以給 請(qǐng)求回來(lái)的數(shù)據(jù)中添加一個(gè) lastNode = true 的鍵值了,這就在你點(diǎn)開改節(jié)點(diǎn)的父級(jí)的時(shí)候就不回有提示 ,還能展開的三角箭頭了。
        */
        resultValue.nodes.forEach(item => {
          item.hasChildren == false ? item.lastNode = ture : null
        });
        /* 
          這里是 props 對(duì)象中 children: 'childNodes' 屬性的用法,因?yàn)槭菓屑虞d每次求情回來(lái)的數(shù)據(jù)都是沒有和上面的數(shù)據(jù)有關(guān)聯(lián)的一個(gè)數(shù)組,這時(shí)用想通過 el-tree 來(lái)實(shí)現(xiàn)樹型結(jié)構(gòu)的話我們就要拼裝數(shù)據(jù),將去回來(lái)的數(shù)據(jù)拼裝到 當(dāng)前節(jié)點(diǎn)的 childNodes 鍵值當(dāng)中即可
        */
        node.data.childNodes = resultValue.nodes
        resultValue.nodes[0].exValue == "" || resultValue.nodes[0].exValue == 'DYDJ' ? resultValue.nodes[0].disabled = true : null
        return reslove(resultValue.nodes)
      }
    },
    // 套用模版
    async loadNode(node, reslove) {
      if (node.level == 0) {
        let res = await getData()
        return reslove(res.data)
      } else {
        let res = await getData()
        // 判斷是否為最底層節(jié)點(diǎn) 根據(jù)自己接口返回的數(shù)據(jù)判斷即可
        res.data.forEach(item => {
          item.hasChildren == false ? item.lastNode = ture : null
        });
        return reslove(res.data)
      }
    },
  },

到此這篇關(guān)于vue2+elementUI的el-tree的懶加載的文章就介紹到這了,更多相關(guān)vue2 elementUI懶加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論