This time I will bring you a case of vue using file tree. What are theprecautionsfor vue using file tree case. The following is a practical case, let's take a look.
This article mainly analyzes the file tree component in the vue official warehouse [vue github]
The demo can be viewedhttps://codepen.io/shayminsky21/pen/xXwxgm
First is the html template:
//显示文件名 {{model.name}} //若是文件夹的话则显示[+]来控制文件夹的开关闭合 [{{open ? '-' : '+'}}]
Next is the source code of the component part:
Vue.component('item', { template: '#item-template', props: { model: Object //将文件数据通过props传入 }, data: function () { return { open: false //open表示文件夹闭合状态 } }, computed: { isFolder: function () { return this.model.children && this.model.children.length } }, //计算对象是否有子节点并且子节点数大于0来判断是否是文件夹 methods: { toggle: function () { if (this.isFolder) { this.open = !this.open } }, //控制文件夹闭合的方法 单击触发改变open changeType: function () { if (!this.isFolder) { Vue.set(this.model, 'children', []) this.addChild() this.open = true } }, //双击触发,通过给文件增加子节点来使文件属性变成文件夹 addChild: function () { this.model.children.push({ name: 'new stuff' }) //点击文件夹里的+节点触发 为文件夹添加一个新文件 } } })
So the design idea is to determine whether the object has child nodes to determine whether it is a folder or file, and then recursively reuse the
The last is the data format of the incoming component:
var data = { name: 'My Tree', children: [ { name: 'hello' }, { name: 'wat' }, { name: 'child folder', children: [ { name: 'child folder', children: [ { name: 'hello' }, { name: 'wat' } ] }, { name: 'hello' }, { name: 'wat' }, { name: 'child folder', children: [ { name: 'hello' }, { name: 'wat' } ] } ] } ] }
You can continue to expand the function and display effect of the file tree based on this basic structure.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of using JS EventEmitter
Operation Vue to export excel table
The above is the detailed content of vue use file tree case. For more information, please follow other related articles on the PHP Chinese website!