表格自定义

This commit is contained in:
Gary Fu
2024-01-01 11:37:16 +08:00
parent a73e998498
commit cd12a30764
5 changed files with 162 additions and 6 deletions

View File

@@ -7,6 +7,9 @@
* @property {string} property 数据属性 * @property {string} property 数据属性
* @property {string} width 宽度 * @property {string} width 宽度
* @property {boolean} isOperation 是否是操作列 * @property {boolean} isOperation 是否是操作列
* @property {string} slot 自定义插槽
* @property [ButtonProps] buttons 自定义按钮
* @property {Object} attrs 其他属性
*/ */
/** /**
* 配置信息 * 配置信息
@@ -18,16 +21,61 @@ const props = defineProps({
column: { column: {
type: Object, type: Object,
required: true required: true
},
buttonSize: {
type: String,
default: 'small'
} }
}) })
</script> </script>
<template> <template>
<el-table-column <el-table-column
:label="column.label" v-if="!column.isOperation"
:label="column.label || $t(column.labelKey)"
:property="column.property" :property="column.property"
:width="column.width" :width="column.width"
/> v-bind="column.attrs"
>
<template
v-if="column.slot"
#default="scope"
>
<slot
v-bind="scope"
name="default"
/>
</template>
</el-table-column>
<el-table-column
v-if="column.isOperation"
:label="column.label || $t(column.labelKey)"
:width="column.width"
v-bind="column.attrs"
>
<template
#default="scope"
>
<el-button
v-for="(button, index) in column.buttons"
:key="index"
:type="button.type"
:icon="button.icon"
:size="button.size||buttonSize"
:disabled="button.disabled"
:round="button.round"
:circle="button.circle"
@click="button.click&&button.click(scope.row, scope)"
>
{{ button.label || $t(button.labelKey) }}
</el-button>
<slot
name="default"
v-bind="scope"
/>
</template>
</el-table-column>
</template> </template>
<style scoped> <style scoped>

View File

@@ -1,5 +1,6 @@
<script setup> <script setup>
import CommonTableColumn from '@/components/common-table/common-table-column.vue' import CommonTableColumn from '@/components/common-table/common-table-column.vue'
import { computed } from 'vue'
/** /**
* @typedef {TableProps} CommonTableProps * @typedef {TableProps} CommonTableProps
@@ -32,8 +33,54 @@ const props = defineProps({
stripe: { stripe: {
type: Boolean, type: Boolean,
default: true default: true
},
border: {
type: Boolean,
default: true
},
/**
* el-button
* @type [ButtonProps]
*/
buttons: {
type: Array,
default () {
return []
}
},
buttonsSlot: {
type: String,
default: ''
},
buttonSize: {
type: String,
default: 'small'
},
pageInfo: {
type: Object,
default: null
},
pageAttrs: {
type: Object,
default () {
return {}
}
} }
}) })
const calcColumns = computed(() => {
let _columns = props.columns
if (props.buttons.length || props.buttonsSlot) {
const buttonColumn = {
labelKey: 'common.label.operation',
isOperation: true,
slot: props.buttonsSlot,
buttons: props.buttons
}
_columns = [..._columns, buttonColumn]
}
return _columns
})
</script> </script>
<template> <template>
@@ -42,12 +89,26 @@ const props = defineProps({
:highlight-current-row="highlightCurrentRow" :highlight-current-row="highlightCurrentRow"
:stripe="stripe" :stripe="stripe"
:data="data" :data="data"
:border="border"
> >
<common-table-column <common-table-column
v-for="(column, index) in columns" v-for="(column, index) in calcColumns"
:key="index" :key="index"
:column="column" :column="column"
/> :button-size="buttonSize"
>
<!--用于自定义显示属性-->
<template
#default="scope"
>
<slot
:scope="scope"
:item="scope.row"
:name="column.slot"
/>
</template>
</common-table-column>
<el-pagination v-if="pageInfo" />
</el-table> </el-table>
</template> </template>

View File

@@ -23,6 +23,13 @@ common.label.about = '关于'
common.label.logout = '退出' common.label.logout = '退出'
common.label.submit = '提交' common.label.submit = '提交'
common.label.reset = '重置' common.label.reset = '重置'
common.label.operation = '操作'
common.label.new = '新增'
common.label.edit = '编辑'
common.label.modify = '修改'
common.label.delete = '删除'
common.label.search = '搜索'
common.label.find = '查找'
//* =======================msg=====================// //* =======================msg=====================//
common.msg.nonNull = '{0}不能为空' common.msg.nonNull = '{0}不能为空'

View File

@@ -23,6 +23,13 @@ common.label.about = 'About'
common.label.logout = 'Logout' common.label.logout = 'Logout'
common.label.submit = 'Submit' common.label.submit = 'Submit'
common.label.reset = 'Reset' common.label.reset = 'Reset'
common.label.operation = 'Operations'
common.label.new = 'New'
common.label.edit = 'Edit'
common.label.modify = 'Modify'
common.label.delete = 'Delete'
common.label.search = 'Search'
common.label.find = 'Find'
//* =======================msg=====================// //* =======================msg=====================//
common.msg.nonNull = '{0} is required.' common.msg.nonNull = '{0} is required.'

View File

@@ -1,4 +1,6 @@
<script setup> <script setup>
import { ref } from 'vue'
const tableData = [ const tableData = [
{ {
birthday: '2016-05-03', birthday: '2016-05-03',
@@ -34,7 +36,8 @@ const columns = [{
property: 'userName' property: 'userName'
}, { }, {
label: '性别', label: '性别',
property: 'gender' property: 'gender',
slot: 'gender'
}, { }, {
label: '出生日期', label: '出生日期',
property: 'birthday' property: 'birthday'
@@ -42,13 +45,43 @@ const columns = [{
label: '地址', label: '地址',
property: 'address' property: 'address'
}] }]
const buttons = ref([{
labelKey: 'common.label.edit',
type: 'primary',
click: item => {
console.info('编辑=============', item)
}
}, {
labelKey: 'common.label.delete',
type: 'danger',
click: item => {
console.info('删除=============', item)
}
}, {
label: '其他操作'
}])
</script> </script>
<template> <template>
<common-table <common-table
:data="tableData" :data="tableData"
:columns="columns" :columns="columns"
/> :buttons="buttons"
buttons-slot="buttons"
>
<template #gender="{item}">
<el-tag
:type="item.gender === 'male' ? '' : 'success'"
>
{{ item.gender }}
</el-tag>
</template>
<template #buttons="{item}">
<el-button @click="console.info(item)">
测试
</el-button>
</template>
</common-table>
</template> </template>
<style scoped> <style scoped>