update BaseTable

This commit is contained in:
g7hoo 2022-08-22 15:16:22 +08:00
parent 69dda72fab
commit 9a22601f5b
3 changed files with 95 additions and 7 deletions

View File

@ -0,0 +1,40 @@
<template>
<el-table-column
:label="opt.label ? opt.label : opt.name"
:prop="opt.prop || null"
:width="opt.width || null"
:min-width="opt.minWidth || null"
:fixed="opt.fixed || null"
:show-overflow-tooltip="opt.showOverflowTooltip || false"
filter-placement="top"
:align="opt.align || null"
v-bind="opt.more"
>
<template v-if="opt.prop" slot-scope="scope">
<component v-if="opt.subcomponent" :is="opt.subcomponent" :key="idx + 'sub'" :inject-data="{ ...scope.row, head: opt }" @emit-data="handleSubEmitData" />
<!-- 直接展示数据或应用过滤器 -->
<span v-else>{{ scope.row[opt.prop] | commonFilter(opt.filter) }}</span>
</template>
<!-- 递归 -->
<template v-if="!opt.prop && opt.children">
<TableHead v-for="(subhead, index) in opt.children" :key="'subhead' + index" :opt="subhead" />
</template>
</el-table-column>
</template>
<script>
export default {
name: 'TableHead',
filters: {
commonFilter: (source, filterType = a => a) => {
return filterType(source)
}
},
props: {
opt: {
type: Object,
default: () => ({})
}
}
}
</script>

View File

@ -17,15 +17,15 @@
<!-- 普通的表头 --> <!-- 普通的表头 -->
<el-table-column <el-table-column
v-else v-else
:key="idx+'else'" :key="idx + 'else'"
:label="head.label ? head.label : head.name" :label="head.label ? head.label : head.name"
:prop="head.prop" :prop="head.prop || null"
:width="head.width" :width="head.width || null"
:min-width="head.minWidth" :min-width="head.minWidth || null"
:fixed="head.fixed" :fixed="head.fixed || null"
:show-overflow-tooltip="head.showOverflowTooltip || false" :show-overflow-tooltip="head.showOverflowTooltip || false"
filter-placement="top" filter-placement="top"
:align="head.align" :align="head.align || null"
v-bind="head.more" v-bind="head.more"
> >
<!-- 子组件 --> <!-- 子组件 -->
@ -34,6 +34,11 @@
<!-- 直接展示数据或应用过滤器 --> <!-- 直接展示数据或应用过滤器 -->
<span v-else>{{ scope.row[head.prop] | commonFilter(head.filter) }}</span> <span v-else>{{ scope.row[head.prop] | commonFilter(head.filter) }}</span>
</template> </template>
<!-- 多级表头 -->
<template v-if="!head.prop && head.children">
<TableHead v-for="(subhead, subindex) in head.children" :key="'subhead-' + idx + '-subindex-' + subindex" :opt="subhead" />
</template>
</el-table-column> </el-table-column>
</template> </template>
</el-table> </el-table>
@ -41,6 +46,7 @@
</template> </template>
<script> <script>
import TableHead from './components/table-head.vue'
export default { export default {
name: 'BaseTable', name: 'BaseTable',
props: { props: {
@ -69,6 +75,7 @@ export default {
handleSubEmitData(payload) { handleSubEmitData(payload) {
this.$emit('operate-event', payload) this.$emit('operate-event', payload)
} }
} },
components: { TableHead }
} }
</script> </script>

View File

@ -0,0 +1,41 @@
<template>
<div style="background: #ccc; width: 800px; height: 800px;">
<base-table :table-head-configs="configs" :data="dataList" />
</div>
</template>
<script>
import BaseTable from '@/components/base-table'
export default {
name: 'Test',
components: { BaseTable },
data() {
return {
configs: [
{ prop: 'createTime', name: '创建日期' },
{ prop: 'name', name: '名称' },
{
label: '地址',
children: [
{ prop: 'province', name: '省' },
{
// prop: 'city',
name: '市',
children: [
{ prop: 'county', name: '县' },
{ prop: 'downtown', name: '镇' }
]
}
]
},
{ prop: 'status', name: '状态', filter: val => ['激活', '注销'][val] }
],
dataList: [
{ createTime: '2022-01-01', name: '奥特曼', province: '北京', city: '昌平', county: '怀宁', downtown: '石牌', status: 0 },
{ createTime: '2022-01-02', name: '盈江', province: '上海', city: '徐家汇', status: 1 }
]
}
}
}
</script>