

基于vue+Element Table 表格的封装
source link: https://blog.51cto.com/u_15928719/5989464
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

基于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": "[email protected]",
"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>
- 1赞
- 收藏
- 评论
- 分享
- 举报
Recommend
-
24
明晚八点准时走,谁不打卡谁是狗。 使用过 element-ui 的表格的同学应该都有这样的体会,做一个简单的表格还比较容易,但如果这个表格包含了顶部的按钮,还有分页,甚至再包含了行编辑,那开发工作量就成倍的增加...
-
7
【记录】Element表格动态表头及数据发布于 5 分钟前实现方式1、取数据列表中第一条作为表头数据,再遍历删除不是表头的固定属性,结果赋值...
-
8
vue+element中table树形结构懒加载发布于 今天 09:15 1.开发环境 vue+element2.电脑系统 windows10专业版3.在开发的过程中,我们会遇到树形结构的表格,...
-
8
element el-table 表格列 v-if 显示 隐藏 乱序问题时间: 05/31/2020作者: ll浏览量: 1517 当tab为投保人时,表格中显示字段为投保人相关信息 当tab为被保人时,表格中显示字段为被保人相关信息 我在来回切...
-
7
、慕白 posts - 38, comments - 2, trackbacks - 0, articles - 0
-
5
#yyds干货盘点 element-tree-grid(表格树)的使用 原创 写在前面: e...
-
6
Element Plus修改表格行、单元格样式实习工作需要根据表格的状态字段来设置行的样式,记录一波。 先来一下基础配置。(Vue3) <template> <el-table :data="tableData" bord...
-
12
在 Vue3 + Element Plus 中生成动态表格,动态修改表格,多级表头,合并单元格stone前端工程师最近更新 2022年...
-
10
基于vue+Element UI的文件上传(可拖拽上传) 精选 原创
-
3
基于vue+Element Table封装(纯前端解决方案,附源码) 精选 原创 这个项目是拿来练手的项目,基于VUE+Eleme...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK