feat(data-grid): make the row number column show original row numbers
This commit is contained in:
1 parent
9ab1462ca8
commit
6517284d95
17 files changed
+196
-10
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveDataGridRowNumberLabel } from "@/lib/dataGrid/dataGridRowNumber";
|
||||
|
||||
describe("data grid row number labels", () => {
|
||||
it("keeps the current view position by default", () => {
|
||||
expect(resolveDataGridRowNumberLabel({ displayIndex: 0, sourceRowNumbers: false, pageOffset: 0 })).toBe("1");
|
||||
expect(resolveDataGridRowNumberLabel({ displayIndex: 41, sourceIndex: 137, sourceRowNumbers: false, pageOffset: 0 })).toBe("42");
|
||||
});
|
||||
|
||||
it("adds the page offset in paged results", () => {
|
||||
expect(resolveDataGridRowNumberLabel({ displayIndex: 0, sourceRowNumbers: false, pageOffset: 100 })).toBe("101");
|
||||
expect(resolveDataGridRowNumberLabel({ displayIndex: 0, sourceIndex: 0, sourceRowNumbers: true, pageOffset: 100 })).toBe("101");
|
||||
});
|
||||
|
||||
it("survives filtering by reporting the original row number", () => {
|
||||
// 第 137 行被筛成视图里的第 3 行:原始行号模式下仍应显示 137
|
||||
expect(resolveDataGridRowNumberLabel({ displayIndex: 2, sourceIndex: 136, sourceRowNumbers: true, pageOffset: 0 })).toBe("137");
|
||||
// 分页场景下 sourceIndex 是页内下标,需要叠加页偏移
|
||||
expect(resolveDataGridRowNumberLabel({ displayIndex: 2, sourceIndex: 36, sourceRowNumbers: true, pageOffset: 100 })).toBe("137");
|
||||
});
|
||||
|
||||
it("falls back to the view position for rows without a source index", () => {
|
||||
// 新增行还没有落库位置,sourceIndex 为 undefined
|
||||
expect(resolveDataGridRowNumberLabel({ displayIndex: 7, sourceRowNumbers: true, pageOffset: 0 })).toBe("8");
|
||||
});
|
||||
|
||||
it("keeps the draft placeholder regardless of the mode", () => {
|
||||
expect(resolveDataGridRowNumberLabel({ displayIndex: 7, isDraft: true, sourceRowNumbers: false, pageOffset: 0 })).toBe("*");
|
||||
expect(resolveDataGridRowNumberLabel({ displayIndex: 7, sourceIndex: 3, isDraft: true, sourceRowNumbers: true, pageOffset: 100 })).toBe("*");
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import type { CellSelectionRange } from "@/lib/dataGrid/gridSelection";
|
||||
import type { RowStatus } from "@/lib/dataGrid/gridRowStatus";
|
||||
import { DATA_GRID_DARK_SEARCH_COLORS, dataGridTypeForeground, resolveDataGridPaintTheme, type DataGridPaintTheme } from "@/lib/dataGrid/dataGridPaintTheme";
|
||||
import type { CrosshairTarget } from "@/lib/dataGrid/crosshairHighlight";
|
||||
import { resolveDataGridRowNumberLabel } from "@/lib/dataGrid/dataGridRowNumber";
|
||||
|
||||
export const CANVAS_DATA_GRID_ROW_HEIGHT = 26;
|
||||
export const MAX_CANVAS_DATA_GRID_PIXEL_RATIO = 4;
|
||||
@@ -102,6 +103,8 @@ export interface DrawCanvasDataGridOptions {
|
||||
booleanDisplayMode?: "checkbox" | "dropdown";
|
||||
flatteningMultiLineEnabled: boolean;
|
||||
showWhitespace?: boolean;
|
||||
/** 行号栏取值:`view` = 当前视图序号(默认,与筛选前一致),`source` = 筛选前的原始行号 */
|
||||
rowNumberMode?: "view" | "source";
|
||||
}
|
||||
|
||||
type NumericCanvasContext = CanvasRenderingContext2D & {
|
||||
@@ -365,6 +368,7 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions): boolean
|
||||
booleanDisplayMode = "dropdown",
|
||||
flatteningMultiLineEnabled,
|
||||
showWhitespace = false,
|
||||
rowNumberMode = "view",
|
||||
} = options;
|
||||
// 框选热路径:整次绘制只判断一次。常见情况(单矩形 / 多列且每段都是多格)可跳过逐格 kind 查询
|
||||
const paintSelectionOuterFrame = dataGridSelectionUsesOuterFrame(selectionFrames);
|
||||
@@ -485,13 +489,17 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions): boolean
|
||||
ctx.beginPath();
|
||||
ctx.rect(0, y, rowNumberWidth, CANVAS_DATA_GRID_ROW_HEIGHT);
|
||||
ctx.clip();
|
||||
if (item.isDraft) {
|
||||
ctx.fillText("*", rowNumberTextX, textY);
|
||||
} else if (infiniteScrollEnabled) {
|
||||
ctx.fillText(String(item.displayIndex + 1), rowNumberTextX, textY);
|
||||
} else {
|
||||
ctx.fillText(String(item.displayIndex + 1 + pageOffset), rowNumberTextX, textY);
|
||||
}
|
||||
ctx.fillText(
|
||||
resolveDataGridRowNumberLabel({
|
||||
displayIndex: item.displayIndex,
|
||||
sourceIndex: item.sourceIndex,
|
||||
isDraft: item.isDraft,
|
||||
sourceRowNumbers: rowNumberMode === "source",
|
||||
pageOffset: infiniteScrollEnabled ? 0 : pageOffset,
|
||||
}),
|
||||
rowNumberTextX,
|
||||
textY,
|
||||
);
|
||||
ctx.restore();
|
||||
ctx.font = normalFont;
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* 行号栏取值。
|
||||
*
|
||||
* `displayIndex` 是当前视图里的压缩序号:一旦应用筛选(Ctrl+F 过滤模式、列头筛选、
|
||||
* 行状态筛选),被隐藏的行后面的行号会整体前移,无法再对应回完整结果里的位置。
|
||||
* `sourceIndex` 是筛选前的原始下标,一直保留在行数据里,因此 `source` 模式直接用它,
|
||||
* 让筛选前后同一个行号始终指向同一条数据。分页模式下原始下标是页内下标,
|
||||
* 仍需叠加页偏移才能得到完整结果中的位置。
|
||||
*/
|
||||
export interface DataGridRowNumberInput {
|
||||
displayIndex: number;
|
||||
sourceIndex?: number;
|
||||
isDraft?: boolean;
|
||||
/** true = 显示原始行号(sourceIndex),false = 显示当前视图序号(displayIndex) */
|
||||
sourceRowNumbers: boolean;
|
||||
/** 已加载行之前的行数:分页页码偏移;无限滚动 / 单页时为 0 */
|
||||
pageOffset: number;
|
||||
}
|
||||
|
||||
export function resolveDataGridRowNumberLabel(input: DataGridRowNumberInput): string {
|
||||
// 待保存的草稿行没有落库位置,保持原有的 "*" 占位
|
||||
if (input.isDraft) return "*";
|
||||
const index = input.sourceRowNumbers && input.sourceIndex !== undefined ? input.sourceIndex : input.displayIndex;
|
||||
return String(index + 1 + input.pageOffset);
|
||||
}
|
||||
Reference in new issue
Block a user