wkjp-list-page 1.0.34 → 1.0.36

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": "wkjp-list-page",
3
- "version": "1.0.34",
3
+ "version": "1.0.36",
4
4
  "description": "Vue2 + ElementUI CommonListPage component",
5
5
  "main": "src/index.js",
6
6
  "module": "src/index.js",
@@ -93,13 +93,13 @@
93
93
  @change="(val) => handleDateFieldChange(item, val)"
94
94
  />
95
95
  </el-form-item>
96
- <el-form-item v-if="searchCollapseActive && !searchExpanded" class="common-list-page__search-toggle">
97
- <el-button type="text" class="common-list-page__search-toggle-btn" @click="searchExpanded = true">
96
+ <el-form-item v-if="searchCollapseActive && !internalSearchExpanded" class="common-list-page__search-toggle">
97
+ <el-button type="text" class="common-list-page__search-toggle-btn" @click="toggleSearchExpanded(true)">
98
98
  {{ expandSearchText }}
99
99
  </el-button>
100
100
  </el-form-item>
101
- <el-form-item v-if="searchCollapseActive && searchExpanded" class="common-list-page__search-toggle">
102
- <el-button type="text" class="common-list-page__search-toggle-btn" @click="searchExpanded = false">
101
+ <el-form-item v-if="searchCollapseActive && internalSearchExpanded" class="common-list-page__search-toggle">
102
+ <el-button type="text" class="common-list-page__search-toggle-btn" @click="toggleSearchExpanded(false)">
103
103
  {{ foldSearchText }}
104
104
  </el-button>
105
105
  </el-form-item>
