Home > Web Front-end > JS Tutorial > Detailed explanation of the use of vue file tree component

Detailed explanation of the use of vue file tree component

亚连
Release: 2018-05-28 11:27:59
Original
2400 people have browsed it

This article mainly introduces the use of vue file tree component in detail. It has certain reference value. Interested friends can refer to it.

The example of this article shares the vue file tree with everyone. The implementation method of the component is for your reference. The specific content is as follows

This article mainly analyzes the file tree component in the vue official warehouse [vue github]

The first is the html template:

<li>
  <p
   //文件夹加粗表示
   :class="{bold: isFolder}" 
   //处理单击事件 打开闭合文件列表            
   @click="toggle"  
   //处理双击事件 双击子文件,子文件属性变为文件夹               
   @dblclick="changeType">  
   //显示文件名      
   {{model.name}}
  //若是文件夹的话则显示[+]来控制文件夹的开关闭合
   <span v-if="isFolder">[{{open ? &#39;-&#39; : &#39;+&#39;}}]</span>
  </p>
  <ul v-show="open" v-if="isFolder">
  //利用v-for显示子文件列表,通过递归使用item组件来完成文件树
   <item
   class="item"
   v-for="model in model.children"
   :model="model">
   </item>
   //增加一个+标记,单击可以增加子文件
   <li class="add" @click="addChild">+</li>
  </ul>
</li>
Copy after login

The next is the source code of the component part:

Vue.component(&#39;item&#39;, {
 template: &#39;#item-template&#39;,
 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, &#39;children&#39;, [])
  this.addChild()
  this.open = true
  }
 }, //双击触发,通过给文件增加子节点来使文件属性变成文件夹
 addChild: function () {
  this.model.children.push({
  name: &#39;new stuff&#39;
  })  //点击文件夹里的+节点触发 为文件夹添加一个新文件
 }   
 }
})
Copy after login

So the design idea is to pass judgment Whether the object has child nodes determines whether it is a folder or a file, and then the component is recursively reused to show the effect of the file tree.

The last is the data format of the incoming component:

var data = {
 name: &#39;My Tree&#39;,
 children: [
 { name: &#39;hello&#39; },
 { name: &#39;wat&#39; },
 {
  name: &#39;child folder&#39;,
  children: [
  {
   name: &#39;child folder&#39;,
   children: [
   { name: &#39;hello&#39; },
   { name: &#39;wat&#39; }
   ]
  },
  { name: &#39;hello&#39; },
  { name: &#39;wat&#39; },
  {
   name: &#39;child folder&#39;,
   children: [
   { name: &#39;hello&#39; },
   { name: &#39;wat&#39; }
   ]
  }
  ]
 }
 ]
}
Copy after login

The above is what I compiled for everyone, I hope it will be helpful to everyone in the future helpful.

Related articles:

Native nodejs uses websocket code sharing

Detailed explanation of nodejs rendering page resources through response writeback

vue drop-down list function example code

The above is the detailed content of Detailed explanation of the use of vue file tree component. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template