基于vue+Element Table 表格的封装

项目场景:

项目场景:需要频繁使用列表进行呈现数据,不可能每次都写一个表格,可以将表格封装为一个组件,在需要使用时可以直接调用。


效果展示:

在这里插入图片描述


项目结构:

在这里插入图片描述

具体实现:

Table.vue

<!-- 

  component:列表
  time:2022/11/15

  UserData:列数据
  tableConfig:表格的列配置,[UserId,key,label,width]
  getTableHeaderStyle:表格CSS类名

  表格数据:
  tableConfig:[
    {
      label:'姓名',
      width:150,
      key:'UserName'
    }
  ]
  UserData:[
    {
      "UserId":"1",
      "UserName":"杠铁侠",
      "UserNameValue":'1',
      "UserGender":"男",
      "UserAge":"31",
      "UserItem":"斯塔克工业",
      "UserItemValue":"1",
      "UserPostion":"Java工程师",
      "UserPostionValue":"1",
      "UserPhone": "15987462455",
      "UserEmail": "154893@163.com",
      "UserEntryTime":"2020-8-27",
      "Useraddress":"上海市普陀区金沙江路 1518 弄"
    },
  ]

-->
<template>
  <div class="UserTable">
    <el-table
      :data="UserData"
      style="width: 100%"
      :header-cell-style="getTableHeaderStyle"
    >
      <template slot="empty">
        <el-empty :image-size="100" description='暂无数据'></el-empty>
      </template>
      <el-table-column
        v-for="column in tableConfig"
        :key="column.UserId"
        :prop=column.key
        :label=column.label
        :width=column.width>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
          <el-button type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data(){
    return{
      Userdata:[],
      tableconfig:[],
    }
  },
  props:{
    tableConfig:{
      default(){
        return [];
      }
    },
    UserData:{
      default(){
        return [];
      }
    },
  },
  methods: {
    getTableHeaderStyle(){
      return "background:#eee"
    },
    handleClick(row) {
      console.log(row);
    },
  },
}
</script>

<style>
.el-table{
  padding: 0px 10px 0px 0px;
}
.el-table__body-wrapper{
  padding-bottom: 10px;
}
</style>

UserManagement.vue

<template>
	<!-- 只需要传入数据及配置 -->
	<Table :UserData="Userdata" :tableConfig="tableconfig"></Table>
</template>

<script>
import UserData from '@/data/User'
import Table from '@/views/components/Table.vue'
export default {
  props:{UserData},
  components:{
    Table
  },
  data(){
    Userdata:[],
    tableconfig:[],
  }
  methods(){
  	Tableconfig(){  //列表配置
      this.tableconfig = [
        { //用户姓名
          label:'姓名',
          width:140,
          key:'UserName',
        },
        { //用户性别
          label:'性别',
          width:140,
          key:'UserGender',
        },
        { //用户年龄
          label:'年龄',
          width:140,
          key:'UserAge',
        },
        { //用户团队
          label:'团队',
          width:180,
          key:'UserItem',
        },
        { //用户职位
          label:'职位',
          width:200,
          key:'UserPostion',
        },
        { //用户电话
          label:'电话',
          width:200,
          key:'UserPhone',
        },
        { //用户邮箱
          label:'邮箱',
          width:250,
          key:'UserEmail',
        },
        { //用户入职时间
          label:'入职时间',
          width:150,
          key:'UserEntryTime',
        },
        { //用户地址
          label:'地址',
          width:300,
          key:'Useraddress',
        },
      ]
    },
	},
  mounted(){
    this.Userdata = UserData
    this.Tableconfig()
  },
}
</script>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wh1T3ZzT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值