stk-table-vue 0.2.8 → 0.3.0
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.
- package/README.md +97 -10
- package/lib/src/StkTable/StkTable.vue.d.ts +27 -4
- package/lib/src/StkTable/const.d.ts +13 -8
- package/lib/src/StkTable/types/index.d.ts +19 -0
- package/lib/src/StkTable/useAutoResize.d.ts +2 -2
- package/lib/src/StkTable/useColResize.d.ts +5 -5
- package/lib/src/StkTable/useFixedCol.d.ts +5 -5
- package/lib/src/StkTable/useFixedStyle.d.ts +3 -3
- package/lib/src/StkTable/useHighlight.d.ts +16 -16
- package/lib/src/StkTable/useKeyboardArrowScroll.d.ts +4 -3
- package/lib/src/StkTable/useVirtualScroll.d.ts +4 -4
- package/lib/src/StkTable/utils.d.ts +11 -3
- package/lib/stk-table-vue.js +349 -199
- package/lib/style.css +8 -2
- package/package.json +4 -5
- package/src/StkTable/StkTable.vue +81 -47
- package/src/StkTable/const.ts +15 -8
- package/src/StkTable/style.less +11 -6
- package/src/StkTable/types/index.ts +21 -0
- package/src/StkTable/useAutoResize.ts +5 -5
- package/src/StkTable/useColResize.ts +18 -18
- package/src/StkTable/useFixedCol.ts +6 -6
- package/src/StkTable/useFixedStyle.ts +15 -12
- package/src/StkTable/useHighlight.ts +267 -109
- package/src/StkTable/useKeyboardArrowScroll.ts +19 -7
- package/src/StkTable/useVirtualScroll.ts +25 -17
- package/src/StkTable/utils.ts +27 -9
- package/src/vite-env.d.ts +4 -0
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { Ref, ShallowRef, computed, ref } from 'vue';
|
|
2
|
-
import {
|
|
2
|
+
import { DEFAULT_TABLE_HEIGHT, DEFAULT_TABLE_WIDTH } from './const';
|
|
3
3
|
import { StkTableColumn } from './types';
|
|
4
|
-
import {
|
|
4
|
+
import { getCalculatedColWidth } from './utils';
|
|
5
5
|
|
|
6
6
|
type Option<DT extends Record<string, any>> = {
|
|
7
7
|
props: any;
|
|
8
|
-
|
|
8
|
+
tableContainerRef: Ref<HTMLElement | undefined>;
|
|
9
9
|
dataSourceCopy: ShallowRef<DT[]>;
|
|
10
|
-
tableHeaderLast:
|
|
11
|
-
tableHeaders:
|
|
10
|
+
tableHeaderLast: ShallowRef<StkTableColumn<DT>[]>;
|
|
11
|
+
tableHeaders: ShallowRef<StkTableColumn<DT>[][]>;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
/** 暂存纵向虚拟滚动的数据 */
|
|
@@ -52,7 +52,7 @@ const VUE2_SCROLL_TIMEOUT_MS = 200;
|
|
|
52
52
|
*/
|
|
53
53
|
export function useVirtualScroll<DT extends Record<string, any>>({
|
|
54
54
|
props,
|
|
55
|
-
|
|
55
|
+
tableContainerRef,
|
|
56
56
|
dataSourceCopy,
|
|
57
57
|
tableHeaderLast,
|
|
58
58
|
tableHeaders,
|
|
@@ -93,7 +93,10 @@ export function useVirtualScroll<DT extends Record<string, any>>({
|
|
|
93
93
|
});
|
|
94
94
|
|
|
95
95
|
const virtualX_on = computed(() => {
|
|
96
|
-
return
|
|
96
|
+
return (
|
|
97
|
+
props.virtualX &&
|
|
98
|
+
tableHeaderLast.value.reduce((sum, col) => (sum += getCalculatedColWidth(col)), 0) > virtualScrollX.value.containerWidth + 100
|
|
99
|
+
);
|
|
97
100
|
});
|
|
98
101
|
|
|
99
102
|
const virtualX_columnPart = computed(() => {
|
|
@@ -126,7 +129,7 @@ export function useVirtualScroll<DT extends Record<string, any>>({
|
|
|
126
129
|
for (let i = virtualScrollX.value.endIndex; i < tableHeaderLast.value.length; i++) {
|
|
127
130
|
const col = tableHeaderLast.value[i];
|
|
128
131
|
if (col.fixed !== 'right') {
|
|
129
|
-
width +=
|
|
132
|
+
width += getCalculatedColWidth(col);
|
|
130
133
|
}
|
|
131
134
|
}
|
|
132
135
|
return width;
|
|
@@ -138,14 +141,14 @@ export function useVirtualScroll<DT extends Record<string, any>>({
|
|
|
138
141
|
*/
|
|
139
142
|
function initVirtualScrollY(height?: number) {
|
|
140
143
|
if (!virtual_on.value) return;
|
|
141
|
-
const { offsetHeight, scrollTop } =
|
|
144
|
+
const { offsetHeight, scrollTop } = tableContainerRef.value || {};
|
|
142
145
|
const { rowHeight } = virtualScroll.value;
|
|
143
146
|
let containerHeight: number;
|
|
144
147
|
// FIXME: 可能多次获取offsetHeight 会导致浏览器频繁重排
|
|
145
148
|
if (typeof height === 'number') {
|
|
146
149
|
containerHeight = height;
|
|
147
150
|
} else {
|
|
148
|
-
containerHeight = offsetHeight ||
|
|
151
|
+
containerHeight = offsetHeight || DEFAULT_TABLE_HEIGHT;
|
|
149
152
|
}
|
|
150
153
|
const { headless, headerRowHeight } = props;
|
|
151
154
|
let pageSize = Math.ceil(containerHeight / rowHeight);
|
|
@@ -160,9 +163,9 @@ export function useVirtualScroll<DT extends Record<string, any>>({
|
|
|
160
163
|
|
|
161
164
|
function initVirtualScrollX() {
|
|
162
165
|
if (!props.virtualX) return;
|
|
163
|
-
const { offsetWidth, scrollLeft } =
|
|
166
|
+
const { offsetWidth, scrollLeft } = tableContainerRef.value || {};
|
|
164
167
|
// scrollTo(null, 0);
|
|
165
|
-
virtualScrollX.value.containerWidth = offsetWidth ||
|
|
168
|
+
virtualScrollX.value.containerWidth = offsetWidth || DEFAULT_TABLE_WIDTH;
|
|
166
169
|
updateVirtualScrollX(scrollLeft);
|
|
167
170
|
}
|
|
168
171
|
/**
|
|
@@ -239,12 +242,16 @@ export function useVirtualScroll<DT extends Record<string, any>>({
|
|
|
239
242
|
let offsetLeft = 0;
|
|
240
243
|
|
|
241
244
|
let colWidthSum = 0;
|
|
245
|
+
let leftColWidthSum = 0;
|
|
242
246
|
for (let colIndex = 0; colIndex < headerLength; colIndex++) {
|
|
243
247
|
startIndex++;
|
|
244
248
|
const col = tableHeaderLast.value[colIndex];
|
|
249
|
+
const colWidth = getCalculatedColWidth(col);
|
|
245
250
|
// fixed left 不进入计算列宽
|
|
246
|
-
if (col.fixed === 'left')
|
|
247
|
-
|
|
251
|
+
if (col.fixed === 'left') {
|
|
252
|
+
leftColWidthSum += colWidth;
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
248
255
|
colWidthSum += colWidth;
|
|
249
256
|
// 列宽(非固定列)加到超过scrollLeft的时候,表示startIndex从上一个开始下标
|
|
250
257
|
if (colWidthSum >= sLeft) {
|
|
@@ -255,13 +262,14 @@ export function useVirtualScroll<DT extends Record<string, any>>({
|
|
|
255
262
|
}
|
|
256
263
|
// -----
|
|
257
264
|
colWidthSum = 0;
|
|
265
|
+
const containerWidth = virtualScrollX.value.containerWidth - leftColWidthSum;
|
|
258
266
|
let endIndex = headerLength;
|
|
259
267
|
for (let colIndex = startIndex + 1; colIndex < headerLength; colIndex++) {
|
|
260
268
|
const col = tableHeaderLast.value[colIndex];
|
|
261
|
-
colWidthSum +=
|
|
269
|
+
colWidthSum += getCalculatedColWidth(col);
|
|
262
270
|
// 列宽大于容器宽度则停止
|
|
263
|
-
if (colWidthSum >=
|
|
264
|
-
endIndex = colIndex + 1; //
|
|
271
|
+
if (colWidthSum >= containerWidth) {
|
|
272
|
+
endIndex = colIndex + 1; // 由于slice[start,end),要加1
|
|
265
273
|
break;
|
|
266
274
|
}
|
|
267
275
|
}
|
package/src/StkTable/utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DEFAULT_COL_WIDTH, STK_ID_PREFIX } from './const';
|
|
2
2
|
import { Order, SortConfig, SortOption, SortState, StkTableColumn } from './types';
|
|
3
3
|
|
|
4
4
|
/** 是否空值 */
|
|
@@ -182,20 +182,38 @@ export function howDeepTheHeader(arr: StkTableColumn<any>[], level = 1) {
|
|
|
182
182
|
return Math.max(...levels);
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
-
/**
|
|
185
|
+
/**
|
|
186
|
+
* 获取列宽
|
|
187
|
+
*
|
|
188
|
+
* 关于列宽的操作往往在横向滚动中使用。既然已经有横向滚动了,则列宽会被压缩至minWidth,所以优先取minWidth
|
|
189
|
+
*/
|
|
186
190
|
export function getColWidth(col: StkTableColumn<any> | null): number {
|
|
187
|
-
const val = col?.width ??
|
|
191
|
+
const val = col?.minWidth ?? col?.width ?? DEFAULT_COL_WIDTH;
|
|
188
192
|
if (typeof val === 'number') {
|
|
189
193
|
return Math.floor(val);
|
|
190
194
|
}
|
|
191
195
|
return parseInt(val);
|
|
192
196
|
}
|
|
193
197
|
|
|
194
|
-
/**
|
|
195
|
-
export function
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
198
|
+
/** 获取计算后的宽度 */
|
|
199
|
+
export function getCalculatedColWidth(col: StkTableColumn<any> | null) {
|
|
200
|
+
return col?.__WIDTH__ ?? +DEFAULT_COL_WIDTH;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** number列宽+px */
|
|
204
|
+
export function transformWidthToStr(width?: string | number) {
|
|
205
|
+
if (typeof width === 'number') {
|
|
206
|
+
return width + 'px';
|
|
199
207
|
}
|
|
200
|
-
return
|
|
208
|
+
return width;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** 创建组件唯一标识 */
|
|
212
|
+
export function createStkTableId() {
|
|
213
|
+
if (!window.__STK_TB_ID_COUNT__) {
|
|
214
|
+
window.__STK_TB_ID_COUNT__ = 0;
|
|
215
|
+
}
|
|
216
|
+
window.__STK_TB_ID_COUNT__ += 1;
|
|
217
|
+
|
|
218
|
+
return STK_ID_PREFIX + window.__STK_TB_ID_COUNT__.toString(36);
|
|
201
219
|
}
|