fix(kpi-grid): 卡片改用ResizeObserver动态计算列数,自适应填满容器宽度

This commit is contained in:
小蟑螂
2026-06-05 00:55:30 +08:00
parent 8d73ae17f0
commit cd5d4a0976
3 changed files with 59 additions and 16 deletions
@@ -1,7 +1,7 @@
<script lang="ts" setup>
import type { WorkbenchVO } from '#/api/ecommerce/workbench/types';
import { computed } from 'vue';
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
import { Tooltip } from 'ant-design-vue';
@@ -60,12 +60,51 @@ const showEmptyPrefsHint = computed(
() => props.kpis.length > 0 && props.visibleLabels.length === 0,
);
const MIN_CARD_WIDTH = 285;
const GRID_GAP = 12;
const gridColumnCount = computed(() =>
clampFinanceKpiColsPerRow(props.colsPerRow),
);
const gridRef = ref<HTMLElement | null>(null);
const containerWidth = ref(0);
let resizeObserver: ResizeObserver | null = null;
function updateWidth() {
if (gridRef.value) containerWidth.value = gridRef.value.clientWidth;
}
function ensureObserving() {
if (gridRef.value && !resizeObserver) {
updateWidth();
resizeObserver = new ResizeObserver(updateWidth);
resizeObserver.observe(gridRef.value);
}
}
onMounted(() => {
ensureObserving();
});
/** 数据加载后网格才可见,补上 ResizeObserver */
watch(orderedKpiRows, () => {
nextTick(() => ensureObserving());
});
onUnmounted(() => {
resizeObserver?.disconnect();
resizeObserver = null;
});
const effectiveCols = computed(() => {
const pref = gridColumnCount.value;
const maxFit = Math.max(1, Math.floor((containerWidth.value + GRID_GAP) / (MIN_CARD_WIDTH + GRID_GAP)));
return Math.min(pref, maxFit);
});
const gridStyle = computed(() => ({
gridTemplateColumns: `repeat(${gridColumnCount.value}, minmax(0, 1fr))`,
gridTemplateColumns: `repeat(${effectiveCols.value}, minmax(0, 1fr))`,
}));
function compareRangeHint(start?: string | null, end?: string | null): string {
@@ -152,6 +191,7 @@ const trendChipClass =
</div>
<div
v-else
ref="gridRef"
class="grid gap-3"
:style="gridStyle"
>
@@ -4,12 +4,11 @@ import type { FinanceKpiGroupId } from '../kpi-catalog';
import { computed, ref, watch } from 'vue';
import { createIconifyIcon } from '@vben/icons';
import { Button, Checkbox, message, Modal, Radio } from 'ant-design-vue';
import { Button, Checkbox, message, Modal, Slider } from 'ant-design-vue';
import {
clampFinanceKpiColsPerRow,
FINANCE_KPI_CATALOG,
FINANCE_KPI_COLS_PER_ROW_OPTIONS,
FINANCE_KPI_DEFAULT_COLS_PER_ROW,
FINANCE_KPI_DEFAULT_VISIBLE,
FINANCE_KPI_GROUP_ORDER,
@@ -135,24 +134,28 @@ function handleCancel() {
</template>
<p class="mb-4 text-sm text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">
勾选要在经营概览中展示的指标,并可调整每行卡片数量;设置保存在本浏览器。
勾选要在经营概览中展示的指标,并可调整最多列数;设置保存在本浏览器。
</p>
<div
class="mb-4 rounded-xl border border-slate-100 bg-slate-50/80 px-4 py-3 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(240deg_3.7%_12%)]"
>
<div class="mb-2 text-sm font-semibold text-slate-700 dark:text-[hsl(0deg_0%_90%)]">
每行显示卡片数
最多列数
</div>
<div class="flex items-center gap-4 px-1">
<Slider
v-model:value="draftColsPerRow"
:min="3"
:max="6"
:step="1"
:marks="{ 3: '3', 4: '4', 5: '5', 6: '6' }"
class="flex-1"
/>
<span class="min-w-[3ch] text-center text-sm font-bold text-slate-700 dark:text-[hsl(0deg_0%_90%)]">
{{ draftColsPerRow }} 列
</span>
</div>
<Radio.Group v-model:value="draftColsPerRow" class="flex flex-wrap gap-2">
<Radio
v-for="n in FINANCE_KPI_COLS_PER_ROW_OPTIONS"
:key="n"
:value="n"
>
{{ n }} 张
</Radio>
</Radio.Group>
</div>
<div class="mb-4 flex flex-wrap gap-2">
@@ -98,7 +98,7 @@ defineExpose({ redraw });
<span class="text-sm text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">
经营 KPI
<span v-if="visibleKpiCount > 0" class="ml-1 text-slate-400">
(已选 {{ visibleKpiCount }} 项 · 每行 {{ colsPerRow }} 张)
(已选 {{ visibleKpiCount }} 项 · 最多 {{ colsPerRow }} 列)
</span>
</span>
<Button