封装的el-table组件
<template>
<el-table
border
v-loading="loading"
:data="tableData"
@selection-change="selectionChange"
style="width: 100%">
<el-table-column
v-if="tableType"
:type="tableType"
width="55">
</el-table-column>
<template v-for="item in tableConfig">
<el-table-column
v-if="item.slotName"
:sortable="item.sortable"
:prop="item.field"
:min-width="item.minWidth"
:label="item.label"
>
<template slot-scope="scope">
<slot :name="item.slotName" :scope="scope"></slot>
</template>
</el-table-column>
<el-table-column
v-else
:sortable="item.sortable"
:prop="item.field"
:min-width="item.minWidth"
:label="item.label"
>
<template slot-scope="scope">
<span @click="() => {$emit(item.method, scope.row)}">{{scope.row[item.field]}}</span>
</template>
</el-table-column>
</template>
</el-table>
</template>
<script>
export default {
props: {
loading: {
type: Boolean,
default: false
},
tableType: String,
tableConfig:Array,
tableData: Array
},
methods: {
selectionChange(selection) {
this.$emit('selectionChange',selection)
}
}
}
</script>
父组件中的使用
<template>
<MyTable
:tableData="tableData"
:tableConfig="tableConfig"
:loading="loading"
tableType="selection" >
<template v-slot:operation="slotData">
<el-button type="primary" size="mini">添加</el-button>
<el-button type="danger" size="mini">删除</el-button>
</template>
</MyTable>
</template>
<script>
export default {
data() {
return {
loading: false,
tableData:[{
name:'张山',
sex: 18
}],
tableConfig: [
{
field: 'name',
label: '姓名',
sortable: true,
minWidth: 60
},
{
field: 'sex',
label: '年龄',
},
{
label: '操作',
slotName: 'operation',
}
],
}
}
}
</script>