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.
@@ -1,14 +1,14 @@
1
1
  import { Ref, ShallowRef, computed, ref } from 'vue';
2
- import { Default_Table_Height, Default_Table_Width } from './const';
2
+ import { DEFAULT_TABLE_HEIGHT, DEFAULT_TABLE_WIDTH } from './const';
3
3
  import { StkTableColumn } from './types';
4
- import { getColWidth } from './utils';
4
+ import { getCalculatedColWidth } from './utils';
5
5
 
6
6
  type Option<DT extends Record<string, any>> = {
7
7
  props: any;
8
- tableContainer: Ref<HTMLElement | undefined>;
8
+ tableContainerRef: Ref<HTMLElement | undefined>;
9
9
  dataSourceCopy: ShallowRef<DT[]>;
10
- tableHeaderLast: Ref<StkTableColumn<DT>[]>;
11
- tableHeaders: Ref<StkTableColumn<DT>[][]>;
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
- tableContainer,
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 props.virtualX && tableHeaderLast.value.reduce((sum, col) => (sum += getColWidth(col)), 0) > virtualScrollX.value.containerWidth + 100;
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 += getColWidth(col);
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 } = tableContainer.value || {};
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 || Default_Table_Height;
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 } = tableContainer.value || {};
166
+ const { offsetWidth, scrollLeft } = tableContainerRef.value || {};
164
167
  // scrollTo(null, 0);
165
- virtualScrollX.value.containerWidth = offsetWidth || Default_Table_Width;
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') continue;
247
- const colWidth = getColWidth(col);
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 += getColWidth(col);
269
+ colWidthSum += getCalculatedColWidth(col);
262
270
  // 列宽大于容器宽度则停止
263
- if (colWidthSum >= virtualScrollX.value.containerWidth) {
264
- endIndex = colIndex + 1; // TODO:预渲染的列数
271
+ if (colWidthSum >= containerWidth) {
272
+ endIndex = colIndex + 1; // 由于slice[start,end),要加1
265
273
  break;
266
274
  }
267
275
  }
@@ -1,4 +1,4 @@
1
- import { Default_Col_Width } from './const';
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 ?? Default_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 getColWidthStr(col: StkTableColumn<any> | null | undefined, key: 'width' | 'minWidth' | 'maxWidth' = 'width') {
196
- const val = col?.[key];
197
- if (typeof val === 'number') {
198
- return val + 'px';
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 val;
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
  }
package/src/vite-env.d.ts CHANGED
@@ -4,3 +4,7 @@ declare module '*.vue' {
4
4
  const component: DefineComponent<object, object, any>;
5
5
  export default component;
6
6
  }
7
+
8
+ interface Window {
9
+ __STK_TB_ID_COUNT__: number;
10
+ }