修改render渲染项的描述方式
This commit is contained in:
parent
3721a658f8
commit
4e36b54bbc
@ -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>
|
||||||
|
|||||||
@ -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>
|
||||||
|
|||||||
@ -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',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user