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

el-tree樹設(shè)置懶加載以及設(shè)置默認(rèn)勾選方式

 更新時(shí)間:2023年04月24日 08:41:13   作者:騎上我心愛(ài)的小摩托  
這篇文章主要介紹了el-tree樹設(shè)置懶加載以及設(shè)置默認(rèn)勾選方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

el-tree樹設(shè)置懶加載以及設(shè)置默認(rèn)勾選

當(dāng)需要對(duì)element-ui組件懶加載進(jìn)行拓展功能,如添加查看更多或者其他圖標(biāo)進(jìn)行加載,可使用下面的方法進(jìn)行調(diào)整使用

1.加載tree樹時(shí),要求能夠通過(guò)點(diǎn)擊查看更多進(jìn)行懶加載,且可進(jìn)行勾選復(fù)選框獲取數(shù)據(jù),由于界面存在多個(gè)Tree樹,故命名多個(gè)ref值傳遞進(jìn)來(lái)子組件Tree樹內(nèi)

 <DsmasTreeLoadMore
 ref="dataStructure"
   :show-checkbox="true"
   :bind-ref="{label: '結(jié)構(gòu)化數(shù)據(jù)',id: 'dataStructure'}"
 />
 <script>
	import DsmasTreeLoadMore from '@/views/components/dsmas-tree-loadmore';
	export default {
		components: {
			DsmasTreeLoadMore 
		}
		data(){
			return{
				defaultProps: {
			       label: 'label',
			        children: 'children',
			        isLeaf: 'leaf'
			      },
			    pageNumber: 1,
			    pageSize: 10,
			    complete: false,
			    defaultCheckedKeys: [],
				treeData: {
				    dataStructure: [
				      {
				        label: '結(jié)構(gòu)化數(shù)據(jù)',
				        id: 'dataStructure',
				        children: [],
				        disabled: true
				      }
				    ],
				      dataUnstructure: [
				      {
				        label: '非結(jié)構(gòu)化數(shù)據(jù)',
				        id: 'dataUnstructure',
				        children: [],
				        disabled: true
				      }
				    ],
				    collapse:false
				}
			},
	  created() {
	    this.getTreeLoadmore();
	  },
	  methods: {
	    // 查看更多按鈕
	    loadmore() {
	      ++this.pageNumber;
	      this.getTreeLoadmore();
	    },
	    async getTreeLoadmore() {
	      let ref = this.bindRef.id; let resultData = ''; let treeData = [];
	      if (ref === 'dataStructure') {
	        resultData = await getDistributeDatasource({pageNumber: this.pageNumber, pageSize: this.pageSize, structured: true});
	        treeData = resultData.data.items.map(({id, name: label}) => {
	          return {id, label};
	        });
	      } else if (ref === 'dataUnstructure') {
	        resultData = await getDistributeDatasource({pageNumber: this.pageNumber, pageSize: this.pageSize, structured: false});
	        treeData = resultData.data.items.map(({id, name: label}) => {
	          return {id, label};
	        });
	      }
	      this.treeData[ref][0].children = this.treeData[ref][0].children.concat(treeData);
	
	      // 初次加載選定前十存儲(chǔ)與查詢數(shù)據(jù)
	      let storage = this.getStorageMethod(this.bindRef.id); let setChecked = [];
	      if (storage) {
	        setChecked = storage;
	      } else {
	        setChecked = treeData.map(({id}) => {
	          return id;
	        });
	      }
	      this.$nextTick(() => {
	        this.$refs[ref].setCheckedKeys(setChecked);
	      });
	      this.defaultCheckedKeys = setChecked;
	      // 數(shù)據(jù)加載完畢
	      if (this.treeData[ref][0].children.length >= resultData.data.total) {
	        this.complete = true;
	      }
	    },
	    // 取出當(dāng)前ref對(duì)應(yīng)緩存
	    getStorageMethod(ref) {
	      let paramObj = getStorage('distribute');
	      if (paramObj) {
	        if (ref === 'dataStructure' && paramObj.dataStructure) {
	          return paramObj.dataStructure;
	        } else if (ref === 'dataUnstructure' && paramObj.dataUnstructure) {
	          return paramObj.dataUnstructure;
	        }
	      }
	    },
	    // 勾選接口聚合
	    handleCheck(data, checked) {
	      let checkedNode = this.$refs[this.bindRef.id].getNode(data.id).checked;
	      let storage = this.getStorageMethod(this.bindRef.id);
	      if (storage) {
	        if (checkedNode) {
	          storage.push(data.id);
	        } else {
	          storage.splice(storage.findIndex(item => item === data.id), 1);
	        }
	      } else {
	        storage = checked.checkedKeys;
	      }
	      this.defaultCheckedKeys = storage;
	      this.$refs[this.bindRef.id].setCheckedKeys(storage);
	      this.$forceUpdate();
	      let storageFormal = getStorage('distribute');
	      storageFormal[this.bindRef.id] = storage;
	      setStorage('distribute', storageFormal);
	    },
	    // 節(jié)點(diǎn)展開(kāi)
	    handleNodeExpand() {
	      this.collapse = true;
	    },
	    // 節(jié)點(diǎn)關(guān)閉
	    handleNodeCollapse() {
	      this.collapse = false;
	      // 剔除當(dāng)前節(jié)點(diǎn)所有子節(jié)點(diǎn) 過(guò)濾節(jié)點(diǎn)是關(guān)閉當(dāng)前節(jié)點(diǎn)且是其子級(jí),則不予賦值
	      // this.defaultExpandKeys = this.defaultExpandKeys.filter((x) => (![data.id].some((item) => x === item) && !(x.indexOf(data.id) > -1)));
	    },
	    handleNodeClick(node) {
	      this.$emit('handleNodeClick', {id: node.id, label: node.label});
	    }
	}
</script>

2.當(dāng)前Tree樹,默認(rèn)是兩層結(jié)構(gòu),所以不需要用到loadmore原生方法,這里直接拼接數(shù)據(jù),查詢接口數(shù)據(jù)為第二層數(shù)據(jù)傳入,當(dāng)前,當(dāng)翻入到第二頁(yè)時(shí),默認(rèn)第二頁(yè)未勾選,當(dāng)用戶進(jìn)行勾選時(shí)不影響翻頁(yè)效果

 <div class="tree-load">
    <el-tree
      :ref="bindRef.id"
      class="treeDom"
      :data="treeData[bindRef.id]"
      default-expand-all
      show-checkbox
      node-key="id"
      :expand-on-click-node="false"
      :default-checked-keys="defaultCheckedKeys"
      :props="defaultProps"
      @node-expand="handleNodeExpand"
      @node-collapse="handleNodeCollapse"
      @check="handleCheck"
    >
      <div slot-scope="{ node,data }" class="custom-tree-node">
        <el-tooltip
          class="item"
          effect="light"
          placement="right-start"
        >
          <div slot="content" style="max-width: 300px;">
            {{ node.label }}
          </div>
          <span v-if="data.id !=='loadmore'" class="span-tree-node">{{ node.label }}</span>
        </el-tooltip>
      </div>
    </el-tree>

    <el-link
      v-if="!complete && collapse"
      :underline="false"
      class="tree-more"
      @click="loadmore"
    >
      查看更多
    </el-link>
  </div>

vue el-tree樹的懶加載實(shí)現(xiàn)

樣式1:

首先加載第一層樹節(jié)點(diǎn)(要有加載第一層節(jié)點(diǎn)的接口才ok)

樣式2:

當(dāng)點(diǎn)擊第一層節(jié)點(diǎn),或者其他層父節(jié)點(diǎn)的時(shí)候,加載其子節(jié)點(diǎn),形成一個(gè)懶加載的效果。

代碼實(shí)現(xiàn):

重要的是在tree中設(shè)置

:load=“loadNode”

lazy

 <el-tree
              :data="treeData"
              node-key="id"
              :filter-node-method="filterNode"
              ref="indexTree"
              :props="treeDataDefaultProp"
              :expand-on-click-node="false"
              class="tree_Style"
              :load="loadNode"
              lazy
            >
            </el-tree>
created() {
    this.init();
  },
 methods: {
    // 初始化第一層樹
    init() {
      this.getTreeData();
    },
    // 得到第一層樹的列表
    async getTreeData() {
      const param = {
        type: Number(this.cateTabActive),
        keyword: this.keyword
        };
      const res = await this.$api.get('/api/category', param);
      if (res.code == 200) {
      // treeData 就是樹綁定的數(shù)據(jù)
        this.treeData = res.info;
      } else {
        return false;
      }
    },
     // 樹的懶加載
    loadNode(node, reslove) {
      let that = this;
      if (node.level === 0) {
        reslove(that.treeData);
      }
      if (node.level >= 1) {
        this.loadNodeChildren(node, reslove);
      }
    },
    // 樹的懶加載獲取子節(jié)點(diǎn)
    async loadNodeChildren(node, reslove) {
      let param = {
        categoryId: node.data.id,
        type: Number(this.cateTabActive)
      };
      const res = await this.$api.get('/api/category', param);
      let resArr = [];
      if (res.code === 200) {
        res.info.forEach(item => {
          item = JSON.parse(JSON.stringify(item));
          resArr.push({
            name: item.name,
            id: item.id,
            leaf: item.leaf,
            parentCategoryId: item.parentCategoryId,
            currentLevel: item.currentLevel,
            relateCount: item.relateCount
          });
        });
        // 將得到的孩子列表,塞進(jìn)樹中
        this.$refs.indexTree.updateKeyChildren(node.data.id, resArr);
        return reslove(res.info);
      }
    },

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue-cli Eslint在vscode里代碼自動(dòng)格式化的方法

    Vue-cli Eslint在vscode里代碼自動(dòng)格式化的方法

    本篇文章主要介紹了Vue-cli Eslint在vscode里代碼自動(dòng)格式化的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • vue瀏覽器返回監(jiān)聽(tīng)的具體步驟

    vue瀏覽器返回監(jiān)聽(tīng)的具體步驟

    這篇文章主要給大家介紹了關(guān)于vue瀏覽器返回監(jiān)聽(tīng)的具體步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié)

    vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié)

    這篇文章主要介紹了vue改變對(duì)象或數(shù)組時(shí)的刷新機(jī)制的方法總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-04-04
  • vue+element-ui實(shí)現(xiàn)頭部導(dǎo)航欄組件

    vue+element-ui實(shí)現(xiàn)頭部導(dǎo)航欄組件

    這篇文章主要為大家詳細(xì)介紹了vue+element-ui實(shí)現(xiàn)頭部導(dǎo)航欄組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 基于Vue實(shí)現(xiàn)頁(yè)面切換左右滑動(dòng)效果

    基于Vue實(shí)現(xiàn)頁(yè)面切換左右滑動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了基于Vue實(shí)現(xiàn)頁(yè)面切換左右滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Vue中如何引用公共方法和公共組件

    Vue中如何引用公共方法和公共組件

    這篇文章主要介紹了Vue中如何引用公共方法和公共組件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue項(xiàng)目首屏性能優(yōu)化組件實(shí)戰(zhàn)指南

    Vue項(xiàng)目首屏性能優(yōu)化組件實(shí)戰(zhàn)指南

    Vue眾所周知是一個(gè)輕量級(jí)的框架,源碼僅僅為72.9KB,但是也有它自己的缺點(diǎn),就是首屏加載會(huì)比較慢,這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目首屏性能優(yōu)化組件的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • vscode 使用Prettier插件格式化配置使用代碼詳解

    vscode 使用Prettier插件格式化配置使用代碼詳解

    這篇文章主要介紹了vscode 使用Prettier插件格式化配置使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Vue中render方法的使用詳解

    Vue中render方法的使用詳解

    這篇文章主要為大家詳細(xì)介紹了Vue中render方法的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Vue自定義指令寫法與個(gè)人理解

    Vue自定義指令寫法與個(gè)人理解

    VUE指令是什么,VUE自定義指令又是什么,下面就和大家分享一下個(gè)人對(duì)它們的理解
    2019-02-02

最新評(píng)論