element-ui之树形表格(treetable&&treegrid)

yizhihongxing

Element-UI之树形表格(treetable&&treegrid)

Element-UI是一套基于Vue.js的UI组件库,提供了丰富的组件和功能。其中,树形表格(treet&&treegrid)是一种常用的组,用于展示具有层级关系的数据。本文将介绍如何使用Element-UI的树形表格组件,并提供两个例说明。

1. treetable和treegrid的区别

在Element-UI中,树形表格有两种实现方式:treetable和treegrid。它们的区别在于:

  • treetable:每一行都可以展开子行,子行和父行在同一级别。
  • treegrid:每一行都可以展子行,子行和父行在不同的级别。

2. 使用treetable实现树形表格

使用treetable实现树形表格的步骤如下:

  1. 引入treetable组件:
<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column typeexpand"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import { ElTable, ElTableColumn } from 'element-ui'

export default {
  components: {
    ElTable,
    ElTableColumn
  },
  data() {
    return {
      tableData: [
        {
          name: '张三',
          age: 18,
          address: '北京市朝阳区',
          children: [
            {
              name: '李四',
              age: 20,
              address: '北京市海淀区'
            },
            {
              name: '王五',
              age: 22,
              address: '北京市西城区'
            }
          ]
        },
        {
          name: '赵六',
          age: 25,
          address: '北京市东城区'
        }
      ]
    }
  }
}
</script>

上面的代码使用了treetable组件展示了一个包含层级关系的数据。

  1. 配置展开行:
<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <div>
            <el-table :data="props.row.children" style="width: 100%">
              <el-table-column prop="name" label="名称"></el-table-column>
              <el-table-column prop="age" label="年龄"></el-table-column>
              <el-table-column prop="address" label="地址"></el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
  </div>
</template>

上面的代码使用了type="expand"配置了展开行,并使用了slot-scope插槽自定义了展开行的内容。

3. 使用treegrid实现树形表格

使用treegrid实现树形表格的步骤如下:

  1. 引入treegrid组件:
<template>
  <div>
    <el-table :data="tableData" style="width: 100%" :tree-props="{ children: '', hasChildren: 'hasChildren' }">
      <el-table-column type="expand"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import { ElTable, ElTableColumn } from 'element-ui'

export default {
  components: {
    ElTable,
    ElTableColumn
  },
  data() {
    return {
      tableData: [
        {
          name: '张三',
          age: 18,
          address: '北京市朝阳区',
          hasChildren: true,
          children: [
            {
              name: '李四',
              age: 20,
              address: '北京市海淀区',
              hasChildren: false
            },
            {
              name: '王五',
              age: 22,
              address: '北京市西城区',
              hasChildren: false
            }
          ]
        },
        {
          name: '赵六',
          age: 25,
          address: '北京市东城区',
          hasChildren: false
        }
      ]
    }
  }
}
</script>

上面的代码使用了treegrid组件展示了一个包含层级关系的数据。

  1. 配置展开行:
<template>
  <div>
    <el-table :data="tableData" style="width: 100%" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column type="expand">
        <template slot-scope="props">
          <div>
            <el-table :data="props.row.children" style="width: 100%" :tree-props="{ children: 'children', hasChildren: 'hasChildren }">
              <el-table-column type="expand"></el-table-column>
              <el-table-column prop="name" label="名称"></el-table-column>
              <el-table-column prop="age" label="年龄"></el-table-column>
              <el-table-column prop="address" label="地址"></el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
  </div>
</template>

上面的代码使用了type="expand"配置了展开行,并使用了slot-scope插槽自定义了展开行的内容。同时,使用了:tree-props配置了treegrid的相关属性。

4. 示例1:使用treetable实现树形表格

<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <div>
            <el-table :data="props.row.children" style="width: 100%">
              <el-table-column prop="name" label="名称"></el-table-column>
              <el-table-column prop="age" label="年龄"></el-table-column>
              <el-table-column prop="address" label="地址"></el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import { ElTable, ElTableColumn } from 'element-ui'

export default {
  components: {
    ElTable,
    ElTableColumn
  },
  data() {
    return {
      tableData: [
        {
          name: '张三',
          age: 18,
          address: '北京市朝阳区',
          children: [
            {
              name: '李四',
              age: 20,
              address: '北京市海淀区'
            },
            {
              name: '王五',
              age: 22,
              address: '北京市西城区'
            }
          ]
        },
        {
          name: '赵六',
          age: 25,
          address: '北京市东城区'
        }
      ]
    }
  }
}
</script>

上面的代码使用了treetable组件展示了一个包含层级关系的数据。

5. 示例2:使用treegrid实现树形表格

<template>
  <div>
    <el-table :data="tableData" style="width: 100%" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column type="expand">
        <template slot-scope="props">
          <div>
            <el-table :data="props.row.children" style="width: 100%" :tree-props="{ children: 'children', hasChildren: 'hasChildren }">
              <el-table-column type="expand"></el-table-column>
              <el-table-column prop="name" label="名称"></el-table-column>
              <el-table-column prop="age" label="年龄"></el-table-column>
              <el-table-column prop="address" label="地址"></el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import { ElTable, ElTableColumn } from 'element-ui'

