基于vue+iview 实现treetable

(((( 新人光环 ))))

项目中刚刚遇到这种treetable的需求,于是在www.npmjs.com/找到了vue-table-with-tree-grid,上手简单

只为了安利,详细学习请戳链接: 原文链接原例子

一个简单的例子

效果图

.vue文件如下

<template lang="html">
    <zk-table 
        ref="table" 
        sum-text="sum"
        index-text="#" 
        :data="treeTableData.data" 
        :columns="columns" 
        :stripe="props.stripe" 
        :border="props.border" 
        :show-header="props.showHeader" 
        :show-summary="props.showSummary"
        :show-row-hover="props.showRowHover"
        :show-index="props.showIndex"
        :tree-type="props.treeType" 
        :is-fold="props.isFold" 
        :expand-type="props.expandType"
        :selection-type="props.selectionType">
        <!-- 原文中 scope="scope" 会警告, 2.5版本后应为slot-scope="scope"-->
        <template slot="active" slot-scope="scope">
            <!--  ... ... (图中对应状态栏的绿点儿)-->
        </template>
        
        <template slot="empower" slot-scope="scope">
            <!-- ... ... (为授权的图标,可在这里添加点击事件)-->
        </template>
    </zk-table>
</template>
<script>
export default {
    name: 'example',
    //表格各行的数据
    data() {
      return {
        props: {
          stripe: false,//是否显示间隔斑马纹
          border: false,//是否显示纵向边框
          showHeader: true,//是否显示表头
          showSummary: false,//是否显示表尾合计行
          showRowHover: true,//鼠标悬停时,是否高亮当前行
          showIndex: false,//是否显示数据索引
          treeType: true,//是否为树形表格
          isFold: true,//树形表格中父级是否默认折叠
          expandType: false,//是否为展开行类型表格(为 True 时,需要添加作用域插槽, 它可以获取到 row, rowIndex)
          selectionType: false,//是否显示间隔斑马纹
        },
        data: [
          {
            name: '根组织',
            description: '111',
            owner:'admin',
            active: true,
            children: [
              {
                name: '大中华区',
                description: '',
                owner: '',
                active: true,
                children: [
                  {
                    name: '浙江省',
                    description: '',
                    owner: '',
                    active: true,
                    children: [
                      {
                        name: '杭州市',
                        description: '',
                        owner: '',
                        active: true
                      }
                    ]
                  }
                ]
              }
            ],
          },
          {
            name: 'Tom',
            sex: 'male',
            likes: ['football', 'basketball'],
            score: 20,
          },
        ],
        //表格标题数据
        columns: [
            {
                label: "用户组名",
                prop: "name"
            },
            {
                label: "描述",
                prop: "description",
                minWidth: "50px"
            },
            {
                label: "所有者",
                prop: "owner"
            },
            {
                label: "状态",
                prop: "active",
                type: "template",
                template: "active"
            },
            {
                label: "授权",
                type: "template",
                template: "empower"
            }
        ]
      };
    }
  };
</script>
复制代码

table 可配置的属性

属性说明类型参数默认值
data表格各行的数据Array-[]
empty-text表格数据为空时显示的文字String-'暂无数据'
columns表格各列的配置Array-[]
show-header是否显示表头Boolean-true
show-index是否显示数据索引Boolean-false
index-text数据索引名称String-'序号'
show-summary是否显示表尾合计行Boolean-false
sum-text表尾合计行首列名称String-'合计'
summary-method表尾合计行计算方法Functiondata, column, columnIndex-
max-height最大高度[String, Number]-'auto'
stripe是否显示间隔斑马纹Boolean-false
border是否显示纵向边框Boolean-'false'
show-row-hover鼠标悬停时是否高亮当前行Boolean-true
tree-type是否为树形表格Boolean-false
children-prop树形表格中遍历的属性名称String-children
is-fold树形表格中父级是否默认折叠Boolean-true
expand-type是否为展开行类型表格(为 True 时,需要添加名称为 '$expand' 的作用域插槽, 它可以获取到 row, rowIndex)Boolean-false
selection-type是否为多选类型表格Boolean-false
row-key行数据的 Key,用来优化 Table 的渲染Functionrow, rowIndexrowIndex

Columns 可配置的属性

属性说明类型默认值
label列标题名称String''
prop对应列内容的属性名String''
align对应列内容的对齐方式,可选值有 'center', 'right'String'left'
headerAlign对应列标题的对齐方式,可选值有 'center', 'right'String'left'
width列宽度[String, Number]'auto'
minWidth列最小宽度[String, Number]'80px'
type列类型,可选值有 'template'(自定义列模板)String''
template列类型为 'template'(自定义列模板) 时,对应的作用域插槽(它可以获取到 row, rowIndex, column, columnIndex)名称String''

(... ...省略号,不行了,点原文链接查询所有api)

总结

在使用vue-table-with-tree-grid之前,我尝试iview自带的table展开来做这个功能

iview-table

table展开后判断树形数据中是否有children。

如果有,展开后还是一个 可展开的table。

若果没有,设置该数据_disableExpand,禁用展开

刚开始数据只有三个层级时,完全可以,但数据增加到7个时(具体多少个,没去研究)浏览器会报一个大概是无限循环的错。具体原因还没有去钻研。

项目紧急,在找到vue-table-with-tree-grid后,仅用不到半天的时间,便实现了。

带着感激之情安利开源。

.

.

.

.

.

.

.

.

. 出一口气

Logo

前往低代码交流专区

更多推荐