开发中是否会遇见在一个页面中加载的table的列是不固定的,列名需要根据后台数据而动态加载;so element ui 的table 已经不再满足需求,我们得在他的基础上再次封装
增加 refactor_table.vue 组件
<template
>
<el
-table
:data
="tableData"
border
:height
="tableHeight"
style
="width: 100%"
v
-loading
="tableLoading"
:size
="tableSize"
>
<el
-table
-column
v
-for="(th, key) in tableColumnsConfig"
:key
="key"
:prop
="th.prop"
:label
="th.label"
:fixed
="th.fixed"
:width
="th.width"
:align
="th.align"
show
-overflow
-tooltip
="true">
<template slot
-scope
="scope">
<div v
-if="th.prop==''">
<el
-button v
-for="(btn,index) in th.operate" type
="text" size
="small" :key
="index"
:style
="btn.type=='del'?'color:#ff3e3e' : ''" @click
="btnHandle(scope.row,btn.type)">
{{btn
.name
}}
</el
-button
>
</div
>
<div v
-else>
<span
>{{ scope
.row
[th
.prop
] }}</span
>
</div
>
</template
>
</el
-table
-column
>
</el
-table
>
</template
>
<script
>
export default {
name
: 'refactor_table',
props
: {
tableData
: {
type
: Array
,
default: function () {
return []
}
},
tableLoading
: {
type
: Boolean
,
default: false
},
tableHeight
: {
type
: Number
},
tableSize
:{
type
:String
},
tableColumnsConfig
: {
type
: Array
,
default: function () {
return []
}
}
},
methods
: {
btnHandle(row
, type
) {
this.$emit("operateHandle", row
, type
)
}
}
}
</script
>
在main.ve中调用
<template
>
<div
>
<refactor
-table
:table
-data
="tableData"
:table
-columns
-config
="tableColumns"
:table
-loading
="loading"
:tableSize
="tableSize"
@operateHandle
="tableOperateHandle"
></refactor
-table
>
</div
>
</template
>
<script type
="text/ecmascript-6">
import RefactorTable
from '@/components/refactor_table';
export default {
data() {
return {
tableHeight
: 300,
tableData
: [],
tableColumns
: [],
tableSize
: 'mini'
}
},
created() {
this.loadingTable();
},
methods
: {
loadingTable() {
this.tableData
= [
{id
: '1938238', name
: '节点', grade
: 'ERWFD'},
{id
: '3241', name
: '节点B', grade
: 'FDD'},
{id
: '8238', name
: '节点C', grade
: 'FVDFA'},
{id
: '3424', name
: '节点', grade
: 'ERWFD'},
{id
: '32ree', name
: '节点B', grade
: 'FDD'},
{id
: '821221', name
: '节点C', grade
: 'FVDFA'},
{id
: '89238', name
: '节点', grade
: 'ERWFD'},
{id
: '323432', name
: '节点B', grade
: 'FDD'},
{id
: '2231545', name
: '节点C', grade
: 'FVDFA'},
{id
: '213435', name
: '节点C', grade
: 'FVDFA'}
];
for(let key
in this.tableData
[0]){
this.tableColumns
.push({
prop
: key
,
label
: key
,
align
: 'center'
});
}
this.tableColumns
.push( {
prop
: '',
label
: '操作',
width
: 280,
align
: 'center',
operate
:[
{
type
:'del',
name
:'删除',
},
{
type
:'add',
name
:'新增',
}
]});
},
tableOperateHandle(row
,type
){
console
.log(row
,type
)
}
},
components
: {
RefactorTable
}
}
</script
>
运行预览
转载请注明原文地址: https://mac.8miu.com/read-510749.html