table-cmp组件
<template>
<el-table
ref="table"
v-loading="loading"
element-loading-text="Loading"
:data="tableData"
border fit highlight-current-row
tooltip-effect="dark"
style="width:100%"
@sort-change="handleSortChange"
@selection-change="handleSelectionChange">
<el-table-column v-for="(item,index) in tableLabel" :width="item.width ? item.width : ''" :key="index" :align="item.align" :label="item.label" :prop="item.param" :sortable="item.sortable ? 'custom' : false">
<template slot-scope="scope">
<span v-if="item.render">
{{item.render(scope.row)}}
</span>
<span v-else>{{scope.row[item.param]}}</span>
</template>
</el-table-column>
<el-table-column v-if="tableOption.label" :width="tableOption.width" :label="tableOption.label" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
<el-button v-for="(item,index) in tableOption.options" :key="index" :type="item.type" :icon="item.icon" @click="handleButton(item.methods,scope.row,scope.row)" size="mini">
{{item.label}}
</el-button>
</template>
</el-table-column>
</el-table>
</template>
JS
<script>
export default {
props:{
loading:{
type:Boolean,
default:false
},
tableData:{
type:Array,
default: () => {
return []
}
},
tableLabel:{
type:Array,
default: () => {
return []
}
},
tableOption:{
type:Object,
default: () => {
return {}
}
}
},
components:{},
methods: {
handleButton(methods,row,index){ // 按钮事件
this.$emit('handleButton',{'methods':methods,'row':row,'index':index})
},
handleSortChange(val){ // 排序
this.$emit('handleSortChange',val)
},
handleSelectionChange(val){
this.$emit('handleSelectionChange',val)
}
}
}
</script>
使用方法
1、导入组件
import tableCmp from '@/components/table-cmp/index'
2、模板中加入导入组件
<table-cmp
:loading="loading"
:table-data="tableData"
:table-label="tableLabel"
:table-option="tableOption"
@handleButton="handleButton"
@handleSortChange="handleSortChange"
@handleSelectionChange="handleSelectionChange"
></table-cmp>
3、data数据
tableData:[],
tableLabel: [
{ label: '用户名', param: 'usr', align: 'center',sortable:true },
{ label: '公司名称', param: 'company', align: 'center' },
{ label: '办公邮箱', param: 'email', align: 'center',width:'200' },
{ label: '注册时间', param: 'registTime', align: 'center',sortable:true },
{ label: '审核状态', param: 'status', align: 'center',sortable:true, render: (row) => {
if (row.status === 0) {
return '未审核'
} else if (row.status === 1) {
return '审核通过'
} else if(row.status ===2) {
return '审核不通过'
} else {
return '禁用'
}
}
}
],
tableOption: {
label: '操作',
width: '200',
options: [
{ label: '预览', type: 'primary', icon: 'el-icon-view', methods: 'preview' },
{ label: '审核', type: 'primary', icon: 'el-icon-upload2', methods: 'audit' },
]
}