@@ -426,8 +426,10 @@ export default {
426
426
  collapseSearch: { type: Boolean, default: undefined },
427
427
  /** 折叠时默认展示的查询项条数;未传时按 **6**;显式传 ≤0 表示不折叠 */
428
428
  searchVisibleMax: { type: Number, default: null },
429
+ /** 搜索区是否展开,支持 .sync 修饰符进行双向绑定,默认 true(展开状态) */
430
+ searchExpanded: { type: Boolean, default: true },
429
431
  expandSearchText: { type: String, default: "展开查询" },
430
- foldSearchText: { type: String, default: "收起" },
432
+ foldSearchText: { type: String, default: "收起查询" },
431
433
  /** 为 true 且传入 `export-api` 时,在搜索区右侧显示内置导出按钮(基于 `exportFile`) */
432
434
  showExport: { type: Boolean, default: false },
433
435
  /**
@@ -447,11 +449,12 @@ export default {
447
449
  exportMaxLimit: { type: Number, default: null },
448
450
  /**
449
451
  * 未在列上写 `width` / `minWidth` 时,按表头与当前页单元格内容估算列宽;
450
- * 内容最长超过该字符数则固定按该字数算宽并开启 `showOverflowTooltip`。
452
+ * 内容最长超过该字符宽度单位数则固定按该宽度算宽并开启 `showOverflowTooltip`。
453
+ * 注:中文字符宽度为2,英文字母和数字宽度为1。
451
454
  */
452
455
  columnOverflowTooltipChars: { type: Number, default: 20 },
453
- /** 自动列宽:每字符折算像素(12px 字号下约 12~14) */
454
- columnAutoWidthCharPx: { type: Number, default: 16 },
456
+ /** 自动列宽:每字符宽度单位折算像素(12px 字号下,半角字符约8px,全角字符约16px) */
457
+ columnAutoWidthCharPx: { type: Number, default: 8 },
455
458
  /** 自动列宽:单元格左右内边距等额外宽度 */
456
459
  columnAutoWidthPadding: { type: Number, default: 32 },
457
460
  /** 自动列宽:未写 width / minWidth 时的默认最小宽度(px) */
@@ -459,7 +462,8 @@ export default {
459
462
  },
460
463
  data() {
461
464
  return {
462
- searchExpanded: false,
465
+ // searchExpanded 现在作为 prop 传入,这里只用于内部状态管理
466
+ internalSearchExpanded: this.searchExpanded,
463
467
  innerLoading: false,
464
468
  innerTableData: [],
465
469
  innerPagination: {
@@ -597,7 +601,7 @@ export default {
597
601
  displaySearchOption() {
598
602
  var max = this.effectiveSearchVisibleMax;
599
603
  var list = this.visibleSearchOptionList;
600
- if (!this.searchCollapseActive || this.searchExpanded) {
604
+ if (!this.searchCollapseActive || this.internalSearchExpanded) {
601
605
  return list;
602
606
  }
603
607
  return list.slice(0, max);
@@ -660,16 +664,23 @@ export default {
660
664
  var len = Array.isArray(list) ? list.length : 0;
661
665
  var max = this.effectiveSearchVisibleMax;
662
666
  if (!this.searchCollapseEnabled || max == null || len <= max) {
663
- this.searchExpanded = false;
667
+ this.toggleSearchExpanded(false);
664
668
  }
665
669
  },
666
670
  deep: true
667
671
  },
668
672
  searchVisibleMax() {
669
- this.searchExpanded = false;
673
+ this.toggleSearchExpanded(false);
670
674
  },
671
675
  collapseSearch(val) {
672
- if (val === false) this.searchExpanded = false;
676
+ if (val === false) this.toggleSearchExpanded(false);
677
+ },
678
+ // 监听 searchExpanded prop 的变化,同步到内部状态
679
+ searchExpanded: {
680
+ immediate: true,
681
+ handler(val) {
682
+ this.internalSearchExpanded = val;
683
+ }
673
684
  },
674
685
  columns: {
675
686
  immediate: true,
@@ -718,6 +729,11 @@ export default {
718
729
  forwardSelectionChange(selection) {
719
730
  this.$emit("selection-change", selection);
720
731
  },
732
+ toggleSearchExpanded(expanded) {
733
+ this.internalSearchExpanded = expanded;
734
+ // 使用 .sync 修饰符更新父组件的值
735
+ this.$emit("update:searchExpanded", expanded);
736
+ },
721
737
  isBuiltinCellColumn(col) {
722
738
  return col && ["selection", "index", "expand"].indexOf(col.type) !== -1;
723
739
  },
@@ -729,7 +745,75 @@ export default {
729
745
  return true;
730
746
  },
731
747
  textDisplayLength(text) {
732
- return String(text == null ? "" : text).length;
748
+ var str = String(text == null ? "" : text);
749
+ var length = 0;
750
+ for (var i = 0; i < str.length; i++) {
751
+ var charCode = str.charCodeAt(i);
752
+ // 判断字符是否为全角字符(中文字符、日文、韩文等)
753
+ // 全角字符范围:0x4E00-0x9FFF(CJK统一表意文字)
754
+ // 0x3000-0x303F(CJK符号和标点)
755
+ // 0xFF00-0xFFEF(全角ASCII、全角标点等)
756
+ if (
757
+ (charCode >= 0x4E00 && charCode <= 0x9FFF) || // 中文
758
+ (charCode >= 0x3000 && charCode <= 0x303F) || // CJK符号和标点
759
+ (charCode >= 0xFF00 && charCode <= 0xFFEF) // 全角字符
760
+ ) {
761
+ length += 2; // 全角字符宽度为2
762
+ } else {
763
+ length += 1; // 半角字符宽度为1
764
+ }
765
+ }
766
+ return length;
767
+ },
768
+ // 判断文本是否需要显示省略号
769
+ // 中文字符:超过20个字符宽度单位(约10个中文字)
770
+ // 英文字母/数字:超过40个字符宽度单位(约40个英文字母/数字)
771
+ shouldShowOverflowTooltip(text) {
772
+ var str = String(text == null ? "" : text);
773
+ var fullWidthCount = 0; // 全角字符计数
774
+ var halfWidthCount = 0; // 半角字符计数
775
+
776
+ for (var i = 0; i < str.length; i++) {
777
+ var charCode = str.charCodeAt(i);
778
+ if (
779
+ (charCode >= 0x4E00 && charCode <= 0x9FFF) || // 中文
780
+ (charCode >= 0x3000 && charCode <= 0x303F) || // CJK符号和标点
781
+ (charCode >= 0xFF00 && charCode <= 0xFFEF) // 全角字符
782
+ ) {
783
+ fullWidthCount++;
784
+ } else {
785
+ halfWidthCount++;
786
+ }
787
+ }
788
+
789
+ // 计算总宽度单位:全角字符*2 + 半角字符*1
790
+ var totalWidth = fullWidthCount * 2 + halfWidthCount;
791
+
792
+ // 如果包含全角字符,使用20个宽度单位的阈值
793
+ // 如果只有半角字符,使用40个宽度单位的阈值
794
+ var threshold = fullWidthCount > 0 ? 20 : 40;
795
+
796
+ return totalWidth > threshold;
797
+ },
798
+ // 根据字符类型获取动态阈值
799
+ getDynamicThreshold(text) {
800
+ var str = String(text == null ? "" : text);
801
+ var fullWidthCount = 0; // 全角字符计数
802
+
803
+ for (var i = 0; i < str.length; i++) {
804
+ var charCode = str.charCodeAt(i);
805
+ if (
806
+ (charCode >= 0x4E00 && charCode <= 0x9FFF) || // 中文
807
+ (charCode >= 0x3000 && charCode <= 0x303F) || // CJK符号和标点
808
+ (charCode >= 0xFF00 && charCode <= 0xFFEF) // 全角字符
809
+ ) {
810
+ fullWidthCount++;
811
+ }
812
+ }
813
+
814
+ // 如果包含全角字符,使用20个宽度单位的阈值
815
+ // 如果只有半角字符,使用40个宽度单位的阈值
816
+ return fullWidthCount > 0 ? 20 : 40;
733
817
  },
734
818
  columnOverflowThreshold() {
735
819
  var n = Number(this.columnOverflowTooltipChars);
@@ -756,6 +840,40 @@ export default {
756
840
  }
757
841
  return Math.max(labelLen, maxCell);
758
842
  },
843
+ /** 获取列中最长内容的字符串 */
844
+ getColumnMaxContentText(col) {
845
+ var labelText = (col && (col.label || col.prop || col.slot)) || "";
846
+ var maxText = labelText;
847
+ var maxLength = this.textDisplayLength(labelText);
848
+
849
+ if (col && col.children && col.children.length) {
850
+ // 对于嵌套列,返回子列中最长的内容
851
+ for (var c = 0; c < col.children.length; c++) {
852
+ var childText = this.getColumnMaxContentText(col.children[c]);
853
+ var childLength = this.textDisplayLength(childText);
854
+ if (childLength > maxLength) {
855
+ maxText = childText;
856
+ maxLength = childLength;
857
+ }
858
+ }
859
+ return maxText;
860
+ }
861
+
862
+ if (!col || col.type === "slot" || this.isBuiltinCellColumn(col)) {
863
+ return labelText;
864
+ }
865
+
866
+ var rows = this.innerTableData || [];
867
+ for (var i = 0; i < rows.length; i++) {
868
+ var text = this.resolveCellText(col, rows[i], i);
869
+ var textLength = this.textDisplayLength(text);
870
+ if (textLength > maxLength) {
871
+ maxText = text;
872
+ maxLength = textLength;
873
+ }
874
+ }
875
+ return maxText;
876
+ },
759
877
  resolveAutoColumnLayout(col) {
760
878
  if (!col) return {};
761
879
  if (col.children && col.children.length) {
@@ -765,6 +883,36 @@ export default {
765
883
  return nestedLayout;
766
884
  }
767
885
 
886
+ // slot 列不参与自动宽度计算,直接使用配置的宽度或最小宽度
887
+ if (col.type === "slot") {
888
+ var slotLayout = {};
889
+ if (col.width != null && col.width !== "") {
890
+ var userWidth = Number(col.width);
891
+ if (Number.isFinite(userWidth) && userWidth > 0) {
892
+ slotLayout.width = userWidth;
893
+ }
894
+ }
895
+ if (col.minWidth != null && col.minWidth !== "") {
896
+ var userMinWidth = Number(col.minWidth);
897
+ if (Number.isFinite(userMinWidth) && userMinWidth > 0) {
898
+ slotLayout.minWidth = userMinWidth;
899
+ }
900
+ }
901
+ // 如果既没有设置 width 也没有设置 minWidth,使用默认最小宽度
902
+ if (!slotLayout.width && !slotLayout.minWidth) {
903
+ var defaultMinW = Number(this.columnAutoMinWidth);
904
+ if (!Number.isFinite(defaultMinW) || defaultMinW <= 0) defaultMinW = 120;
905
+ slotLayout.minWidth = defaultMinW;
906
+ }
907
+ // slot 列可以设置 showOverflowTooltip
908
+ // 对于 slot 列,我们只检查用户是否显式设置了 showOverflowTooltip
909
+ // 因为 slot 列的内容是自定义的,无法自动判断是否需要省略号
910
+ if (col.showOverflowTooltip === true) {
911
+ slotLayout.showOverflowTooltip = true;
912
+ }
913
+ return slotLayout;
914
+ }
915
+
768
916
  // 获取表头和内容的最大字符长度
769
917
  var maxLen = this.getColumnMaxContentLength(col);
770
918
  var threshold = this.columnOverflowThreshold();
@@ -776,8 +924,14 @@ export default {
776
924
  if (!Number.isFinite(pad) || pad < 0) pad = 32;
777
925
 
778
926
  // 计算内容宽度(像素)
779
- // 如果内容超过阈值,使用阈值计算宽度(显示省略号)
780
- var widthChars = maxLen > threshold ? threshold : Math.max(maxLen, 1);
927
+ // 获取最长内容的字符串来判断应该使用哪个阈值
928
+ var maxContentText = this.getColumnMaxContentText(col);
929
+ var shouldShowTooltip = this.shouldShowOverflowTooltip(maxContentText);
930
+
931
+ // 根据字符类型动态调整阈值
932
+ // 如果应该显示省略号,使用动态阈值计算宽度
933
+ var dynamicThreshold = this.getDynamicThreshold(maxContentText);
934
+ var widthChars = shouldShowTooltip ? dynamicThreshold : Math.max(maxLen, 1);
781
935
  var contentWidthPx = Math.ceil(widthChars * charPx + pad);
782
936
 
783
937
  // 获取用户设置的 minWidth(如果有)
@@ -806,9 +960,12 @@ export default {
806
960
  if (col.width != null && col.width !== "") {
807
961
  var userWidth = Number(col.width);
808
962
  if (Number.isFinite(userWidth) && userWidth > 0) {
963
+ // 获取最长内容的字符串来判断是否需要显示省略号
964
+ var maxContentText = this.getColumnMaxContentText(col);
965
+ var shouldShowTooltip = this.shouldShowOverflowTooltip(maxContentText) || col.showOverflowTooltip === true;
809
966
  return {
810
967
  width: userWidth,
811
- showOverflowTooltip: maxLen > threshold || col.showOverflowTooltip === true
968
+ showOverflowTooltip: shouldShowTooltip
812
969
  };
813
970
  }
814
971
  }
@@ -818,7 +975,10 @@ export default {
818
975
  };
819
976
 
820
977
  // 判断是否需要显示 tooltip
821
- if (maxLen > threshold || col.showOverflowTooltip === true) {
978
+ // 获取最长内容的字符串来判断是否需要显示省略号
979
+ var maxContentText = this.getColumnMaxContentText(col);
980
+ var shouldShowTooltip = this.shouldShowOverflowTooltip(maxContentText) || col.showOverflowTooltip === true;
981
+ if (shouldShowTooltip) {
822
982
  layout.showOverflowTooltip = true;
823
983
  }
824
984