vsyswin-ui 0.2.82 → 0.2.84-1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vsyswin-ui",
3
- "version": "0.2.82",
3
+ "version": "0.2.84-1",
4
4
  "main": "lib/vsyswin-ui.umd.min.js",
5
5
  "private": false,
6
6
  "description": "Vue2.x的应用组件库.",
@@ -20,6 +20,8 @@
20
20
  :expand-row-keys="expandRowKeys"
21
21
  @sort-change="handleSortChange"
22
22
  @selection-change="handleSelectionChange"
23
+ @select-all="handleSelectAll"
24
+ @select="handleSelect"
23
25
  @expand-change="handleExpandChange"
24
26
  @header-dragend="handleHeaderDragend"
25
27
  >
@@ -182,6 +184,84 @@
182
184
  import noData from './noData';
183
185
  import dragSet from '../../drag-set';
184
186
  import BigDataTable from './BigDataTable.js'
187
+
188
+ /** localStorage 列偏好版本:与 props.columns 合并,避免缓存覆盖新增列与代码侧列定义 */
189
+ const COLUMN_STORAGE_VERSION = 2
190
+
191
+ function migrateLegacyColumnArray (savedArr, devColumns) {
192
+ const devPropSet = new Set(devColumns.map((c) => c.prop).filter(Boolean))
193
+ const order = savedArr.map((c) => c.prop).filter((p) => p && devPropSet.has(p))
194
+ const show = {}
195
+ for (const item of savedArr) {
196
+ if (item.prop && devPropSet.has(item.prop) && item.isShow !== undefined) {
197
+ show[item.prop] = item.isShow
198
+ }
199
+ }
200
+ return {
201
+ order,
202
+ show,
203
+ resizedWidths: {}
204
+ }
205
+ }
206
+
207
+ function parseColumnPrefs (raw, devColumns) {
208
+ if (!raw) return null
209
+ let parsed
210
+ try {
211
+ parsed = JSON.parse(raw)
212
+ } catch {
213
+ return null
214
+ }
215
+ if (Array.isArray(parsed)) {
216
+ return migrateLegacyColumnArray(parsed, devColumns)
217
+ }
218
+ if (parsed && parsed.v === COLUMN_STORAGE_VERSION) {
219
+ return {
220
+ order: Array.isArray(parsed.order) ? parsed.order : [],
221
+ show: parsed.show && typeof parsed.show === 'object' ? parsed.show : {},
222
+ resizedWidths:
223
+ parsed.resizedWidths && typeof parsed.resizedWidths === 'object'
224
+ ? parsed.resizedWidths
225
+ : {}
226
+ }
227
+ }
228
+ return null
229
+ }
230
+
231
+ function mergeColumnsWithPrefs (devColumns, prefs) {
232
+ if (!devColumns || !devColumns.length) return []
233
+ if (!prefs) return devColumns.map((c) => ({ ...c }))
234
+ const { order, show, resizedWidths } = prefs
235
+ const mergedByProp = new Map()
236
+ for (const devCol of devColumns) {
237
+ const prop = devCol.prop
238
+ mergedByProp.set(prop, {
239
+ ...devCol,
240
+ isShow: show[prop] !== undefined ? show[prop] : devCol.isShow,
241
+ width:
242
+ resizedWidths[prop] !== undefined && resizedWidths[prop] !== null
243
+ ? resizedWidths[prop]
244
+ : devCol.width
245
+ })
246
+ }
247
+ const ordered = []
248
+ const seen = new Set()
249
+ const devPropOrder = devColumns.map((c) => c.prop)
250
+ for (const prop of order) {
251
+ if (mergedByProp.has(prop)) {
252
+ ordered.push(mergedByProp.get(prop))
253
+ seen.add(prop)
254
+ }
255
+ }
256
+ for (const prop of devPropOrder) {
257
+ if (!seen.has(prop)) {
258
+ ordered.push(mergedByProp.get(prop))
259
+ seen.add(prop)
260
+ }
261
+ }
262
+ return ordered
263
+ }
264
+
185
265
  export default {
186
266
  name: 'SyTable',
187
267
  props: {
@@ -274,11 +354,19 @@ export default {
274
354
  type: Object,
275
355
  default: () => {}
276
356
  },
277
- // 功能模块key唯一
357
+ // 功能模块key唯一(应用内需区分不同表格时请保证唯一或使用 columnPrefsScope)
278
358
  nameKey: {
279
359
  type: String,
280
360
  default: () => ''
281
361
  },
362
+ /**
363
+ * 列偏好命名空间:避免不同页面共用相同 nameKey 时 localStorage 冲突。
364
+ * 键名为 vsyswin_{columnPrefsScope}__{nameKey}
365
+ */
366
+ columnPrefsScope: {
367
+ type: String,
368
+ default: () => ''
369
+ },
282
370
  // 合计功能保留小数位
283
371
  decimalNum: {
284
372
  type: Number,
@@ -294,6 +382,9 @@ export default {
294
382
  2: '全部合计'
295
383
  },
296
384
  columnList: [], // 表格列数据
385
+ columnPrefs: {
386
+ resizedWidths: {}
387
+ },
297
388
  sumType: 2, // 合计类型 0:分页合计;1选中合计;2全部合计
298
389
  selection: [], // 当前选中表格数据
299
390
  fillData: [],
@@ -332,20 +423,25 @@ export default {
332
423
  }
333
424
  },
334
425
  immediate: true
426
+ },
427
+ columns: {
428
+ handler () {
429
+ this.syncColumnsFromProps()
430
+ },
431
+ deep: true
432
+ },
433
+ nameKey () {
434
+ this.syncColumnsFromProps()
435
+ },
436
+ columnPrefsScope () {
437
+ this.syncColumnsFromProps()
335
438
  }
336
439
  },
