Files
admin-govern/src/components/table/fieldRender/index.vue

268 lines
8.0 KiB
Vue
Raw Normal View History

2023-12-22 16:19:33 +08:00
<template>
<!-- Icon -->
<Icon class="ba-icon-dark" v-if="field.render == 'icon'" :name="fieldValue ? fieldValue : field.default ?? ''" />
<!-- switch -->
<el-switch
v-if="field.render == 'switch'"
@change="onChangeField"
:model-value="fieldValue.toString()"
:loading="row.loading"
active-value="1"
inactive-value="0"
/>
<!-- image -->
<div v-if="field.render == 'image' && fieldValue" class="ba-render-image">
<el-image
:hide-on-click-modal="true"
:preview-teleported="true"
:preview-src-list="[fullUrl(fieldValue)]"
:src="fullUrl(fieldValue)"
></el-image>
</div>
<!-- tag -->
<div v-if="field.render == 'tag' && fieldValue !== ''">
2024-01-30 15:17:30 +08:00
<el-tag :type="getTagType(fieldValue, field.custom)" size="small">
2023-12-22 16:19:33 +08:00
{{ field.replaceValue ? field.replaceValue[fieldValue] : fieldValue }}
</el-tag>
</div>
<!-- datetime -->
<div v-if="field.render == 'datetime'">
2023-12-29 15:45:07 +08:00
{{ !fieldValue ? '/' : timeFormat(fieldValue, field.timeFormat ?? undefined) }}
2023-12-22 16:19:33 +08:00
</div>
<!-- color -->
<div v-if="field.render == 'color'">
<div :style="{ background: fieldValue }" class="ba-render-color"></div>
</div>
<!-- customTemplate 自定义模板 -->
2023-12-28 14:06:57 +08:00
<div
v-if="field.render == 'customTemplate'"
v-html="field.customTemplate ? field.customTemplate(row, field, fieldValue, column, index) : ''"
></div>
2023-12-22 16:19:33 +08:00
<!-- 自定义组件/函数渲染 -->
<component
v-if="field.render == 'customRender'"
:is="field.customRender"
:renderRow="row"
:renderField="field"
:renderValue="fieldValue"
:renderColumn="column"
:renderIndex="index"
/>
<!-- 按钮组 -->
<!-- 只对默认的编辑删除排序按钮进行鉴权其他按钮请通过 display 属性控制按钮是否显示 -->
2024-01-30 14:11:29 +08:00
<div v-if="field.render == 'buttons' && field.buttons" class="cn-render-buttons">
2023-12-22 16:19:33 +08:00
<template v-for="(btn, idx) in field.buttons" :key="idx">
2024-01-30 14:11:29 +08:00
<template v-if="!(btn.disabled && btn.disabled(row, field))">
<!-- 常规按钮 -->
2023-12-22 16:19:33 +08:00
<el-button
2024-01-30 14:11:29 +08:00
link
v-if="btn.render == 'basicButton'"
2023-12-22 16:19:33 +08:00
@click="onButtonClick(btn)"
:class="btn.class"
class="table-operate"
:type="btn.type"
2024-01-30 14:11:29 +08:00
:loading="props.row.loading || false"
2023-12-22 16:19:33 +08:00
v-bind="btn.attr"
>
2024-01-30 14:11:29 +08:00
<div v-if="btn.text || btn.title" class="table-operate-text">{{ btn.text || btn.title }}</div>
2023-12-22 16:19:33 +08:00
</el-button>
2024-01-30 14:11:29 +08:00
<!-- 带提示信息的按钮 -->
<el-tooltip
v-if="btn.render == 'tipButton'"
:disabled="btn.title && !btn.disabledTip ? false : true"
:content="btn.title"
placement="top"
>
<el-button
link
@click="onButtonClick(btn)"
:class="btn.class"
class="table-operate"
:type="btn.type"
v-bind="btn.attr"
>
<div v-if="btn.text || btn.title" class="table-operate-text">{{ btn.text || btn.title }}</div>
</el-button>
</el-tooltip>
<!-- 带确认框的按钮 -->
<el-popconfirm
v-if="btn.render == 'confirmButton'"
2023-12-22 16:19:33 +08:00
:disabled="btn.disabled && btn.disabled(row, field)"
2024-01-30 14:11:29 +08:00
v-bind="btn.popconfirm"
@confirm="onButtonClick(btn)"
2023-12-22 16:19:33 +08:00
>
2024-01-30 14:11:29 +08:00
<template #reference>
<div style="display: inline-block">
<el-tooltip :disabled="btn.title ? false : true" :content="btn.title" placement="top">
<el-button
link
:class="btn.class"
class="table-operate"
:type="btn.type"
v-bind="btn.attr"
>
<div v-if="btn.text || btn.title" class="table-operate-text">
{{ btn.text || btn.title }}
</div>
</el-button>
</el-tooltip>
</div>
</template>
</el-popconfirm>
</template>
2023-12-22 16:19:33 +08:00
</template>
</div>
</template>
<script setup lang="ts">
import { ref, inject } from 'vue'
2023-12-28 11:27:03 +08:00
import type { TagProps } from 'element-plus'
2023-12-22 16:19:33 +08:00
import type TableStoreClass from '@/utils/tableStore'
import { fullUrl, timeFormat } from '@/utils/common'
2023-12-28 14:06:57 +08:00
import type { VxeColumnProps } from 'vxe-table'
2023-12-22 16:19:33 +08:00
const TableStore = inject('tableStore') as TableStoreClass
interface Props {
row: TableRow
field: TableColumn
2023-12-28 11:27:03 +08:00
column: VxeColumnProps
2023-12-22 16:19:33 +08:00
index: number
}
const props = defineProps<Props>()
// 字段值(单元格值)
2023-12-28 11:27:03 +08:00
const fieldName = ref(props.field.field)
2023-12-22 16:19:33 +08:00
const fieldValue = ref(fieldName.value ? props.row[fieldName.value] : '')
if (fieldName.value && fieldName.value.indexOf('.') > -1) {
let fieldNameArr = fieldName.value.split('.')
let val: any = ref(props.row[fieldNameArr[0]])
for (let index = 1; index < fieldNameArr.length; index++) {
val.value = val.value ? val.value[fieldNameArr[index]] ?? '' : ''
}
fieldValue.value = val.value
}
if (props.field.renderFormatter && typeof props.field.renderFormatter == 'function') {
fieldValue.value = props.field.renderFormatter(props.row, props.field, fieldValue.value, props.column, props.index)
}
const onChangeField = (value: any) => {
TableStore.onTableAction('field-change', { value: value, ...props })
}
const onButtonClick = (btn: OptButton) => {
btn.click && btn.click(props.row, props.field)
}
const getTagType = (value: string, custom: any): TagProps['type'] => {
return custom && custom[value] ? custom[value] : ''
}
</script>
<style scoped lang="scss">
.m-10 {
margin: 4px;
}
.ba-render-image {
text-align: center;
}
.images-item {
width: 50px;
margin: 0 5px;
}
.el-image {
height: 36px;
width: 36px;
}
.table-operate-text {
padding-left: 5px;
2024-01-15 10:36:24 +08:00
font-size: 12px;
2023-12-22 16:19:33 +08:00
}
.table-operate {
padding: 4px 5px;
height: auto;
}
2024-01-30 15:17:30 +08:00
.cn-render-buttons {
.icon {
font-size: 12px !important;
// color: var(--ba-bg-color-overlay) !important;
}
.el-button--primary {
.icon {
color: var(--el-color-primary) !important;
&:hover {
color: var(--el-color-primary-light-3) !important;
}
}
}
.el-button--success {
.icon {
color: var(--el-color-success) !important;
&:hover {
color: var(--el-color-success-light-3) !important;
}
}
}
.el-button--warning {
.icon {
color: var(--el-color-warning) !important;
&:hover {
color: var(--el-color-warning-light-3) !important;
}
}
}
.el-button--danger {
.icon {
color: var(--el-color-danger) !important;
&:hover {
color: var(--el-color-danger-light-3) !important;
}
}
}
.el-button--info {
.icon {
color: var(--el-color-info) !important;
&:hover {
color: var(--el-color-info-light-3) !important;
}
}
}
2023-12-22 16:19:33 +08:00
}
2024-01-30 15:17:30 +08:00
2023-12-22 16:19:33 +08:00
.move-button {
cursor: move;
}
.ml-6 {
display: inline-flex;
vertical-align: middle;
margin-left: 6px;
}
.ml-6 + .el-button {
margin-left: 6px;
}
.ba-render-color {
height: 25px;
width: 100%;
}
2024-01-30 14:11:29 +08:00
.cn-render-buttons {
:deep(.el-button) {
margin-left: 0;
}
}
2023-12-22 16:19:33 +08:00
</style>