vue+element UI实现树形表格


Posted in Vue.js onDecember 29, 2020

本文实例为大家分享了vue+element UI实现树形表格的具体代码,供大家参考,具体内容如下

一、在component文件夹下新建如下treeTable文件夹,里面有2个文件:

vue+element UI实现树形表格

eval.js:将数据转换成树形数据

/**
* @Author: jianglei
* @Date:  2017-10-12 12:06:49
*/
'use strict'
import Vue from 'vue'
export default function treeToArray(data, expandAll, parent = null, level = null) {
 let tmp = []
 Array.from(data).forEach(function(record) {
  if (record._expanded === undefined) {
   Vue.set(record, '_expanded', expandAll)
  }
  let _level = 1
  if (level !== undefined && level !== null) {
   _level = level + 1
  }
  Vue.set(record, '_level', _level)
  // 如果有父元素
  if (parent) {
   Vue.set(record, 'parent', parent)
  }
  tmp.push(record)
  if (record.children && record.children.length > 0) {
   const children = treeToArray(record.children, expandAll, record, _level)
   tmp = tmp.concat(children)
  }
 })
 return tmp
}

index.vue:树形表格组件

<template>
 <el-table :data="formatData" :row-style="showRow" v-bind="$attrs">
  <el-table-column v-if="columns.length===0" width="150">
   <template slot-scope="scope">
    <span v-for="space in scope.row._level" :key="space" class="ms-tree-space"/>
    <span v-if="iconShow(0,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)">
     <i v-if="!scope.row._expanded" class="el-icon-plus"/>
     <i v-else class="el-icon-minus"/>
    </span>
    {{ scope.$index }}
   </template>
  </el-table-column>
  <el-table-column v-for="(column, index) in columns" v-else :key="column.value" :label="column.text" :width="column.width">
   <template slot-scope="scope">
    <!-- Todo -->
    <!-- eslint-disable-next-line vue/no-confusing-v-for-v-if -->
    <span v-for="space in scope.row._level" v-if="index === 0" :key="space" class="ms-tree-space"/>
    <span v-if="iconShow(index,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)">
     <i v-if="!scope.row._expanded" class="el-icon-plus"/>
     <i v-else class="el-icon-minus"/>
    </span>
    {{ scope.row[column.value] }}
   </template>
  </el-table-column>
  <slot/>
 </el-table>
</template>
 
<script>
/**
 Auth: Lei.j1ang
 Created: 2018/1/19-13:59
*/
import treeToArray from "./eval";
export default {
 name: "TreeTable",
 props: {
  /* eslint-disable */
  data: {
   type: [Array, Object],
   required: true
  },
  columns: {
   type: Array,
   default: () => []
  },
  evalFunc: Function,
  evalArgs: Array,
  expandAll: {
   type: Boolean,
   default: false
  }
 },
 computed: {
  // 格式化数据源
  formatData: function() {
   let tmp;
   if (!Array.isArray(this.data)) {
    tmp = [this.data];
   } else {
    tmp = this.data;
   }
   const func = this.evalFunc || treeToArray;
   const args = this.evalArgs
    ? Array.concat([tmp, this.expandAll], this.evalArgs)
    : [tmp, this.expandAll];
   return func.apply(null, args);
  }
 },
 methods: {
  showRow: function(row) {
   const show = row.row.parent
    ? row.row.parent._expanded && row.row.parent._show
    : true;
   row.row._show = show;
   return show
    ? "animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;"
    : "display:none;";
  },
  // 切换下级是否展开
  toggleExpanded: function(trIndex) {
   const record = this.formatData[trIndex];
   record._expanded = !record._expanded;
  },
  // 图标显示
  iconShow(index, record) {
   return index === 0 && record.children && record.children.length > 0;
  }
 }
};
</script>
<style rel="stylesheet/css">
@keyframes treeTableShow {
 from {
  opacity: 0;
 }
 to {
  opacity: 1;
 }
}
@-webkit-keyframes treeTableShow {
 from {
  opacity: 0;
 }
 to {
  opacity: 1;
 }
}
</style>
 
<style scoped>
.ms-tree-space {
 position: relative;
 top: 1px;
 display: inline-block;
 font-style: normal;
 font-weight: 400;
 line-height: 1;
 width: 18px;
 height: 14px;
}
.ms-tree-space::before {
 content: "";
}
.processContainer {
 width: 100%;
 height: 100%;
}
table td {
 line-height: 26px;
}
.tree-ctrl {
 position: relative;
 cursor: pointer;
 color: #2196f3;
 margin-left: -18px;
}
</style>

二、在需要的地方引入该组件

例如:在component文件夹下新建a.vue:

<tree-table :data="data" :columns="columns" border/>

import treeTable from "./TreeTable";