337
440
  created() {
338
441
  this.getColumnsData(); // 获取表格列数据
339
442
  },
340
443
  mounted () {
341
- for (const item of this.columnList) {
342
- this.$set(item, 'visible', false);
343
- if (['multiple', 'daterange', 'monthrange'].indexOf(item.filterType) > -1) {
344
- this.$set(item, 'value', []);
345
- } else if (['text', 'single'].indexOf(item.filterType) > -1) {
346
- this.$set(item, 'value', '');
347
- }
348
- }
444
+ this.applyColumnFilterDefaults()
349
445
  },
350
446
  updated () {
351
447
  this.doLayout();
@@ -392,28 +488,77 @@ export default {
392
488
  this.tableControl = new BigDataTable(this, options)
393
489
  console.log(this.fillData, 375)
394
490
  },
395
- // 获取表格列数据
396
- getColumnsData () {
397
- if (this.nameKey) {
398
- const list = localStorage.getItem(`vsyswin_${this.nameKey}`);
399
- if (list) {
400
- this.columnList = JSON.parse(list);
401
- } else {
402
- this.columnList = this.columns;
491
+ getColumnStorageKey () {
492
+ if (!this.nameKey) return ''
493
+ const scope = (this.columnPrefsScope || '').trim()
494
+ return scope
495
+ ? `vsyswin_${scope}__${this.nameKey}`
496
+ : `vsyswin_${this.nameKey}`
497
+ },
498
+ persistColumnPrefs () {
499
+ if (!this.nameKey) return
500
+ const props = new Set(this.columnList.map((c) => c.prop).filter(Boolean))
501
+ const pruned = {}
502
+ for (const key of Object.keys(this.columnPrefs.resizedWidths)) {
503
+ if (props.has(key)) pruned[key] = this.columnPrefs.resizedWidths[key]
504
+ }
505
+ const payload = {
506
+ v: COLUMN_STORAGE_VERSION,
507
+ order: this.columnList.map((c) => c.prop),
508
+ show: Object.fromEntries(
509
+ this.columnList.map((c) => [c.prop, !!c.isShow])
510
+ ),
511
+ resizedWidths: pruned
512
+ }
513
+ localStorage.setItem(this.getColumnStorageKey(), JSON.stringify(payload))
514
+ },
515
+ syncColumnsFromProps () {
516
+ this.getColumnsData()
517
+ this.applyColumnFilterDefaults()
518
+ },
519
+ applyColumnFilterDefaults () {
520
+ for (const item of this.columnList) {
521
+ if (item.visible === undefined) this.$set(item, 'visible', false)
522
+ if (['multiple', 'daterange', 'monthrange'].indexOf(item.filterType) > -1) {
523
+ if (item.value === undefined) this.$set(item, 'value', [])
524
+ } else if (['text', 'single'].indexOf(item.filterType) > -1) {
525
+ if (item.value === undefined) this.$set(item, 'value', '')
403
526
  }
404
- } else {
405
- this.columnList = this.columns;
406
527
  }
407
528
  },
529
+ // 获取表格列数据(以 columns 为权威定义,localStorage 只存顺序 / 显示 / 用户拖拽宽度)
530
+ getColumnsData () {
531
+ const devColumns = Array.isArray(this.columns)
532
+ ? this.columns.map((c) => ({ ...c }))
533
+ : []
534
+ if (!this.nameKey) {
535
+ this.columnList = devColumns
536
+ this.columnPrefs.resizedWidths = {}
537
+ return
538
+ }
539
+ const raw = localStorage.getItem(this.getColumnStorageKey())
540
+ const prefs = parseColumnPrefs(raw, devColumns)
541
+ if (!prefs) {
542
+ this.columnList = devColumns
543
+ this.columnPrefs.resizedWidths = {}
544
+ return
545
+ }
546
+ this.columnPrefs.resizedWidths = { ...prefs.resizedWidths }
547
+ this.columnList = mergeColumnsWithPrefs(devColumns, prefs)
548
+ },
408
549
  // 列表头宽度拉伸
409
550
  handleHeaderDragend (newWidth, oldWidth, column, event) {
410
551
  if (this.nameKey) {
552
+ const prop = column.property
411
553
  for (const item of this.columnList) {
412
- if (item.label === column.label && item.prop === column.property) {
413
- this.$set(item, 'width', newWidth);
554
+ if (item.prop === prop) {
555
+ this.$set(item, 'width', newWidth)
414
556
  }
415
557
  }
416
- localStorage.setItem(`vsyswin_${this.nameKey}`, JSON.stringify(this.columnList));
558
+ if (prop !== undefined && prop !== null && prop !== '') {
559
+ this.$set(this.columnPrefs.resizedWidths, prop, newWidth)
560
+ }
561
+ this.persistColumnPrefs()
417
562
  } else {
418
563
  this.$emit('header-dragend', newWidth, oldWidth, column, event);
419
564
  }
@@ -431,6 +576,14 @@ export default {
431
576
  this.selection = selection;
432
577
  this.$emit('selection-change', selection);
433
578
  },
579
+ // 当用户手动勾选全选 Checkbox 时触发的事件
580
+ handleSelectAll (selection) {
581
+ this.$emit('select-all', selection);
582
+ },
583
+ // 当用户手动勾选数据行的 Checkbox 时触发的事件
584
+ handleSelect (selection, row) {
585
+ this.$emit('select', selection, row);
586
+ },
434
587
  // 表格行展开事件
435
588
  handleExpandChange (row, value) {
436
589
  this.$emit('expand-change', row, value);
@@ -446,7 +599,7 @@ export default {
446
599
  dragChange (list) {
447
600
  if (this.nameKey) {
448
601
  this.columnList = list;
449
- localStorage.setItem(`vsyswin_${this.nameKey}`, JSON.stringify(this.columnList));
602
+ this.persistColumnPrefs()
450
603
  } else {
451
604
  this.$emit('drag-change', list);
452
605
  }