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

uni-app無(wú)限級(jí)樹(shù)形組件簡(jiǎn)單實(shí)現(xiàn)代碼

 更新時(shí)間:2025年01月13日 11:02:50   作者:洗發(fā)水很好用  
文章介紹了如何在uni-app中簡(jiǎn)單封裝一個(gè)無(wú)限級(jí)樹(shù)形組件,該組件可以無(wú)線(xiàn)嵌套,展開(kāi)和收縮,并獲取子節(jié)點(diǎn)數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧

       因?yàn)轫?xiàng)目一些數(shù)據(jù)需要樹(shù)形展示,但是官網(wǎng)組件沒(méi)有?,F(xiàn)在簡(jiǎn)單封裝一個(gè)組件在app中使用,可以無(wú)線(xiàn)嵌套,展開(kāi),收縮,獲取子節(jié)點(diǎn)數(shù)據(jù)等。

簡(jiǎn)單效果

組件TreeData

<template>
  <view class="tree">
    <template v-for="(node, index) in treeData">
      <view>
        <span @click="toggleNode($event, node)">
          <uni-icons
            v-if="node.children && node.children.length > 0"
            :type="node.expanded ? 'arrowdown' : 'arrowright'"
            size="14"
          ></uni-icons>
          {{ node.label }}
        </span>
        <span
          @click.stop="deleteNode($event, node)"
          class="action-button delete-button"
          >刪除</span
        >
        <span
          @click.stop="editNode($event, node)"
          class="action-button edit-button"
          >編輯</span
        >
        <view v-if="node.expanded" class="children">
          <Tree
            :treeData="node.children"
            @edit-node="(childNode) => $emit('edit-node', childNode)"
            @delete-node="(childNode) => $emit('delete-node', childNode)"
          />
        </view>
      </view>
    </template>
  </view>
</template>
<script>
export default {
  name: "Tree",
  props: {
    treeData: {
      type: Array,
      default: () => [],
    },
    expandAll: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      init: false,
    };
  },
  watch: {
    treeData: {
      immediate: true,
      handler(newData) {
        if (!this.init) {
          this.initializeTreeData(newData, this.expandAll);
          this.init = true;
        }
      },
    },
  },
  methods: {
    initializeTreeData(nodes, expanded) {
      nodes.forEach((node) => {
        this.$set(node, "expanded", expanded); // 使用 $set 確保響應(yīng)式
        if (node.children && node.children.length > 0) {
          this.initializeTreeData(node.children, expanded); // 遞歸處理子節(jié)點(diǎn)
        }
      });
    },
    toggleNode(event, node) {
      event.stopPropagation(); // 阻止事件冒泡
      node.expanded = !node.expanded; // 切換節(jié)點(diǎn)展開(kāi)狀態(tài)
    },
    editNode(event, node) {
      event.stopPropagation();
      this.$emit("edit-node", node); // 觸發(fā)父組件的 edit-node 事件,并傳遞當(dāng)前節(jié)點(diǎn)
    },
    deleteNode(event, node) {
      event.stopPropagation();
      this.$emit("delete-node", node); // 觸發(fā)父組件的 delete-node 事件,并傳遞當(dāng)前節(jié)點(diǎn)
    },
  },
};
</script>
  <style scoped>
.tree {
  padding-left: 15px;
}
.children {
  padding-left: 15px;
}
.tree-node {
  display: flex;
  align-items: center;
}
.action-button {
  cursor: pointer;
  margin-left: 10px;
  color: #409eff;
}
.edit-button {
  float: right;
}
.delete-button {
  float: right;
}
</style>

在頁(yè)面中使用...

<template>
  <view class="page">
    <Tree
      :treeData="treeData"
      :expandAll="expandAll"
      @edit-node="handleEditNode"
      @delete-node="handleDeleteNode"
    />
  </view>
</template>
<script>
import Tree from "@/components/TreeData";
export default {
  components: {
    Tree,
  },
  data() {
    return {
      treeData: [
        {
          label: "根節(jié)點(diǎn) 1",
          children: [
            {
              label: "子節(jié)點(diǎn) 1-1",
              children: [
                {
                  label: "子節(jié)點(diǎn) 1-1-1",
                  children: [],
                },
                {
                  label: "子節(jié)點(diǎn) 1-1-2",
                  children: [],
                },
              ],
            },
            {
              label: "子節(jié)點(diǎn) 1-2",
              children: [],
            },
          ],
        },
        {
          label: "根節(jié)點(diǎn) 2",
          children: [
            {
              label: "子節(jié)點(diǎn) 2-1",
              children: [],
            },
          ],
        },
      ],
      expandAll: true, // 控制是否全部展開(kāi)
    };
  },
  methods: {
    handleEditNode(node) {
      console.log("編輯節(jié)點(diǎn)", node);
      // 處理編輯節(jié)點(diǎn)的邏輯
    },
    handleDeleteNode(node) {
      console.log("刪除節(jié)點(diǎn)", node);
      // 處理刪除節(jié)點(diǎn)的邏輯
    },
  },
};
</script>
<style scoped>
page {
  background-color: #f5f6f8;
}
.page {
  padding: 20px;
}
</style>

