fix: 修复日期范围选择器时区和性能问题
- 修复时区问题:使用本地时区格式化日期,避免 toISOString() 导致的日期偏移 - 优化日期范围选择器性能:使用 untrack 避免循环更新 - 统一日期格式化方法:在 utils.ts 中添加 formatLocalDate 工具函数 - 修复分页逻辑:优化页码计算和显示 - 更新相关页面:bills 和 analysis 页面使用统一的日期格式化方法
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
import { Badge } from '$lib/components/ui/badge';
|
||||
import * as Card from '$lib/components/ui/card';
|
||||
import { DateRangePicker } from '$lib/components/ui/date-range-picker';
|
||||
import { formatLocalDate } from '$lib/utils';
|
||||
import BarChart3 from '@lucide/svelte/icons/bar-chart-3';
|
||||
import Loader2 from '@lucide/svelte/icons/loader-2';
|
||||
import AlertCircle from '@lucide/svelte/icons/alert-circle';
|
||||
@@ -41,8 +42,9 @@
|
||||
const today = new Date();
|
||||
const year = today.getFullYear();
|
||||
const month = today.getMonth();
|
||||
const startDate = new Date(year, month, 1).toISOString().split('T')[0];
|
||||
const endDate = today.toISOString().split('T')[0];
|
||||
|
||||
const startDate = formatLocalDate(new Date(year, month, 1));
|
||||
const endDate = formatLocalDate(today);
|
||||
return { startDate, endDate };
|
||||
}
|
||||
const defaultDates = getDefaultDates();
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
import { Label } from '$lib/components/ui/label';
|
||||
import * as Table from '$lib/components/ui/table';
|
||||
import { DateRangePicker } from '$lib/components/ui/date-range-picker';
|
||||
import { formatLocalDate } from '$lib/utils';
|
||||
import Loader2 from '@lucide/svelte/icons/loader-2';
|
||||
import AlertCircle from '@lucide/svelte/icons/alert-circle';
|
||||
import Search from '@lucide/svelte/icons/search';
|
||||
@@ -40,8 +41,9 @@
|
||||
const today = new Date();
|
||||
const year = today.getFullYear();
|
||||
const month = today.getMonth();
|
||||
const startDate = new Date(year, month, 1).toISOString().split('T')[0];
|
||||
const endDate = today.toISOString().split('T')[0];
|
||||
|
||||
const startDate = formatLocalDate(new Date(year, month, 1));
|
||||
const endDate = formatLocalDate(today);
|
||||
return { startDate, endDate };
|
||||
}
|
||||
const defaultDates = getDefaultDates();
|
||||
@@ -221,11 +223,9 @@
|
||||
<div class="space-y-1.5 col-span-2 sm:col-span-2">
|
||||
<Label class="text-xs">日期范围</Label>
|
||||
<DateRangePicker
|
||||
{startDate}
|
||||
{endDate}
|
||||
bind:startDate
|
||||
bind:endDate
|
||||
onchange={(start, end) => {
|
||||
startDate = start;
|
||||
endDate = end;
|
||||
applyFilters();
|
||||
}}
|
||||
/>
|
||||
@@ -344,6 +344,9 @@
|
||||
<div class="flex items-center justify-between mt-4">
|
||||
<p class="text-sm text-muted-foreground">
|
||||
显示 {(currentPage - 1) * pageSize + 1} - {Math.min(currentPage * pageSize, totalRecords)} 条,共 {totalRecords} 条
|
||||
{#if searchText}
|
||||
<span class="text-muted-foreground/70">(当前页筛选后:{displayRecords.length} 条)</span>
|
||||
{/if}
|
||||
</p>
|
||||
<div class="flex items-center gap-2">
|
||||
<Button
|
||||
@@ -356,13 +359,17 @@
|
||||
上一页
|
||||
</Button>
|
||||
<div class="flex items-center gap-1">
|
||||
{#each Array.from({ length: Math.min(5, totalPages) }, (_, i) => {
|
||||
// 计算显示的页码范围
|
||||
let start = Math.max(1, currentPage - 2);
|
||||
let end = Math.min(totalPages, start + 4);
|
||||
start = Math.max(1, end - 4);
|
||||
return start + i;
|
||||
}).filter(p => p <= totalPages) as page}
|
||||
{#each (() => {
|
||||
// 计算显示的页码范围(最多显示5页)
|
||||
const maxPages = 5;
|
||||
let start = Math.max(1, currentPage - Math.floor(maxPages / 2));
|
||||
let end = Math.min(totalPages, start + maxPages - 1);
|
||||
// 如果右侧空间不足,向左调整
|
||||
if (end - start < maxPages - 1) {
|
||||
start = Math.max(1, end - maxPages + 1);
|
||||
}
|
||||
return Array.from({ length: end - start + 1 }, (_, i) => start + i);
|
||||
})() as page}
|
||||
<Button
|
||||
variant={page === currentPage ? 'default' : 'outline'}
|
||||
size="sm"
|
||||
|
||||
Reference in New Issue
Block a user