小程序中的树形控件

参考:https://segmentfault.com/a/1190000021286232
工作了一段时间,很久没写关于代码上的学习记录,这是在公司处理一个树形控件时寻找解决方法时看到的一篇文章,很有效地帮助到我,非常感谢!!

因为公司的原因,对代码进行了一些修改,但总体的操作理念比较符合我的需求。

以下的代码都是关于微信小程序作为开发

首先先新建一个放自定义组件的文件夹(components),组件的名称根据自己的开发来定义,我这边就称它为(tree)组件,下面为tree组件的全部代码
(技巧:新建组件文件一般都是右键点击文件夹-然后选择 '新建Component'-这样就能快速新建一个组件文件)也不算啥技巧,估计大家都知道#125

tree.wxml

\triangleright 有些wx:if的判断可以暂时没用,因为公司代码用到,所以我就直接复制上去了(我比较懒),还有里面用到的图片(tick.png),只是一张打勾的图片,随便上网找下就是了

<view>
    <block wx:for="{{tree}}" wx:key="index">
        <view style="display:flex;align-items:center;margin-left:{{item.nodes.length !== 0 ? depth*20 : depth*30}}px;font-size:36rpx;margin-top: 5px;"
            wx:if="{{item.category !== 'index'}}">
            <view class="tree_left" wx:if="{{item.nodes.length !== 0}}" data-id="{{item.id}}" data-open="{{item.open}}"
                bindtap="onchange">
                <view class="{{!item.open ? 'icon-right' : 'icon-down'}}"></view>
            </view>

            <view class="{{item.nodes.length !== 0 ?'parent':'node'}} " data-id="{{item.id}}" data-open="{{item.open}}"
                bindtap="onchange">
                {{item.name}}
                <view class="selected" wx:if="{{item.category !== 'index'}}" catchtap="handleClick"
                    data-id="{{item.id}}" data-value="{{item.name}}">
                    <image hidden="{{item.selected === false}}" src="../../pages/image/tick.png"
                        style="width:20px;height:20px;"></image>
                </view>
            </view>
        </view>
        <block wx:if="{{item.nodes}}">
            <view hidden="{{!item.open}}">
                <tree treeList="{{item.nodes}}" id="treeSelect" bind:onclick="click" bind:change="change"
                    depth="{{depth+1}}"></tree>
            </view>
        </block>
    </block>
</view>

tree.wxss

.tree_left{
    position: relative;
    float: left;
}

.icon-down{
    border: 8px solid;
    border-color: transparent transparent #B8B8B8;
    transform: rotate(180deg);
    margin-top: 7px;
    margin-left: 5px;
    margin-right: 10px;
}

.icon-right{
    border: 8px solid;
    border-color: transparent transparent #B8B8B8;
    transform: rotate(90deg);
    margin-left: 5px;
}

.selected{
    height: 20px;
    width: 20px;
    border-radius: 50%;
    border: 1px solid #D6D6D6;
    float: right;
    margin-left: 5px;
    margin-top: 2px;
}

tree.js

