• uni app 树状结构数据展示


    树状数据展示,可以点击item 将点击数据给父组件  ,满足自己需求。不喜勿喷,很简单可以根据自己需求改哈,不要问,点赞收藏就好。其实可以和上一篇文章uni app 自定义 带popup弹窗的input组件-CSDN博客结合使用,做成输入框带popup展示树状结构数据

    1. <template>
    2. <view>
    3. <view v-for="(node, index) in treeData" :key="node.id">
    4. <view v-if="!node.ishow">
    5. <view v-if="uselast">
    6. <view v-if="node.children.length > 0">
    7. <view :style="'margin-left:'+(zjflag*30+32)+'rpx;'" class="item_name_ef"
    8. @tap.stop.prevent="item_click" :data-set="node" :id="index">
    9. <view>
    10. <text v-if="node.children"
    11. :style="'color:'+colors[zjflag]+';margin-right:10rpx'">{{title[zjflag]}}</text>
    12. {{ node.name }}
    13. </view>
    14. <text style="margin-left: 32rpx;color:#f00" v-if="node.children&&node.children.length > 0"
    15. @tap.stop.prevent="open_item(index)">{{image[node.isopen?0:1]}}</text>
    16. </view>
    17. </view>
    18. <view v-else>
    19. <view :style="'margin-left:'+(zjflag*30+32)+'rpx;'" class="item_name"
    20. @tap.stop.preventclick="item_click" :data-set="node" :id="index">
    21. <view>
    22. <text v-if="node.children"
    23. :style="'color:'+colors[zjflag]+';margin-right:10rpx'">{{title[zjflag]}}</text>{{ node.name}}
    24. </view>
    25. <text style="margin-left: 32rpx;color:#f00" v-if="node.children&&node.children.length > 0"
    26. @tap.stop.prevent="open_item(index)">{{image[node.isopen?0:1]}}</text>
    27. </view>
    28. </view>
    29. </view>
    30. <view v-else>
    31. <view :style="'margin-left:'+(zjflag*30+32)+'rpx;'" class="item_name" @tap.stop.prevent="item_click"
    32. :data-set="node" :id="index">
    33. <view>
    34. <text v-if="node.children"
    35. :style="'color:'+colors[zjflag]+';margin-right:10rpx'">{{title[zjflag]}}</text>{{ node.name }}
    36. </view>
    37. <text style="margin-left: 32rpx;color:#f00" v-if="node.children&&node.children.length > 0"
    38. @tap.stop.prevent="open_item(index)">{{image[node.isopen?0:1]}}</text>
    39. </view>
    40. </view>
    41. </view>
    42. <view v-if="node.isopen">
    43. <TreeData v-if="node.children && node.children.length > 0" :tree-data="node.children"
    44. @callBack="mycallback" :uselast="uselast" :zjflag="zjflag+1" />
    45. </view>
    46. </view>
    47. </view>
    48. </template>
    49. <script>
    50. import TreeData from './TreeData.vue';
    51. export default {
    52. components: {
    53. TreeData
    54. },
    55. name: 'TreeData',
    56. props: {
    57. treeData: {
    58. type: Array,
    59. required: true,
    60. },
    61. uselast: {
    62. type: Boolean,
    63. default: false
    64. },
    65. callback: {
    66. type: String,
    67. default: ''
    68. },
    69. showkey: {
    70. type: String,
    71. default: ''
    72. },
    73. zjflag: {
    74. type: Number,
    75. default: 0
    76. }
    77. },
    78. data() {
    79. return {
    80. title: ["①", "②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩"],
    81. // image: ["🔺", "🔻"]
    82. image: ["关", "开"],
    83. colors: ['#ff0', '#f00', '#00f', '#0ff', '#f0f', '#0f0'],
    84. }
    85. },
    86. methods: {
    87. item_click: function(e) {
    88. // console.log("item_click", JSON.stringify(item.currentTarget.dataset.set))
    89. let id = e.currentTarget.id
    90. let item = e.currentTarget.dataset.set
    91. if (this.uselast) {
    92. if (item.children.length == 0) {
    93. this.$emit('callBack', {
    94. mydetail: item
    95. });
    96. } else {
    97. this.treeData[id].isopen = !this.treeData[id].isopen
    98. }
    99. } else {
    100. this.$emit('callBack', {
    101. mydetail: item
    102. });
    103. }
    104. // if (item.isopen) {
    105. // this.treeData[id].isopen = false
    106. // } else {
    107. // this.treeData[id].isopen = true
    108. // }
    109. this.$forceUpdate()
    110. },
    111. open_item: function(id) {
    112. this.treeData[id].isopen = !this.treeData[id].isopen
    113. this.$forceUpdate()
    114. },
    115. mycallback: function(item) {
    116. this.$emit('callBack', {
    117. mydetail: item
    118. });
    119. }
    120. },
    121. };
    122. </script>
    123. <style lang="scss" scoped>
    124. @function tovmin($rpx) {
    125. //$rpx为需要转换的字号
    126. @return #{$rpx * 100 / 750}vmin;
    127. }
    128. .item_name {
    129. display: flex;
    130. justify-content: space-between;
    131. font-size: 28;
    132. padding-top: 5rpx;
    133. padding-bottom: 5rpx;
    134. flex: 1;
    135. min-height: tovmin(80);
    136. align-items: center;
    137. text-align: left;
    138. margin-right: tovmin(64);
    139. color: #333;
    140. font-weight: 600;
    141. overflow-y: auto;
    142. overflow-x: scroll;
    143. border-bottom: solid #efefef 2rpx;
    144. white-space: normal;
    145. word-break: break-all;
    146. word-wrap: break-word;
    147. }
    148. .item_name_ef {
    149. font-size: 28;
    150. padding-top: 5rpx;
    151. padding-bottom: 5rpx;
    152. flex: 1;
    153. min-height: tovmin(80);
    154. display: flex;
    155. justify-content: space-between;
    156. align-items: center;
    157. text-align: left;
    158. margin-right: tovmin(64);
    159. color: #999;
    160. font-weight: 600;
    161. overflow-y: auto;
    162. overflow-x: scroll;
    163. border-bottom: solid #efefef 2rpx;
    164. white-space: normal;
    165. word-break: break-all;
    166. word-wrap: break-word;
    167. }
    168. </style>

    讲一下 

            treeData 树状数据

            uselast  只能使用末级

            showkey  列表要展示的字段

            zjflag        树状结构第几层默认0开始

    使用  在父组件

    引用组件

    import TreeNode from './TreeNode.vue';

    components: {
                
                TreeNode
            },

        

    treeData: [{
                            id: 1,
                            name: '节点1',
                            ishow: false,
                            isopen: true,
                            children: [{
                                    id: 2,
                                    name: '子1-节点1',
                                    ishow: false,
                                    isopen: true,
                                    children: []
                                },
                                {
                                    id: 3,
                                    name: '子2-节点1',
                                    ishow: false,
                                    isopen: true,
                                    children: [{
                                        id: 4,
                                        name: '孙1-子2-节点1',
                                        ishow: false,
                                        isopen: true,
                                        children: []
                                    }]
                                },
                            ],
                        },
                        {
                            id: 5,
                            name: '节点2',
                            ishow: false,
                            isopen: true,
                            children: [{
                                    id: 6,
                                    name: '子1-节点2',
                                    ishow: false,
                                    isopen: true,
                                    children: [{
                                        id: 8,
                                        name: '孙1-子1-节点2',
                                        ishow: false,
                                        isopen: true,
                                        children: [{
                                            id: 8,
                                            name: '重1-孙1-子1-节点2',
                                            ishow: false,
                                            isopen: true,
                                            children: [{
                                                id: 9,
                                                name: '曾1-重1-孙1-子1-节点2',
                                                ishow: false,
                                                isopen: true,
                                                children: [{
                                                    id: 9,
                                                    name: '僧1-曾1-重1-孙1-子1-节点2',
                                                    ishow: false,
                                                    isopen: true,
                                                    children: []
                                                }]
                                            }]
                                        }]
                                    }]
                                },
                                {
                                    id: 7,
                                    name: '子2-节点2',
                                    ishow: false,
                                    isopen: true,
                                    children: [{
                                        id: 8,
                                        name: '孙1-子2-节点2',
                                        ishow: false,
                                        isopen: true,
                                        children: [{
                                            id: 8,
                                            name: '重1-孙1-子2-节点2',
                                            ishow: false,
                                            isopen: true,
                                            children: [{
                                                id: 9,
                                                name: '曾1-重1-孙1-子2-节点2',
                                                ishow: false,
                                                isopen: true,
                                                children: [{
                                                    id: 9,
                                                    name: '僧1-曾1-重1-孙1-子2-节点2',
                                                    ishow: false,
                                                    isopen: true,
                                                    children: []
                                                }]
                                            }]
                                        }]
                                    }]
                                },
                            ],
                        },

                    ],

        tree_node_back: function(e) {
                    if (e.mydetail) {
                        this.tree_node_back(e.mydetail)

                    } else {
                        console.log("tree_node_back=222==", JSON.stringify(e))
                    }

                },

    注意  treedata 中  isshow 是翻着的 false 展示  true 隐藏  isopen 是正常  false 关闭  true 展开

  • 相关阅读:
    借助这个宝藏神器,我成为全栈了
    java.swing 飞机大战小游戏
    DockerFile微服务实战
    MySQL系列-高级-深入理解Mysql事务隔离级别与锁机制01
    JVM的运行时数据区
    conda常用指令
    MongoDB快速上手
    数据结构四:线性表之带头结点的单向循环循环链表的设计
    利星行汽车携手南凌科技 拥抱网络数字化变革
    记录 | docker权限原因导致service ssh start失败
  • 原文地址:https://blog.csdn.net/yuehua_zhang/article/details/139796390