到此這篇關(guān)于uni-app無(wú)限級(jí)樹(shù)形組件簡(jiǎn)單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)uni-app無(wú)限級(jí)樹(shù)形組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue 處理跨域問(wèn)題及解決方法小結(jié)

    vue 處理跨域問(wèn)題及解決方法小結(jié)

    跨域問(wèn)題的出現(xiàn)是因?yàn)闉g覽器的同源策略問(wèn)題,如果沒(méi)有同源策略我們的瀏覽器將會(huì)十分的不安全,隨時(shí)都可能受到攻擊,今天小編通過(guò)本文給大家介紹下vue 處理跨域問(wèn)題,感興趣的朋友一起看看吧
    2021-09-09
  • vueJS簡(jiǎn)單的點(diǎn)擊顯示與隱藏的效果【實(shí)現(xiàn)代碼】

    vueJS簡(jiǎn)單的點(diǎn)擊顯示與隱藏的效果【實(shí)現(xiàn)代碼】

    下面小編就為大家?guī)?lái)一篇vueJS簡(jiǎn)單的點(diǎn)擊顯示與隱藏的效果【實(shí)現(xiàn)代碼】。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • 基于Vue2x實(shí)現(xiàn)響應(yīng)式自適應(yīng)輪播組件插件VueSliderShow功能

    基于Vue2x實(shí)現(xiàn)響應(yīng)式自適應(yīng)輪播組件插件VueSliderShow功能

    本文講述的是從開(kāi)發(fā)一款基于Vue2x的響應(yīng)式自適應(yīng)輪播組件插件的一個(gè)全過(guò)程,包含發(fā)布到npm,構(gòu)建自己的npm包,供下載安裝使用的技巧,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-05-05
  • vue項(xiàng)目打包后網(wǎng)頁(yè)的title亂碼解決方案

    vue項(xiàng)目打包后網(wǎng)頁(yè)的title亂碼解決方案

    這篇文章主要介紹了vue項(xiàng)目打包后網(wǎng)頁(yè)的title亂碼解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 圖文詳解keep-alive如何清除緩存

    圖文詳解keep-alive如何清除緩存

    vue項(xiàng)目中常常會(huì)用到keepalive來(lái)作緩存,在應(yīng)付基本要求上能夠說(shuō)很是方便,可是遇到同一個(gè)頁(yè)面,根據(jù)條件不一樣,分別緩存或者不緩存,就有些麻煩了,這篇文章主要給大家介紹了關(guān)于keep-alive如何清除緩存的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Vue項(xiàng)目打包壓縮的實(shí)現(xiàn)(讓頁(yè)面更快響應(yīng))

    Vue項(xiàng)目打包壓縮的實(shí)現(xiàn)(讓頁(yè)面更快響應(yīng))

    這篇文章主要介紹了Vue項(xiàng)目打包壓縮的實(shí)現(xiàn)(讓頁(yè)面更快響應(yīng)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 詳解如何實(shí)現(xiàn)Element樹(shù)形控件Tree在懶加載模式下的動(dòng)態(tài)更新

    詳解如何實(shí)現(xiàn)Element樹(shù)形控件Tree在懶加載模式下的動(dòng)態(tài)更新

    這篇文章主要介紹了詳解如何實(shí)現(xiàn)Element樹(shù)形控件Tree在懶加載模式下的動(dòng)態(tài)更新,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-04-04
  • 利用Vue3實(shí)現(xiàn)一個(gè)可以用js調(diào)用的組件

    利用Vue3實(shí)現(xiàn)一個(gè)可以用js調(diào)用的組件

    最近遇到個(gè)功能要求,想要在全局中調(diào)用組件,而且要在某些js文件內(nèi)調(diào)用,所以這篇文章主要給大家介紹了關(guān)于如何利用Vue3實(shí)現(xiàn)一個(gè)可以用js調(diào)用的組件的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • VueX瀏覽器刷新如何實(shí)現(xiàn)保存數(shù)據(jù)

    VueX瀏覽器刷新如何實(shí)現(xiàn)保存數(shù)據(jù)

    這篇文章主要介紹了VueX瀏覽器刷新如何實(shí)現(xiàn)保存數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue使用路由守衛(wèi)實(shí)現(xiàn)菜單的權(quán)限設(shè)置

    vue使用路由守衛(wèi)實(shí)現(xiàn)菜單的權(quán)限設(shè)置

    我們使?vue-element-admin前端框架開(kāi)發(fā)后臺(tái)管理系統(tǒng)時(shí),?般都會(huì)涉及到菜單的權(quán)限控制問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于vue使用路由守衛(wèi)實(shí)現(xiàn)菜單的權(quán)限設(shè)置的相關(guān)資料,需要的朋友可以參考下
    2023-06-06

最新評(píng)論