mt-yd-ui/src/components/base-table/index.vue

97 lines
2.3 KiB
Vue
Raw Normal View History

2022-08-05 16:03:38 +08:00
<template>
<div class="base-table">
2022-08-23 11:11:51 +08:00
<el-table
:data="data"
style="width: 100%"
fit
border
:header-cell-style="{ background: '#FAFAFA', color: '#606266', height: '40px' }"
:max-height="maxHeight"
:span-method="spanMethod || null"
>
2022-08-05 16:03:38 +08:00
<!-- 表格头定义 -->
2022-08-08 10:07:15 +08:00
<template v-for="(head, idx) in tableHeadConfigs">
<!-- 带type的表头 -->
<el-table-column
:key="idx"
v-if="head.type"
:type="head.type"
:label="head.label || head.name || ''"
:header-align="head.align || 'center'"
:align="head.align || 'center'"
:width="head.width || 50"
v-bind="head.more"
></el-table-column>
<!-- 普通的表头 -->
<el-table-column
v-else
2022-08-22 15:16:22 +08:00
:key="idx + 'else'"
2022-08-08 10:07:15 +08:00
:label="head.label ? head.label : head.name"
2022-08-22 15:16:22 +08:00
:prop="head.prop || null"
:width="head.width || null"
:min-width="head.minWidth || null"
:fixed="head.fixed || null"
2022-08-08 10:07:15 +08:00
:show-overflow-tooltip="head.showOverflowTooltip || false"
filter-placement="top"
2022-08-22 15:16:22 +08:00
:align="head.align || null"
2022-08-08 10:07:15 +08:00
v-bind="head.more"
>
<!-- 子组件 -->
<template v-if="head.prop" slot-scope="scope">
2022-08-10 10:47:12 +08:00
<component v-if="head.subcomponent" :is="head.subcomponent" :key="idx + 'sub'" :inject-data="{ ...scope.row, head }" @emit-data="handleSubEmitData" />
2022-08-08 10:07:15 +08:00
<!-- 直接展示数据或应用过滤器 -->
<span v-else>{{ scope.row[head.prop] | commonFilter(head.filter) }}</span>
</template>
2022-08-22 15:16:22 +08:00
<!-- 多级表头 -->
<template v-if="!head.prop && head.children">
<TableHead v-for="(subhead, subindex) in head.children" :key="'subhead-' + idx + '-subindex-' + subindex" :opt="subhead" />
</template>
2022-08-08 10:07:15 +08:00
</el-table-column>
</template>
2022-08-05 16:03:38 +08:00
</el-table>
</div>
</template>
<script>
2022-08-22 15:16:22 +08:00
import TableHead from './components/table-head.vue'
2022-08-05 16:03:38 +08:00
export default {
name: 'BaseTable',
props: {
2022-08-08 10:07:15 +08:00
tableHeadConfigs: {
type: Array,
default: () => []
},
2022-08-05 16:03:38 +08:00
data: {
type: Array,
default: () => []
},
2022-08-08 10:07:15 +08:00
maxHeight: {
type: Number,
default: 500
2022-08-23 11:11:51 +08:00
},
spanMethod: {
type: Function,
default: () => {
() => [0, 0]
},
required: false
2022-08-08 10:07:15 +08:00
}
2022-08-05 16:03:38 +08:00
},
filters: {
commonFilter: (source, filterType = a => a) => {
return filterType(source)
}
},
2022-08-08 10:07:15 +08:00
data() {
return {}
},
2022-08-05 16:03:38 +08:00
methods: {
handleSubEmitData(payload) {
this.$emit('operate-event', payload)
}
2022-08-22 15:16:22 +08:00
},
components: { TableHead }
2022-08-05 16:03:38 +08:00
}
</script>