components: { treeTable },
data() {
  return {
   columns: [
    {
     text: "事件",
     value: "event",
     width: 200
    },
    {
     text: "ID",
     value: "id"
    },
    {
     text: "时间线",
     value: "timeLine"
    },
    {
     text: "备注",
     value: "comment"
    }
   ],
   data: [
    {
     id: 0,
     event: "事件1",
     timeLine: 50,
     comment: "无"
    },
    {
     id: 1,
     event: "事件1",
     timeLine: 100,
     comment: "无",
     children: [
      {
       id: 2,
       event: "事件2",
       timeLine: 10,
       comment: "无"
      },
      {
       id: 3,
       event: "事件3",
       timeLine: 90,
       comment: "无",
       children: [
        {
         id: 4,
         event: "事件4",
         timeLine: 5,
         comment: "无"
        },
        {
         id: 5,
         event: "事件5",
         timeLine: 10,
         comment: "无"
        },
        {
         id: 6,
         event: "事件6",
         timeLine: 75,
         comment: "无",
         children: [
          {
           id: 7,
           event: "事件7",
           timeLine: 50,
           comment: "无",
           children: [
            {
             id: 71,
             event: "事件71",
             timeLine: 25,
             comment: "xx"
            },
            {
             id: 72,
             event: "事件72",
             timeLine: 5,
             comment: "xx"
            },
            {
             id: 73,
             event: "事件73",
             timeLine: 20,
             comment: "xx"
            }
           ]
          },
          {
           id: 8,
           event: "事件8",
           timeLine: 25,
           comment: "无"
          }
         ]
        }
       ]
      }
     ]
    }
   ]
  };
 },

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
Vue开发中常见的套路和技巧总结
Nov 24 Vue.js
vue+element UI实现树形表格
Dec 29 Vue.js
jenkins自动构建发布vue项目的方法步骤
Jan 04 Vue.js
vue实现防抖的实例代码
Jan 11 Vue.js
如何在vue中使用HTML 5 拖放API
Jan 14 Vue.js
vue实现无缝轮播效果(跑马灯)
May 14 Vue.js
解决Vue+SpringBoot+Shiro跨域问题
Jun 09 Vue.js
SSM VUE Axios详解
Oct 05 Vue.js
vue报错function () { [native code] },无法出现我们想要的内容 Unknown custom element
Apr 11 Vue.js
如何优化vue打包文件过大
Apr 13 Vue.js
el-table-column 内容不自动换行的解决方法
Aug 14 Vue.js
Vue3实现简易音乐播放器组件
Aug 14 Vue.js
vue实现树状表格效果
Dec 29 #Vue.js
vue实现图书管理系统
Dec 29 #Vue.js
Vue实现随机验证码功能
Dec 29 #Vue.js
vue3+typeScript穿梭框的实现示例
Dec 29 #Vue.js
Vue.extend 登录注册模态框的实现
Dec 29 #Vue.js
vue实现简易的双向数据绑定
Dec 29 #Vue.js
vue中配置scss全局变量的步骤
Dec 28 #Vue.js
You might like
DOTA2 6.87版本后新眼位详解攻略
2020/04/20 DOTA
PHP分页显示制作详细讲解
2006/10/09 PHP
PHP配置把错误日志以邮件方式发送方法(Windows系统)
2015/06/23 PHP
PHP 计算至少是其他数字两倍的最大数的实现代码
2020/05/26 PHP
Add a Formatted Table to a Word Document
2007/06/15 Javascript
在浏览器中获取当前执行的脚本文件名的代码
2011/07/19 Javascript
js实现在页面上弹出蒙板技巧简单实用
2013/04/16 Javascript
网页广告中JS代码的信息监听示例
2014/04/02 Javascript
js实现类似新浪微博首页内容渐显效果的方法
2015/04/10 Javascript
web前端开发JQuery常用实例代码片段(50个)
2015/08/28 Javascript
JS+CSS实现的经典圆角下拉菜单效果代码
2015/10/21 Javascript
JS for...in 遍历语句用法实例分析
2016/08/24 Javascript
React实现双向绑定示例代码
2016/09/19 Javascript
vue mint-ui tabbar变组件使用
2018/05/04 Javascript
JS实现可用滑块滑动的缓动图代码
2019/09/01 Javascript
简单实现节流函数和防抖函数过程解析
2019/10/08 Javascript
Angular+ionic实现折叠展开效果的示例代码
2020/07/29 Javascript
Python入门篇之条件、循环
2014/10/17 Python
用python读写excel的方法
2014/11/18 Python
pyqt 实现QlineEdit 输入密码显示成圆点的方法
2019/06/24 Python
详解pyinstaller selenium python3 chrome打包问题
2019/10/18 Python
python数据预处理 :数据共线性处理详解
2020/02/24 Python
PyQt5中QSpinBox计数器的实现
2021/01/18 Python
Biblibili视频投稿接口分析并以Python实现自动投稿功能
2021/02/05 Python
Html5页面内使用JSON动画的实现
2019/01/29 HTML / CSS
SQL Server 2000数据库的文件有哪些,分别进行描述。
2015/11/09 面试题
财务管理专业应届毕业生求职信
2013/09/22 职场文书
家庭教育先进个人事迹材料
2014/01/24 职场文书
联谊活动策划书
2014/01/26 职场文书
文体活动实施方案
2014/03/27 职场文书
2014年学习厉行节约反对浪费思想汇报
2014/09/10 职场文书
党的群众路线个人对照检查材料
2014/09/23 职场文书
文体活动总结
2015/02/04 职场文书
Win10系统下配置Java环境变量
2021/06/13 Java/Android
Java 多态分析
2022/04/26 Java/Android
Win10玩csgo闪退如何解决?Win10玩csgo闪退的解决方法
2022/07/23 数码科技