Commit 9f9b39bb authored by Cai Wei's avatar Cai Wei

feat(*): 修改表格样式

parent 65e7e260
<template>
<div class="common-table">
<el-table :data="currentPageData" v-bind="$attrs" style="width: 100%">
<el-table
ref="tableRef"
:data="currentPageData"
v-bind="$attrs"
border
:height="tableHeight"
>
<template v-for="column in columns" :key="column.prop">
<el-table-column v-bind="column">
<template #default="scope">
......@@ -23,7 +29,7 @@
v-model:page-size="pageSize"
:page-sizes="pageSizes"
:total="total"
layout="total, prev, pager, next, sizes, jumper"
layout="total, prev, pager, next, sizes, jumper"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:total-text="paginationTexts.total || '总计'"
......@@ -36,7 +42,7 @@
</template>
<script setup>
import { ref, computed, watch } from "vue";
import { ref, computed, watch, onMounted, onUnmounted, nextTick } from "vue";
const props = defineProps({
// 表格数据
......@@ -66,22 +72,20 @@ const props = defineProps({
type: Number,
default: 10,
},
// 分页布局
paginationLayout: {
type: String,
default: "total, sizes, prev, pager, next, jumper",
},
// 分页器文字配置
paginationTexts: {
type: Object,
default: () => ({
total: "总计", // 总计xxx条
sizeChange: "条/页", // 每页显示数量的文字
prev: "上一页",
next: "下一页",
jumper: "前往", // 跳转文字
total: "总计",
sizeChange: "条/页",
jumper: "前往",
}),
},
// 表格最小高度
minHeight: {
type: Number,
default: 300,
},
});
const emit = defineEmits([
......@@ -90,6 +94,45 @@ const emit = defineEmits([
"pagination-change",
]);
// 表格引用
const tableRef = ref(null);
// 表格容器引用
const tableHeight = ref(props.minHeight);
// 计算表格高度
const calculateTableHeight = () => {
nextTick(() => {
if (!tableRef.value) return;
const parentHeight = tableRef.value.$el.parentElement.offsetHeight;
const paginationHeight = props.showPagination ? 70 : 0; // 分页器高度 + margin
const calculatedHeight = parentHeight - paginationHeight;
tableHeight.value = Math.max(calculatedHeight, props.minHeight);
});
};
// 监听窗口大小变化
const handleResize = () => {
calculateTableHeight();
};
onMounted(() => {
calculateTableHeight();
window.addEventListener("resize", handleResize);
});
onUnmounted(() => {
window.removeEventListener("resize", handleResize);
});
// 监听数据变化重新计算高度
watch(
() => props.data,
() => {
nextTick(calculateTableHeight);
},
{ deep: true }
);
// 分页相关
const currentPage = ref(1);
const pageSize = ref(props.defaultPageSize);
......@@ -108,6 +151,7 @@ const handleSizeChange = (val) => {
pageSize.value = val;
currentPage.value = 1;
emitPaginationChange();
nextTick(calculateTableHeight);
};
const handleCurrentChange = (val) => {
......@@ -138,11 +182,21 @@ watch(
<style lang="scss" scoped>
.common-table {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
:deep(.el-table) {
flex: 1;
}
.pagination-container {
margin-top: 20px;
display: flex;
justify-content: flex-end;
padding: 10px 0;
height: 32px; // 固定分页器高度
}
}
</style>
......@@ -152,4 +206,13 @@ watch(
.el-table__body {
width: 100% !important;
}
.el-table {
// 设置表头样式
th.el-table__cell {
background-color: #f5f7fa !important;
color: #606266;
font-weight: 600;
}
}
</style>
......@@ -66,48 +66,49 @@
</el-form-item>
</el-form>
</div>
<div class="table-box">
<common-table
:data="tableData"
:columns="tableColumns"
:default-page-size="10"
@pagination-change="handlePaginationChange"
:pagination-texts="{
total: '共',
sizeChange: '条/页',
prev: '前一页',
next: '后一页',
jumper: '跳至',
}"
>
<template #status="{ row }">
<el-tag :type="getStatusType(row.status)" effect="light">
{{ row.status }}
</el-tag>
</template>
<common-table
:data="tableData"
:columns="tableColumns"
:default-page-size="10"
@pagination-change="handlePaginationChange"
:pagination-texts="{
total: '共',
sizeChange: '条/页',
prev: '前一页',
next: '后一页',
jumper: '跳至',
}"
>
<template #status="{ row }">
<el-tag :type="getStatusType(row.status)" effect="light">
{{ row.status }}
</el-tag>
</template>
<template #healthScore="{ row }">
<span :style="{ color: getHealthScoreColor(row.healthScore) }">
{{ row.healthScore }}
</span>
</template>
<template #healthScore="{ row }">
<span :style="{ color: getHealthScoreColor(row.healthScore) }">
{{ row.healthScore }}
</span>
</template>
<template #operation="{ row }">
<el-button type="primary" link @click="handleView(row)">
查看详情
</el-button>
<el-button type="primary" link @click="handleEdit(row)">
编辑
</el-button>
</template>
</common-table>
<template #operation="{ row }">
<el-button type="primary" link @click="handleView(row)">
查看详情
</el-button>
<el-button type="primary" link @click="handleEdit(row)">
编辑
</el-button>
</template>
</common-table>
</div>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
import CommonTable from "@/components/CommonTable/index.vue";
import CommonTable from "@/components/commonTable/index.vue";
const formInline = ref({
deviceName: "",
......@@ -329,6 +330,10 @@ onBeforeUnmount(() => {});
align-items: center;
margin-bottom: 16px;
}
.table-box {
width: 100%;
height: calc(100vh - 400px);
}
}
.default-btn {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment