stk-table-vue 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ import { getCalculatedColWidth } from './utils';
6
6
  type Option<DT extends Record<string, any>> = {
7
7
  props: any;
8
8
  tableContainerRef: Ref<HTMLElement | undefined>;
9
+ theadRef: Ref<HTMLElement | undefined>;
9
10
  dataSourceCopy: ShallowRef<DT[]>;
10
11
  tableHeaderLast: ShallowRef<StkTableColumn<DT>[]>;
11
12
  tableHeaders: ShallowRef<StkTableColumn<DT>[][]>;
@@ -27,6 +28,8 @@ export type VirtualScrollStore = {
27
28
  offsetTop: number;
28
29
  /** 纵向滚动条位置,用于判断是横向滚动还是纵向 */
29
30
  scrollTop: number;
31
+ /** 总滚动高度 */
32
+ scrollHeight: number;
30
33
  };
31
34
  /** 暂存横向虚拟滚动的数据 */
32
35
  export type VirtualScrollXStore = {
@@ -55,10 +58,14 @@ const VUE2_SCROLL_TIMEOUT_MS = 200;
55
58
  export function useVirtualScroll<DT extends Record<string, any>>({
56
59
  props,
57
60
  tableContainerRef,
61
+ theadRef,
58
62
  dataSourceCopy,
59
63
  tableHeaderLast,
60
64
  tableHeaders,
61
65
  }: Option<DT>) {
66
+ /** 表头高度 */
67
+ const tableHeaderHeight = ref(props.headerRowHeight ?? props.rowHeight);
68
+
62
69
  const virtualScroll = ref<VirtualScrollStore>({
63
70
  containerHeight: 0,
64
71
  rowHeight: props.rowHeight,
@@ -67,6 +74,7 @@ export function useVirtualScroll<DT extends Record<string, any>>({
67
74
  endIndex: 0,
68
75
  offsetTop: 0,
69
76
  scrollTop: 0,
77
+ scrollHeight: 0,
70
78
  });
71
79
 
72
80
  const virtualScrollX = ref<VirtualScrollXStore>({
@@ -106,8 +114,8 @@ export function useVirtualScroll<DT extends Record<string, any>>({
106
114
  const tableHeaderLastValue = tableHeaderLast.value;
107
115
  if (virtualX_on.value) {
108
116
  // 虚拟横向滚动,固定列要一直保持存在
109
- const leftCols = [];
110
- const rightCols = [];
117
+ const leftCols: StkTableColumn<DT>[] = [];
118
+ const rightCols: StkTableColumn<DT>[] = [];
111
119
  /**
112
120
  * 存在问题:
113
121
  * table columns 从多到少时。比方原来的start=5,end=10,现在start=4,end=8。这时候endIndex就超出数组范围了。
@@ -146,6 +154,15 @@ export function useVirtualScroll<DT extends Record<string, any>>({
146
154
  return width;
147
155
  });
148
156
 
157
+ /** 表头高度 */
158
+ function getTableHeaderHeight() {
159
+ const { headerRowHeight } = props;
160
+ return headerRowHeight * tableHeaders.value.length;
161
+ // if (props.virtual) {
162
+ // }
163
+ // return theadRef.value?.offsetHeight || 0;
164
+ }
165
+
149
166
  /**
150
167
  * 初始化Y虚拟滚动参数
151
168
  * @param {number} [height] 虚拟滚动的高度
@@ -156,17 +173,27 @@ export function useVirtualScroll<DT extends Record<string, any>>({
156
173
  height = 0;
157
174
  }
158
175
  if (!virtual_on.value) return;
159
- const { offsetHeight, scrollTop } = tableContainerRef.value || {};
176
+ const { offsetHeight, scrollHeight } = tableContainerRef.value || {};
177
+ let scrollTop = tableContainerRef.value?.scrollTop || 0;
178
+
160
179
  const { rowHeight } = virtualScroll.value;
161
180
  const containerHeight = height || offsetHeight || DEFAULT_TABLE_HEIGHT;
162
- const { headless, headerRowHeight } = props;
181
+ const { headless } = props;
163
182
  let pageSize = Math.ceil(containerHeight / rowHeight);
183
+ const headerHeight = getTableHeaderHeight();
184
+ tableHeaderHeight.value = headerHeight;
164
185
  if (!headless) {
165
186
  /** 表头高度占几行表体高度数 */
166
- const headerToBodyRowHeightCount = Math.floor((tableHeaders.value.length * (headerRowHeight || rowHeight)) / rowHeight);
187
+ const headerToBodyRowHeightCount = Math.floor(headerHeight) / rowHeight;
167
188
  pageSize -= headerToBodyRowHeightCount; //减去表头行数
168
189
  }
169
- Object.assign(virtualScroll.value, { containerHeight, pageSize });
190
+ /** 最大的scrollTop */
191
+ const maxScrollTop = dataSourceCopy.value.length * rowHeight + tableHeaderHeight.value - containerHeight;
192
+ if (scrollTop > maxScrollTop) {
193
+ /** 用于修复: 滚动条不在顶部时,表格数据变少,导致滚动条位置有误 */
194
+ scrollTop = maxScrollTop;
195
+ }
196
+ Object.assign(virtualScroll.value, { containerHeight, pageSize, scrollHeight });
170
197
  updateVirtualScrollY(scrollTop);
171
198
  }
172
199
 
@@ -190,11 +217,13 @@ export function useVirtualScroll<DT extends Record<string, any>>({
190
217
  /** 通过滚动条位置,计算虚拟滚动的参数 */
191
218
  function updateVirtualScrollY(sTop = 0) {
192
219
  const { rowHeight, pageSize, scrollTop, startIndex: oldStartIndex } = virtualScroll.value;
193
- // 先更新滚动条位置记录,其他地方可能有依赖。(stripe 时ArrowUp/Down滚动依赖)
220
+ // 先更新滚动条位置记录,其他地方有依赖。(stripe 时ArrowUp/Down滚动依赖)
194
221
  virtualScroll.value.scrollTop = sTop;
195
222
 
196
223
  // 非虚拟滚动不往下执行
197
- if (!virtual_on.value) return;
224
+ if (!virtual_on.value) {
225
+ return;
226
+ }
198
227
 
199
228
  let startIndex = Math.floor(sTop / rowHeight);
200
229
  if (startIndex < 0) {
@@ -211,11 +240,21 @@ export function useVirtualScroll<DT extends Record<string, any>>({
211
240
  if (props.stripe) {
212
241
  endIndex += 1; // 斑马纹下多渲染一些
213
242
  }
214
- const offsetTop = startIndex * rowHeight; // startIndex之前的高度
215
- endIndex = Math.min(endIndex, dataSourceCopy.value.length); // 溢出index修正
243
+
244
+ // 溢出endIndex修正
245
+ endIndex = Math.min(endIndex, dataSourceCopy.value.length);
246
+
247
+ if (startIndex >= endIndex) {
248
+ // 兜底,不一定会执行到这里
249
+ startIndex = endIndex - pageSize;
250
+ }
251
+
216
252
  if (vue2ScrollYTimeout) {
217
253
  window.clearTimeout(vue2ScrollYTimeout);
218
254
  }
255
+
256
+ const offsetTop = startIndex * rowHeight; // startIndex之前的高度
257
+
219
258
  /**
220
259
  * 一次滚动大于一页时表示滚动过快,回退优化
221
260
  */
@@ -18,13 +18,9 @@ function isEmptyValue(val: any, isNumber?: boolean) {
18
18
  * @param sortState.sortType 排序方式
19
19
  * @param newItem 要插入的数据
20
20
  * @param targetArray 表格数据
21
+ * @return targetArray 的浅拷贝
21
22
  */
22
- export function insertToOrderedArray<T extends object>(
23
- sortState: SortState<keyof T>,
24
- newItem: any,
25
- targetArray: T[],
26
- sortConfig: SortConfig<T> = {},
27
- ) {
23
+ export function insertToOrderedArray<T extends object>(sortState: SortState<T>, newItem: any, targetArray: T[], sortConfig: SortConfig<T> = {}) {
28
24
  const { dataIndex, order } = sortState;
29
25
  sortConfig = { emptyToBottom: false, ...sortConfig };
30
26
  let { sortType } = sortState;