export default {
  components: {
    ElTable,
    ElTableColumn
  },
  data() {
    return {
      tableData: [
        {
          name: '张三',
          age: 18,
          address: '北京市朝阳区',
          hasChildren: true,
          children: [
            {
              name: '李四',
              age: 20,
              address: '北京市海淀区',
              hasChildren: false
            },
            {
              name: '王五',
              age: 22,
              address: '北京市西城区',
              hasChildren: false
            }
          ]
        },
        {
          name: '赵六',
          age: 25,
          address: '北京市东城区',
          hasChildren: false
        }
      ]
    }
  }
}
</script>

上面的代码使用了treegrid组件展示了一个包含层级关系的数据。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:element-ui之树形表格(treetable&&treegrid) - Python技术站

(0)
上一篇 2023年5月7日
下一篇 2023年5月7日

相关文章

  • springboot如何使用@ConfigurationProperties封装配置文件

    使用@ConfigurationProperties可以将配置文件中的属性值绑定到JavaBean中,方便管理和使用。下面是完整的“springboot如何使用@ConfigurationProperties封装配置文件”的攻略: 1. 引入依赖 在pom.xml中引入spring-boot-configuration-processor依赖: <de…

    other 2023年6月25日
    00
  • MybatisPlus字段类型转换的实现示例

    下面是详细讲解”MybatisPlus字段类型转换的实现示例”的完整攻略。 一、背景 Mybatis-Plus是Mybatis的增强工具,它提供了很多便利的功能,例如自动生成代码、分页查询、条件构造器等。另外,它还提供了对实体类中字段类型进行转换的功能。 二、实现方式 MybatisPlus对实体类中的字段类型进行转换的实现方式有两种: 1.注解方式 在要转…

    other 2023年6月25日
    00
  • MySQL实现字段或字符串拼接的三种方式总结

    以下是MySQL实现字段或字符串拼接的三种方式总结的详细攻略。 方式一:使用concat函数进行字段或字符串拼接 基本语法 concat(str1,str2,…) 说明:str1,str2为要拼接的字段或字符串,可以有多个参数,拼接的顺序按照参数的输入顺序。 示例说明 例如,有一张学生信息表,字段包括:学号、姓名、性别、年龄,如下所示: stu_id s…

    other 2023年6月25日
    00
  • 基于MATLAB实现的云模型计算隶属度

    基于MATLAB实现的云模型计算隶属度 云计算是当前热门的话题,而基于云的云模型也被广泛运用在各种场景中。本文将介绍如何利用MATLAB来实现云模型计算隶属度。 什么是云模型? 云模型是由李纪为教授提出的,是一种将数量化问题变成概率性问题的解决方法。云模型的核心是将数值与非数值相互转化,使得模糊模型可以被量化。本文不会对云模型的原理进行详细介绍,有兴趣的读者…

    其他 2023年3月28日
    00
  • mac平台下部署ue4工程到ios设备的流程

    以下是在Mac平台下部署UE4工程到iOS设备的完整攻略,包含两个示例说明: 步骤1:安装必要的软件 在Mac平台上部署UE4工程到iOS设备之前,需要安装以下软件: Xcode:用于编译iOS应用程序。 Unreal Engine 4:用于创建和编辑UE4工程。 iOS设备驱动程序:用于将iOS设备连接到Mac电脑。 步骤2:设置UE4工程 在UE4中设置…

    other 2023年5月9日
    00
  • spring中bean id相同引发故障的分析与解决

    一、背景及问题分析 在Spring框架中,我们定义Bean对象时可以通过id属性来指定该Bean的唯一标识,通常一个id唯一对应一个Bean对象。假设在我们代码实现中定义了两个id相同的Bean对象,那么Spring框架使用该id获取Bean对象时将会发生什么? 考虑下面的示例: <bean id="person" class=&q…

    other 2023年6月27日
    00
  • 如何解决Win10系统提示初始化配置资源失败?

    问题描述: Win10系统在开机或者使用的时候突然提示“初始化配置资源失败”的错误提示,这会导致系统无法正常运行,给用户带来了极大的困扰。 解决攻略: 检查系统文件和驱动程序 Win10系统的初始化配置资源失败主要是因为系统文件或驱动程序出现了故障或损坏导致的,因此要解决这个问题,首先要检测系统文件和驱动程序的状态,如果存在问题,需要及时进行修复或更新。 打…

    other 2023年6月20日
    00
  • 【前端基础】动态脚本与JSONP

    【前端基础】动态脚本与JSONP 在前端开发中,动态脚本和JSONP是两个非常重要的概念,它们可以帮助我们更好地构建Web应用。本文将介绍这两个概念,以及如何在实际开发中使用它们。 动态脚本 动态脚本是指在客户端动态加载、执行的JavaScript脚本。在传统的静态页面中,所有的脚本都是在HTML中直接写出来的,但是当我们面对一些复杂的业务逻辑时,需要动态地…

    其他 2023年3月28日
    00
合作推广
合作推广
分享本页
返回顶部