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雳刺、導(dǎo)入組件
import tableCmp from '@/components/table-cmp/index'
2、模板中加入導(dǎo)入組件
<table-cmp
:loading="loading"
:table-data="tableData"
:table-label="tableLabel"
:table-option="tableOption"
@handleButton="handleButton"
@handleSortChange="handleSortChange"
@handleSelectionChange="handleSelectionChange"
></table-cmp>
3、data數(shù)據(jù)
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: '審核狀態(tài)', 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: '預(yù)覽', type: 'primary', icon: 'el-icon-view', methods: 'preview' },
{ label: '審核', type: 'primary', icon: 'el-icon-upload2', methods: 'audit' },
]
}