vsyswin-ui 0.2.83 → 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.83",
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,7 @@
20
20
  :expand-row-keys="expandRowKeys"
21
21
  @sort-change="handleSortChange"
22
22
  @selection-change="handleSelectionChange"
23
+ @select-all="handleSelectAll"
23
24
  @select="handleSelect"
24
25
  @expand-change="handleExpandChange"
25
26
  @header-dragend="handleHeaderDragend"
@@ -183,6 +184,84 @@
183
184
  import noData from './noData';
184
185
  import dragSet from '../../drag-set';
185
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
+
186
265
  export default {
187
266
  name: 'SyTable',
188
267
  props: {
@@ -275,11 +354,19 @@ export default {
275
354
  type: Object,
276
355
  default: () => {}
277
356
  },
278
- // 功能模块key唯一
357
+ // 功能模块key唯一(应用内需区分不同表格时请保证唯一或使用 columnPrefsScope)
279
358
  nameKey: {
280
359
  type: String,
281
360
  default: () => ''
282
361
  },
362
+ /**
363
+ * 列偏好命名空间:避免不同页面共用相同 nameKey 时 localStorage 冲突。
364
+ * 键名为 vsyswin_{columnPrefsScope}__{nameKey}
365
+ */
366
+ columnPrefsScope: {
367
+ type: String,
368
+ default: () => ''
369
+ },
283
370
  // 合计功能保留小数位
284
371
  decimalNum: {
285
372
  type: Number,
@@ -295,6 +382,9 @@ export default {
295
382
  2: '全部合计'
296
383
  },
297
384
  columnList: [], // 表格列数据
385
+ columnPrefs: {
386
+ resizedWidths: {}
387
+ },
298
388
  sumType: 2, // 合计类型 0:分页合计;1选中合计;2全部合计
299
389
  selection: [], // 当前选中表格数据
300
390
  fillData: [],
@@ -333,20 +423,25 @@ export default {
333
423
  }
334
424
  },
335
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()
336
438
  }
337
439
  },
338
440
  created() {
339
441
  this.getColumnsData(); // 获取表格列数据
340
442
  },
341
443
  mounted () {
342
- for (const item of this.columnList) {
343
- this.$set(item, 'visible', false);
344
- if (['multiple', 'daterange', 'monthrange'].indexOf(item.filterType) > -1) {
345
- this.$set(item, 'value', []);
346
- } else if (['text', 'single'].indexOf(item.filterType) > -1) {
347
- this.$set(item, 'value', '');
348
- }
349
- }
444
+ this.applyColumnFilterDefaults()
350
445
  },
351
446
  updated () {
352
447
  this.doLayout();
@@ -393,28 +488,77 @@ export default {
393
488
  this.tableControl = new BigDataTable(this, options)
394
489
  console.log(this.fillData, 375)
395
490
  },
396
- // 获取表格列数据
397
- getColumnsData () {
398
- if (this.nameKey) {
399
- const list = localStorage.getItem(`vsyswin_${this.nameKey}`);
400
- if (list) {
401
- this.columnList = JSON.parse(list);
402
- } else {
403
- 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', '')
404
526
  }
405
- } else {
406
- this.columnList = this.columns;
407
527
  }
408
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
+ },
409
549
  // 列表头宽度拉伸
410
550
  handleHeaderDragend (newWidth, oldWidth, column, event) {
411
551
  if (this.nameKey) {
552
+ const prop = column.property
412
553
  for (const item of this.columnList) {
413
- if (item.label === column.label && item.prop === column.property) {
414
- this.$set(item, 'width', newWidth);
554
+ if (item.prop === prop) {
555
+ this.$set(item, 'width', newWidth)
415
556
  }
416
557
  }
417
- 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()
418
562
  } else {
419
563
  this.$emit('header-dragend', newWidth, oldWidth, column, event);
420
564
  }
@@ -432,9 +576,14 @@ export default {
432
576
  this.selection = selection;
433
577
  this.$emit('selection-change', selection);
434
578
  },
435
- handleSelect (selection, row) {
436
- this.$emit('select', selection, row);
437
- },
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
+ },
438
587
  // 表格行展开事件
439
588
  handleExpandChange (row, value) {
440
589
  this.$emit('expand-change', row, value);
@@ -450,7 +599,7 @@ export default {
450
599
  dragChange (list) {
451
600
  if (this.nameKey) {
452
601
  this.columnList = list;
453
- localStorage.setItem(`vsyswin_${this.nameKey}`, JSON.stringify(this.columnList));
602
+ this.persistColumnPrefs()
454
603
  } else {
455
604
  this.$emit('drag-change', list);
456
605
  }