update base-table

This commit is contained in:
lb 2022-08-08 10:07:15 +08:00
parent 93912aa193
commit 65c4ce9d45
2 changed files with 97 additions and 42 deletions

View File

@ -1,33 +1,41 @@
<template> <template>
<div class="base-table"> <div class="base-table">
<el-table <el-table :data="data" style="width: 100%" fit border :header-cell-style="{ background: '#FAFAFA', color: '#606266', height: '40px' }" :max-height="maxHeight">
:data="data"
style="width: 100%"
fit
border
:header-cell-style="{ background: '#FAFAFA', color: '#606266', height: '40px' }"
:max-height="maxHeight"
>
<!-- 表格头定义 --> <!-- 表格头定义 -->
<el-table-column <template v-for="(head, idx) in tableHeadConfigs">
v-for="(head, idx) in tableHeadConfigs" <!-- 带type的表头 -->
:key="idx" <el-table-column
:type="head.type" :key="idx"
:index="head.index" v-if="head.type"
:label="head.label?head.label:head.name" :type="head.type"
:prop="head.prop" :label="head.label || head.name || ''"
:width="head.width" :header-align="head.align || 'center'"
:min-width="head.minWidth" :align="head.align || 'center'"
:fixed="head.fixed" :width="head.width || 50"
:show-overflow-tooltip="true" v-bind="head.more"
filter-placement="top" ></el-table-column>
:align="head.align" <!-- 普通的表头 -->
> <el-table-column
<template v-if="head.prop" slot-scope="scope"> v-else
<component v-if="head.subcomponent" :is="head.subcomponent" :key="idx + 'sub'" :inject-data="{ ...scope.row, head }" @emitData="handleSubEmitData" /> :key="idx"
<span v-else>{{ scope.row[head.prop] | commonFilter(head.filter) }}</span> :label="head.label ? head.label : head.name"
</template> :prop="head.prop"
</el-table-column> :width="head.width"
:min-width="head.minWidth"
:fixed="head.fixed"
:show-overflow-tooltip="head.showOverflowTooltip || false"
filter-placement="top"
:align="head.align"
v-bind="head.more"
>
<!-- 子组件 -->
<template v-if="head.prop" slot-scope="scope">
<component v-if="head.subcomponent" :is="head.subcomponent" :key="idx + 'sub'" :inject-data="{ ...scope.row, head }" @emitData="handleSubEmitData" />
<!-- 直接展示数据或应用过滤器 -->
<span v-else>{{ scope.row[head.prop] | commonFilter(head.filter) }}</span>
</template>
</el-table-column>
</template>
</el-table> </el-table>
</div> </div>
</template> </template>
@ -36,27 +44,27 @@
export default { export default {
name: 'BaseTable', name: 'BaseTable',
props: { props: {
tableHeadConfigs: { tableHeadConfigs: {
type: Array, type: Array,
default: () => [] default: () => []
}, },
data: { data: {
type: Array, type: Array,
default: () => [] default: () => []
}, },
maxHeight: { maxHeight: {
type: Number, type: Number,
default: 500 default: 500
} }
}, },
filters: { filters: {
commonFilter: (source, filterType = a => a) => { commonFilter: (source, filterType = a => a) => {
return filterType(source) return filterType(source)
} }
}, },
data () { data() {
return {} return {}
}, },
methods: { methods: {
handleSubEmitData(payload) { handleSubEmitData(payload) {
this.$emit('operate-event', payload) this.$emit('operate-event', payload)

View File

@ -12,6 +12,7 @@
</el-form> </el-form>
<el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;"> <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
<el-table-column type="index" header-align="center" align="center" label="序号"></el-table-column>
<el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column> <el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column>
<el-table-column prop="code" header-align="center" align="center" label="编码"></el-table-column> <el-table-column prop="code" header-align="center" align="center" label="编码"></el-table-column>
<el-table-column prop="name" header-align="center" align="center" label="设备名称"></el-table-column> <el-table-column prop="name" header-align="center" align="center" label="设备名称"></el-table-column>
@ -72,9 +73,54 @@
import BaseTable from '@/components/base-table' import BaseTable from '@/components/base-table'
import AddOrUpdate from './equipment-add-or-update' import AddOrUpdate from './equipment-add-or-update'
const TableOperateComponent = {
name: 'TableOperations',
props: {
injectData: {
type: Object,
default: () => ({})
}
},
data() {
return {
btnTypes: {
add: 'primary',
delete: 'danger',
detail: 'info'
// add more...
},
colors: {
// add more...
},
text: {
// for i18n inject:
'add': '添加'
// add more...
}
}
},
methods: {
//
},
render: function(h) {
let btns = []
for (const optionStr of this.injectData.head?.options) {
btns.push(h('el-button', { props: { type: this.btnTypes[optionStr] } }, optionStr))
}
return h('span', null, btns)
}
}
const tableConfigs = [ const tableConfigs = [
{ type: 'selection' }, {
{ type: 'index', name: 'No' }, type: 'index',
name: '序号',
more: {
index: function(index) {
return (index + 1) * 2
}
}
},
{ prop: 'id', name: 'id' }, { prop: 'id', name: 'id' },
{ prop: 'code', name: '编码' }, { prop: 'code', name: '编码' },
{ prop: 'name', name: '设备名称' }, { prop: 'name', name: '设备名称' },
@ -95,12 +141,13 @@ const tableConfigs = [
{ prop: 'valid', name: '删除标志,是否有效:1 可用 0不可用' }, { prop: 'valid', name: '删除标志,是否有效:1 可用 0不可用' },
{ prop: 'creatorId', name: '创建人' }, { prop: 'creatorId', name: '创建人' },
{ prop: 'creatorName', name: '创建人姓名' }, { prop: 'creatorName', name: '创建人姓名' },
{ prop: 'createTime', name: '创建时间' }, { prop: 'createTime', name: '添加时间' },
{ prop: 'updaterId', name: '更新人' }, { prop: 'updaterId', name: '更新人' },
{ prop: 'updaterName', name: '更新人姓名' }, { prop: 'updaterName', name: '更新人姓名' },
{ prop: 'updateTime', name: '更新时间' }, { prop: 'updateTime', name: '更新时间' },
{ prop: 'version', name: '版本号' }, { prop: 'version', name: '版本号' },
{ prop: 'externalCode', name: '外部系统代码' } { prop: 'externalCode', name: '外部系统代码' },
{ prop: 'operations', name: '操作', fixed: 'right', width: 180, subcomponent: TableOperateComponent, options: ['add', 'detail', 'delete'] }
] ]
export default { export default {