修改render渲染项的描述方式

This commit is contained in:
huangchenhao 2025-10-30 17:08:19 +08:00
parent 3721a658f8
commit 4e36b54bbc
3 changed files with 84 additions and 64 deletions

View File

@ -29,14 +29,17 @@
> >
<!-- 自定义渲染render slot --> <!-- 自定义渲染render slot -->
<template v-if="col.render || col.slot" #default="scope"> <template v-if="col.render || col.slot" #default="scope">
<component v-if="col.render" :is="col.render(scope.row, scope.column, scope.$index)" /> <component
v-if="col.render"
:is="col.render(scope.row, scope.column, scope.$index)"
/>
<slot v-else-if="col.slot" :name="col.slot" v-bind="scope" /> <slot v-else-if="col.slot" :name="col.slot" v-bind="scope" />
</template> </template>
</el-table-column> </el-table-column>
<!-- 透传所有插槽 --> <!-- 透传所有插槽 -->
<template v-for="(_, name) in $slots" #[name]="slotProps"> <template v-for="name in Object.keys($slots)" #[name]="scope">
<slot :name="name" v-bind="slotProps || {}" /> <slot :name="name" v-bind="typeof scope === 'object' ? scope : {}" />
</template> </template>
</el-table> </el-table>
@ -55,14 +58,14 @@
</template> </template>
<script setup> <script setup>
import { ref, computed } from 'vue' import { ref, computed } from "vue";
import TableToolbar from './TableToolbar.vue' import TableToolbar from "./TableToolbar.vue";
import { useAutoHeight } from './useAutoHeight' import { useAutoHeight } from "./useAutoHeight";
defineOptions({ defineOptions({
name: 'DynamicTable', name: "DynamicTable",
inheritAttrs: false // inheritAttrs: false, //
}) });
const props = defineProps({ const props = defineProps({
/** /**
@ -70,53 +73,53 @@ const props = defineProps({
*/ */
dataSource: { dataSource: {
type: Array, type: Array,
default: () => [] default: () => [],
}, },
/** /**
* 列配置 * 列配置
*/ */
columns: { columns: {
type: Array, type: Array,
default: () => [] default: () => [],
}, },
/** /**
* 工具栏配置 * 工具栏配置
*/ */
toolbar: { toolbar: {
type: Object, type: Object,
default: null default: null,
}, },
/** /**
* 是否启用自适应高度 * 是否启用自适应高度
*/ */
autoHeight: { autoHeight: {
type: Boolean, type: Boolean,
default: false default: false,
}, },
/** /**
* 分页配置false 表示不分页 * 分页配置false 表示不分页
*/ */
pagination: { pagination: {
type: [Object, Boolean], type: [Object, Boolean],
default: false default: false,
} },
}) });
const emit = defineEmits(['selection-change']) const emit = defineEmits(["selection-change"]);
const containerRef = ref(null) const containerRef = ref(null);
const tableRef = ref(null) const tableRef = ref(null);
const selectedRowKeys = ref([]) const selectedRowKeys = ref([]);
// ==================== ==================== // ==================== ====================
const { tableHeight } = useAutoHeight(containerRef, { const { tableHeight } = useAutoHeight(containerRef, {
enabled: props.autoHeight, enabled: props.autoHeight,
minHeight: 200 minHeight: 200,
}) });
const computedHeight = computed(() => { const computedHeight = computed(() => {
return props.autoHeight ? tableHeight.value : undefined return props.autoHeight ? tableHeight.value : undefined;
}) });
// ==================== ==================== // ==================== ====================
/** /**
@ -124,28 +127,28 @@ const computedHeight = computed(() => {
* 原则: 仅设置默认值,不改变结构,用户配置优先 * 原则: 仅设置默认值,不改变结构,用户配置优先
*/ */
const processedColumns = computed(() => { const processedColumns = computed(() => {
return props.columns.map(col => { return props.columns.map((col) => {
// (selection/index/expand), // (selection/index/expand),
if (col.type) return col if (col.type) return col;
// //
return { return {
showOverflowTooltip: true, // showOverflowTooltip: true, //
align: 'center', // align: "center", //
minWidth: col.width ? undefined : 100, // minWidth: col.width ? undefined : 100, //
...col // ...col, //
} };
}) });
}) });
/** /**
* 获取列的 props用于 v-bind * 获取列的 props用于 v-bind
* 过滤掉自定义属性 (render, slot) * 过滤掉自定义属性 (render, slot)
*/ */
const getColumnProps = (col) => { const getColumnProps = (col) => {
const { render, slot, ...restProps } = col const { render, slot, ...restProps } = col;
return restProps return restProps;
} };
// ==================== ==================== // ==================== ====================
/** /**
@ -153,28 +156,28 @@ const getColumnProps = (col) => {
*/ */
const handleSelectionChange = (selection) => { const handleSelectionChange = (selection) => {
// key // key
const rowKey = props.$attrs?.rowKey || props.$attrs?.['row-key'] || 'id' const rowKey = props.$attrs?.rowKey || props.$attrs?.["row-key"] || "id";
selectedRowKeys.value = selection.map(row => selectedRowKeys.value = selection.map((row) =>
typeof rowKey === 'function' ? rowKey(row) : row[rowKey] typeof rowKey === "function" ? rowKey(row) : row[rowKey]
) );
emit('selection-change', selection) emit("selection-change", selection);
} };
/** /**
* 处理分页页码改变 * 处理分页页码改变
*/ */
const handlePageChange = (page) => { const handlePageChange = (page) => {
props.pagination?.onChange?.(page, props.pagination.pageSize) props.pagination?.onChange?.(page, props.pagination.pageSize);
} };
/** /**
* 处理每页条数改变 * 处理每页条数改变
*/ */
const handleSizeChange = (size) => { const handleSizeChange = (size) => {
// , // ,
props.pagination?.onChange?.(1, size) props.pagination?.onChange?.(1, size);
} };
// ==================== ==================== // ==================== ====================
defineExpose({ defineExpose({
@ -192,11 +195,11 @@ defineExpose({
recalculate: () => { recalculate: () => {
if (props.autoHeight) { if (props.autoHeight) {
setTimeout(() => { setTimeout(() => {
tableHeight.recalculate?.() tableHeight.recalculate?.();
}, 100) }, 100);
} }
} },
}) });
</script> </script>
<style scoped> <style scoped>

View File

@ -10,8 +10,9 @@
<script lang="ts" setup> <script lang="ts" setup>
import DynamicTable from "../../component/DynamicTable"; import DynamicTable from "../../component/DynamicTable";
import { h } from "vue"; import { h, onMounted } from "vue";
import { request } from "@/utils/request"; import { request } from "@/utils/request";
const tableData = [ const tableData = [
{ {
county: "潼南", county: "潼南",
@ -114,6 +115,20 @@ const tableData = [
service_station_address: "交通公路部门", service_station_address: "交通公路部门",
}, },
]; ];
const getTableData = async () => {
try {
const params = {};
const res = await request({
url: "/api/asdsad/asdasd",
method: "GET",
params: params,
});
} catch (error) {}
};
onMounted(() => {
getTableData();
});
const columns = [ const columns = [
{ {
prop: "county", prop: "county",
@ -152,20 +167,16 @@ const columns = [
label: "操作", label: "操作",
fixed: "right", fixed: "right",
width: 100, width: 100,
render: (row) => { render: (row) => () => h(
return h(
ElButton, ElButton,
{ {
type: "text", type: "text",
on: { onClick: () => {
click: () => {
console.log(row); console.log(row);
}, },
}, },
}, () => "详情"
"详情" ),
);
},
}, },
]; ];
</script> </script>

View File

@ -24,7 +24,13 @@ export default defineConfig({
server: { server: {
port: 3000, port: 3000,
open: true, open: true,
cors: true cors: true,
proxy: {
'/api': {
target: '',
changeOrigin: true,
},
}
}, },
build: { build: {
outDir: 'dist', outDir: 'dist',