直接上代码,原理其实还是依靠循环和父子通信的原理
<template>
<el-table
ref="multipleTable"
v-loading="tableLoading"
:i="i"
:data="tableData"
v-bind="$attrs"
row-key="id"
element-loading-text="加载中"
element-loading-spinner="el-icon-loading"
:height="height"
element-loading-background="rgba(255, 255, 255, 0.8)"
tooltip-effect="dark"
style="width: 100%"
border
fit
highlight-current-row
:header-cell-style="{ background: '#f8f8f9', 'text-align': 'center' }"
:summary-method="getSummaries"
:show-summary="showsummary"
:row-class-name="rowClassName"
stripe
v-on="$listeners"
@selection-change="handleSelectionChange"
>
<!-- 复选框 -->
<template v-if="isSelection">
<el-table-column type="selection" width="40" :selectable="selectable" />
</template>
<!-- 表身 -->
<template v-for="(item, index) in column">
<el-table-column
v-if="typeof item.showHide === 'function' ? item.showHide(item) : true"
:key="index"
:class-name="'1' + i"
:i="i"
:label="item.label || item.title"
:prop="item.prop"
:type="item.type"
:width="item.width"
:fixed="item.fixed"
:sortable="item.sortable ? true : false"
:filters="item.filters"
:column-key="item.columnKey"
:filtered-value="item.filteredValue"
:filter-multiple="item.filterMultiple"
:min-width="item.minWidth"
:align="item.center || 'left'"
>
<template v-if="item.hasOwnProperty('colunmTemplate')" :slot="item.colunmTemplate" slot-scope="scope">
<slot v-if="item.theadSlot" :name="item.theadSlot" :row="scope.row" :index="index" />
</template>
<template slot-scope="scope">
<!-- 插槽 -->
<div v-if="item.dataType == 'slot'">
<slot v-if="item.slot" :name="item.slot" :row="scope.row" :index="scope.$index" :keys="item.slot" :click-fun="item.clickFun" />
</div>
<!-- 进度条 -->
<div v-else-if="item.dataType == 'progress'">
<el-progress :percentage="Number(scope.row[item.prop])" />
</div>
<!-- tag -->
<div v-else-if="item.dataType == 'tag'">
<el-tag
v-if="typeof dataTypeFn(scope.row[item.prop], item.formatData) == 'string'"
:title="scope.row[item.prop] | formatters(item.formatData)"
:type="formatType(scope.row[item.prop], item.formatType)"
>
{{ scope.row[item.prop] | formatters(item.formatData) }}
</el-tag>
<el-tag
v-for="(tag, index) in dataTypeFn(scope.row[item.prop], item.formatData)"
v-else-if="typeof dataTypeFn(scope.row[item.prop], item.formatData) == 'object'"
:key="index"
:title="scope.row[item.prop] | formatters(item.formatData)"
:type="formatType(tag, item.formatType)"
>
{{ item.tagGroup ? (tag[item.tagGroup.label] ? tag[item.tagGroup.label] : tag) : tag }}
</el-tag>
<el-tag v-else :title="scope.row[item.prop] | formatters(item.formatData)" :type="formatType(scope.row[item.prop], item.formatType)">
{{ scope.row[item.prop] | formatters(item.formatData) }}
</el-tag>
</div>
<!-- 按钮 -->
<div v-else-if="item.dataType == 'option'">
<template v-for="(o, key) in item.operation">
<el-button
v-if="typeof o.showHide === 'function' ? o.showHide(scope.row) : true"
:key="key"
:style="o.style"
:icon="o.icon | iconFn(scope.row)"
:disabled="o.disabled ? o.disabled(scope.row) : false"
:plain="o.plain"
:type="o.type | typeFn(scope.row)"
:size="o.size"
@click="o.clickFun(scope.row, scope.$index, i)"
>
{{ o.name }}
</el-button>
</template>
</div>
<!-- -->
<!-- 默认纯展示数据 -->
<div v-else>
<span v-if="!item.formatData">{{ scope.row[item.prop] }}</span>
<span v-else-if="item.prop">{{ scope.row[item.prop] | formatters(item.formatData) }}</span>
<span v-else>{{ scope.row | formatters(item.formatData) }}</span>
</div>
</template>
<!-- </div> -->
</el-table-column>
</template>
</el-table>
</template>
<script>
export default {
filters: {
iconFn(val, row) {
if (typeof val === 'function') {
return val(row)
} else return val
},
typeFn(val, row) {
if (typeof val === 'function') {
return val(row)
} else return val
},
describeConts(val, describeCont) {
if (typeof describeCont === 'function') {
return describeCont(val)
} else return val
},
formatters(val, format) {
if (typeof format === 'function') {
return format(val)
} else return val
}
},
props: {
i: {
// 用于多个表格
type: Number,
default: null
},
isSelection: {
type: Boolean,
default: false
},
height: {
type: Number,
default: null
},
tableLoading: {
type: Boolean,
default: false
},
showsummary: {
type: Boolean,
default: false
},
handleSelectionChange: {
type: Function,
default: () => {
return () => {}
}
},
selectable: {
type: Function,
default: () => {
return () => {}
}
},
headerCellStyle: {
type: Object,
default: () => {
return {}
}
},
column: {
type: Array,
default() {
return []
}
},
rowClassName: {
type: Function,
default: () => {}
},
getSummaries: {
type: Function,
default: () => {}
},
tableData: {
type: Array,
default() {
return []
}
}
},
methods: {
formatType(val, format) {
if (typeof format === 'function') {
return format(val)
} else return ''
},
dataTypeFn(val, format) {
if (typeof format === 'function') {
return format(val)
} else return val
}
}
}
</script>
<style lang="scss" scoped>
span {
white-space: pre-wrap;
}
</style>
然后使用就比较简单了,页面直接引入
<Table :table-data="tableData" :column="column" />
然后table-data就是接口返回的全部数据 column就是表头的数据