const tranverse = (e) => {
    for (let i in e) {
        if (e[i].nodes) {
            e[i].open = false;
            tranverse(e[i].nodes)
        }
        e[i].selected = false;
    }
}
Component({
    properties: {
        treeList: Object,
        depth: {
            type: Number,
            value: 0
        }
    },
    data: {},
    ready() {
        // const {treeList}=this.properties; 
        const { treeList } = this.data;
        for (let i in treeList) {
            if (treeList[i].nodes) {
                treeList[i].open = false;
                tranverse(treeList[i].nodes);
            }
            treeList[i].selected = false;
        }
        this.setData({
            tree: treeList
        })
    },
    methods: {
        //修改折叠状态
        changeOpen(tree, id) {
            for (let i = 0; i < tree.length; i += 1) {
                if (tree[i].id === id) {
                    tree[i].open = !tree[i].open;
                }
                if (tree[i].nodes.length !== 0) {
                    this.changeOpen(tree[i].nodes, id);
                }
            }
            return;
        },
        onchange(e) {
            const { treeList } = this.data;
            const { id } = e.currentTarget.dataset;
            this.changeOpen(treeList, id);
            this.triggerEvent('change', id, { bubbles: true, composed: true });
            this.setData({
                tree: treeList
            })
        },
        change(e) {
            const id = e.detail;
            const { treeList } = this.data;
            this.changeOpen(treeList, id);
            this.setData({
                tree: treeList
            })
        },
        click(e) {
            const t = this.selectAllComponents('#treeSelect');
            t.forEach(item => {
                item.click(e);
            })
            let id = '';
            if (e.detail) {
                id = e.detail.id;
            }
            const { treeList } = this.data;
            this.setStatus(treeList, id);
            this.setData({
                tree: treeList
            })
        },
        handleClick(e) {
            const t = this.selectAllComponents('#treeSelect');
            t.forEach(item => {
                item.click(e);
            })
            //   const {id}=e.currentTarget.dataset;
            const { id, value } = e.currentTarget.dataset;
            const { treeList } = this.data;
            // const value = this.getData(treeList, id)
            this.setStatus(treeList, id);
            this.triggerEvent('onclick', { id, value, treeList }, { composed: true, bubbles: true });
            this.setData({
                tree: treeList
            })
        },
        //切换选中状态
        setStatus(tree, id) {
            for (let i = 0; i < tree.length; i += 1) {
                if (tree[i].id == id) {
                    tree[i].selected = true;
                } else {
                    tree[i].selected = false;
                }
                if (tree[i].nodes) {
                    this.setStatus(tree[i].nodes, id);
                }
            }
            return;
        },
        //获取选中项信息
        getData(tree, id) {
            for (let i = 0; i < tree.length; i += 1) {
                if (tree[i].id === id) {
                    return tree[i].name;
                }
                if (tree[i].nodes) {
                    this.getData(tree[i].nodes, id);
                }
            }
            return '';
        },
    }
})

tree.json

{
    "component": true,
    "usingComponents": {
        "tree": "/components/tree/tree"
    }
}

当你写完这个tree组件的时候,是时候验证它的展示效果,
注意:这里的数据是写死的,请根据自己开发需求进行代码上的修改

随便新建一个Page页面,为demo页面

demo.wxml

\triangleright 这个treeList传的内容为一个数组对象,如:[{..},{...}],这样子的数据

<tree treeList="{{treeList}}" id="treeSelect" bind:onclick="treeClick"></tree>

demo.wxss

/* 这里没写样式,可以自己设计 */

demo.json

\triangleright 这里路径一般根据自己的项目文件来引用,并不一定要按我的写法

{
   "usingComponents": {
    "tree": "../../components/tree/tree"
  }
}

demo.js

Page({
     data: {
        treeList: [
            {
                id: '1',
                pid: '0',
                name: '祖先节点',
                nodes:[
                    {
                        id: '1-1',
                        pid: '1',
                        name: '父节点1',
                        nodes:[
                            {
                                id: '1-1-1',
                                pid: '1-1',
                                name: '子节点1',
                                nodes: []
                            }
                        ]
                    },
                    {
                        id: '1-2',
                        pid: '1',
                        name: '父节点2',
                        nodes:[
                            {
                                id: '1-1-2',
                                pid: '1-1',
                                name: '子节点2',
                                nodes: []
                            }
                        ]
                    }
                ]
            }
        ]
    },

    treeClick(e) {
        console.log(e)
        const t = this.selectAllComponents('#treeSelect');
        t.forEach(item => {
            item.click(e);
        })
        const { id, value } = e.detail;
        console.log(e.detail)
    }
})

展示效果如下:


树形控件.gif

总结:刚开始做公司的这个需求的时候,感觉难度应该不大,可惜我是程序员中的菜鸡,需要找资料才能解决掉,不懂就百度,这是个日常的操作。不说那么多废话,这个树形控件的使用请留意原文章作者的描述(描述的非常详细),因为之前用的树形控件都是不能够进行单选的操作,所以寻找了很多处理方法都搞不掂,最后找到了这边文章,并在原代码进行一定的修改,发现可以满足我的需求,这是非常不错的!!这里再次感谢写这篇文章的作者,因为真的帮到我了,哈哈哈,请原谅我是菜鸡。

当然这个树型控件还是存在一些缺陷,假如节点越来越多又或者节点上的文字宽度占的比例较大的话,会显得比较丑陋,还是需要多加改进。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,753评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,668评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,090评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,010评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,054评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,806评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,484评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,380评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,873评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,021评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,158评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,838评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,499评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,044评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,159评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,449评论 3 374
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,136评论 2 356

推荐阅读更多精彩内容