1.组件代码
<template>
<el-table :data="tableData ? tableData : []" border :header-cell-style="{
background: '#F5F7FA',
'font-size': '12px'
}" size="mini" :max-height="height" :show-summary="showSummary" :summary-method="getSummaries"
@selection-change="handleSelectionChange" @cell-click="cellClick" @header-dragend="headerDragend"
v-loading='loading'>
<el-table-column v-if="hasSelection" type="selection" width="55" align="center" />
<el-table-column type="index" min-width="55" label="序号" align="center" fixed="left" />
<el-table-column v-if="hasOperation && operation" label="操作" align="center" fixed="left">
<template slot-scope="scope">
<el-button v-for="(button, index) in operation" v-if="!(button.hide && button.hide(scope.$index, scope.row))"
:key="index" v-btnPermission="button.permission" :type="button.type || 'text'" :size="button.size || size"
:plain="button.plain || false" class="button" @click="button.fun(scope.$index, scope.row)">
{
{
button.name }}
</el-button>
</template>
</el-table-column>
<el-table-column v-for="(item, index) of column" :key="index" :prop="item.value || item.showField"
:label="item.title || item.showName" :min-width="item.width || '100'" :index="item.id || null"
:align="item.align || 'center'" class="defined" show-overflow-tooltip>
<template slot-scope="scope">
<!-- <el-tooltip class="item" effect="light" content="Top Left 提示文字" placement="top-start"> -->
<!-- underline 表示是否有下划线 且可以点击 -->
<div :class="[{ preLine: item.preLine }, { underline: item.underline }]">
{
{
item.formatter
? item.formatter(scope.row[item.value || item.showField])
: scope.row[item.value || item.showField]
}}
</div>
<!-- </el-tooltip> -->
</template>
</el-table-column>
</el-table>
</template>
<script>
import {
mapGetters
} from 'vuex'
export default {
name: 'Table',
computed: {
...mapGetters(['pageModule', 'tableColumn'])
},
props: {
tableData: {
required: true
},
loading: {
type: Boolean,
default: false
},
column: {
type: Array,
required: true
},
operation: Array,
hasSelection: {
type: Boolean,
default: false
},
hasOperation: {
type: Boolean,
default: true
},
showSummary: {
type: Boolean,
default: false
},
getSummaries: {
type: Function,
default: function () {
}
},
cellClick: {
type: Function,
default: function () {
}
},
isDragenable: {
type: Boolean,
default: false
},
size: {
type: String,
default: 'small'
},
height: {
type: Number,
default: 450
}
},
update() {
this.$refs.table.doLayout()
},
methods: {
handleSelectionChange(val) {
this.$store.dispatch('table/multiple', val)
},
headerDragend(newWidth, oldWidth, column, event) {
if (
this.isDragenable &&
column.label !== '序号' &&
column.label !== '操作'
) {
const index = this.column.findIndex(
item => item.showField === column.property
)
const clonedColumns = this._.cloneDeep(this.column)
clonedColumns[index].width = newWidth
clonedColumns[index].preSeq = clonedColumns[index].seq
const showCol = {
module: this.pageModule,
mySingleShowColumn: clonedColumns[index]
}
this.$store.dispatch('column/updateColumn', showCol)
this.$store.dispatch('column/getColumns', this.pageModule)
} else {
return false
}
}
}
}
</script>
<style lang="scss" scoped>
.underline {
color: #409eff;
text-decoration: underline;
cursor: pointer;
}
.button {
margin: 0;
margin-top: -6px;
}
.preLine {
white-space: pre-line;
}
.el-table th.gutter {
display: table-cell !important;
}
</style>
<style lang="scss">
</style>
2.父组件里使用(这里我是全局组件 我就没写引用了)
<template>
<div class="dashboard-container">
<div class="dashboard-text">用户姓名: {
{
name }}</div>
<div class="dashboard-text" v-for="item in dept " :key="item.index">所属部门: {
{
item }}</div>
<Table :tableData='tableData' :column='column' :handleEidt="handleEidt" :operation='operation' />
</div>
</template>
<script>
import {
mapGetters
} from 'vuex'
export default {
name: 'Dashboard',
computed: {
...mapGetters([
'name',
'dept'
])
},
data() {
return {
operation: [{
name: '修改',
fun: this.onClick
}, {
name: '删除',
fun: this.onDelete
}],
tableData: [{
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄',
phone: 1235612
},
{
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄',
phone: 58456654
},
],
column: [{
value: 'operation',
title: '操作',
width: '60'
}, {
value: 'name',
title: '名称',
width: '150'
}, {
value: 'address',
title: '地址',
}, {
value: 'phone',
title: '电话',
width: '120'
}]
}
},
methods: {
handleEidt(index, row) {
console.log('index, row')
},
onClick(index, row) {
console.log(index, row)
},
onDelete(index, row) {
console.log(index, row)
}
}
}
3.实现效果