Home > Web Front-end > JS Tutorial > body text

vue.js+element-ui implements tree structure

php中世界最好的语言
Release: 2018-04-27 14:31:08
Original
2728 people have browsed it

This time I will bring you vue.js element-ui to implement tree structure, vue.js element-ui to implement tree structure. What are the precautions?. Here are practical cases. Let’s take a look. one time.

Due to business needs, it is required to implement a tree menu, and the menu data is returned from the background. I found several articles on the Internet, and finally found a solution.

Scenario: According to business requirements, it is necessary to implement an active tree menu. The menu data is returned from the background. The final rendering is as follows:

Returned from the background The data format is like this:

data=[{
 pID:'1',//父ID
 name:'目录一',
menuID:'m1',//本身ID
              isContent:false//判断是否是目录
},
 {
pID:'1',
 name:'目录二',
menuID:'m2',
              isContent:false
 },
 {
 pID:'m1',
 name:'目录一--菜单一',
menuID:'m11',
              isContent:true
 },
 {
 pID:'m1',
 name:'目录一--目录一',
menuID:'m12',
              isContent:false
 },
           {
            pID:'m12',
 name:'目录一--目录一--菜单一',
menuID:'m121',
             isContent:true
 },
 {
 pID:'m2',
 name:'目录二--菜单一',
menuID:'m21',
              isContent:true
 },
 {
 pID:'m2',
 name:'目录二--菜单二',
menuID:'m22',
              isContent:true
 },
 ]
Copy after login

This is a string of data with a parent-child relationship. The first step is to convert this large string of data into a tree structure:

tree(){
        let data=[{
              pID:'1',//父ID
              name:'目录一',
              menuID:'m1',//本身ID
                isContent:false//判断是否是目录
            },
            {
              pID:'1',
              name:'目录二',
              menuID:'m2',
                isContent:false
            },
            {
              pID:'m1',
              name:'目录一--菜单一',
              menuID:'m11',
                isContent:true
            },
            {
              pID:'m1',
              name:'目录一--目录一',
              menuID:'m12',
                isContent:false
            },
             {
                pID:'m12',
              name:'目录一--目录一--菜单一',
              menuID:'m121',
               isContent:true
            },
            {
              pID:'m2',
              name:'目录二--菜单一',
              menuID:'m21',
                isContent:true
            },
            {
              pID:'m2',
              name:'目录二--菜单二',
              menuID:'m22',
                isContent:true
            },
          ]
        let tree = []
        for(let i=0;i<data.length;i++){
          if(data[i].pID == &#39;1&#39;){
            let obj = data[i]
            obj.list = []
            tree.push(obj)
            data.splice(i,1)
            i--
          }
        }
        menuList(tree)
        console.log(tree)
        function menuList(arr){
          if(data.length !=0){
            for(let i=0; i<arr.length;i++){
              for(let j=0;j<data.length;j++){
                if(data[j].pID == arr[i].menuID){
                  let obj = data[j]
                  obj.list = []
                  arr[i].list.push(obj)
                  data.splice(j,1)
                  j--
                }
              }
              menuList(arr[i].list)
            }
          }
        }
      }
Copy after login

Returned after running The structure is like this:

[{"pID":"1","name":"目录一","menuID":"m1","isContent":false,"list":[{"pID":"m1","name":"目录一--菜单一","menuID":"m11","isContent":true,"list":[]},{"pID":"m1","name":"目录一--目录一","menuID":"m12","isContent":false,"list":[{"pID":"m12","name":"目录一--目录一--菜单一","menuID":"m121","isContent":true,"list":[]}]}]},{"pID":"1","name":"目录二","menuID":"m2","isContent":false,"list":[{"pID":"m2","name":"目录二--菜单一","menuID":"m21","isContent":true,"list":[]},{"pID":"m2","name":"目录二--菜单二","menuID":"m22","isContent":true,"list":[]}]}]
Copy after login

Next, we will show the Navigationmenu component of element-ui used in the project. In order to achieve such a tree structure, each level The menu acts as a component alone, recursively by judging the value of isContent. I directly posted the code

<el-menu 
      theme="dark"
      :default-active="openMenuID"
      :default-openeds="openMenuArr"
      class="el-menu"
     @select="handleSelect">
      <template v-for="(item,index) in menuList">
         <el-submenu :index=item.menuID v-if="item.IsContent">
           <template slot="title">
          <i class="el-icon-menu"></i>
             {{item.name}}
           </template>
           <tree-menu :data="item.list"></tree-menu>
         </el-submenu>
         <el-menu-item :index=item.menuID v-else>{{item.name}}</el-menu-item>
      </template>
    </el-menu>
Copy after login

The code of the tree-menu component:

<template v-for="(menu,index) in data">
      <el-submenu :index=menu.menuID v-if="menu.IsContent">
        <template slot="title">
          <i class="el-icon-plus"></i>
          {{menu.name}}</template>
        <tree-menu :data="menu.list"></tree-menu>
      </el-submenu>
      <el-menu-item v-else :index=menu.menuID>
        {{menu.name}}
      </el-menu-item>
    </template>
Copy after login

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 the steps to use bootstrap in the vue-cli project

Detailed explanation of the use of vue event mechanism

The above is the detailed content of vue.js+element-ui implements tree structure. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!