@yibozhang/pro-table 16.1.1 → 16.1.2

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,6 +1,6 @@
1
1
  import * as i0 from '@angular/core';
2
2
  import { InjectionToken, Component, Input, ContentChild, Directive, Optional, HostListener, forwardRef, Inject, EventEmitter, Output, ViewChild, ViewContainerRef, TemplateRef, ContentChildren, NgModule, Injectable } from '@angular/core';
3
- import { isEqual, hasIn, cloneDeep, isNil, debounce } from 'lodash-es';
3
+ import { hasIn, cloneDeep, isNil, debounce } from 'lodash-es';
4
4
  import { isObservable, lastValueFrom } from 'rxjs';
5
5
  import * as i1 from '@angular/common';
6
6
  import { CommonModule } from '@angular/common';
@@ -753,8 +753,6 @@ class ProTableComponent {
753
753
  _sortPriority = [];
754
754
  // 存储每个 autoComplete 字段的防抖函数
755
755
  _autoCompleteDebounceMap = new Map();
756
- // 仅在初始化时运行列配置预检查,避免后续调用重复触发
757
- _preCheckDone = false;
758
756
  _columns = [];
759
757
  // 筛选表单数据存储
760
758
  _searchParams = {};
@@ -842,85 +840,9 @@ class ProTableComponent {
842
840
  // 注意:此时所有 ContentChild 和 ContentChildren 都已初始化完成
843
841
  this.autoRegisterTemplates();
844
842
  }
845
- async preCheckServerColumns() {
846
- const res = await this.fetchRemoteColumns();
847
- if (!res)
848
- return false;
849
- const initColumns = this.columns
850
- .filter((col) => !col.auxiliaryColumn)
851
- .map((column) => {
852
- return {
853
- field: column.prop,
854
- isShow: !column.hideInTable,
855
- header: column.title,
856
- sortName: column.sortName || null,
857
- };
858
- });
859
- if (res.code === 0) {
860
- const serverColumns = Array.isArray(res?.data?.tabColumn)
861
- ? res.data.tabColumn
862
- : [];
863
- // 以远程数据为主:保留远程顺序与显示状态,仅做字段结构合并(新增字段追加、删除字段过滤)
864
- const merged = this.mergeServerColumnsWithInit(serverColumns, initColumns);
865
- if (!isEqual(serverColumns, merged)) {
866
- const persistRes = await this.persistRemoteColumns(merged);
867
- if (persistRes) {
868
- this.queryDynamicColumns();
869
- }
870
- return false;
871
- }
872
- return true;
873
- }
874
- else if (res.code === 10) {
875
- // code === 10 表示未初始化,需要继续执行后续逻辑
876
- return true;
877
- }
878
- else {
879
- return false;
880
- }
881
- }
882
- mergeServerColumnsWithInit(serverColumns, initColumns) {
883
- const initByField = new Map();
884
- initColumns.forEach((c) => initByField.set(c.field, c));
885
- const merged = [];
886
- const seen = new Set();
887
- // 1) 保留远程顺序与显示状态;同步 header/sortName;过滤掉前端已不存在的 field
888
- serverColumns.forEach((sc) => {
889
- const field = sc?.field;
890
- if (!field || !initByField.has(field) || seen.has(field))
891
- return;
892
- const init = initByField.get(field);
893
- merged.push({
894
- ...sc,
895
- header: init.header,
896
- sortName: init.sortName ?? null,
897
- });
898
- seen.add(field);
899
- });
900
- // 2) 追加前端新增但远程未存储的字段,使用前端默认配置(默认显示/默认标题等)
901
- initColumns.forEach((ic) => {
902
- const field = ic?.field;
903
- if (!field || seen.has(field))
904
- return;
905
- merged.push({
906
- field,
907
- isShow: ic.isShow,
908
- header: ic.header,
909
- sortName: ic.sortName ?? null,
910
- });
911
- seen.add(field);
912
- });
913
- return merged;
914
- }
915
843
  async queryDynamicColumns() {
916
844
  if (!this.tableName || !this.columnRemote)
917
845
  return;
918
- if (!this._preCheckDone) {
919
- const checkResult = await this.preCheckServerColumns();
920
- this._preCheckDone = true;
921
- if (!checkResult)
922
- return;
923
- }
924
846
  const res = await this.fetchRemoteColumns();
925
847
  if (!res)
926
848
  return;
@@ -933,22 +855,19 @@ class ProTableComponent {
933
855
  hideInTable: !item.isShow,
934
856
  };
935
857
  });
936
- this._serverColumns = _tabColumn;
937
- this.updateTableColumns(_tabColumn);
858
+ this._serverColumns = _tabColumn.filter((col) => this.columns.some((localCol) => localCol.prop === col.prop));
859
+ this.updateTableColumns(this._serverColumns);
938
860
  }
939
861
  else if (res.code === 10) {
940
862
  // 如果未设置接口返回,需要默认调用一次修改函数初始化数据
941
863
  const initColumns = this.columns
942
864
  .filter((col) => !col.auxiliaryColumn)
943
- .map((column) => {
944
- return {
945
- field: column.prop,
946
- isShow: true,
947
- header: column.title,
948
- sortName: column.sortName || null,
949
- };
950
- });
951
- // 调用修改接口初始化数据
865
+ .map((column) => ({
866
+ field: column.prop,
867
+ isShow: true,
868
+ header: column.title,
869
+ sortName: column.sortName || null,
870
+ }));
952
871
  const initRes = await this.persistRemoteColumns(initColumns);
953
872
  if (initRes) {
954
873
  this.queryDynamicColumns();
@@ -1140,63 +1059,21 @@ class ProTableComponent {
1140
1059
  this.setSelectRequest(this._searchFiledColumns);
1141
1060
  }
1142
1061
  updateTableColumns(injectColumns) {
1143
- // 1. 使用最原始的columns作为数据源,确保辅助列完全不受影响
1144
- const originalColumns = cloneDeep(this.columns);
1145
- // 2. 处理字段映射(如果提供了fields参数)
1146
- let processedInjectColumns = injectColumns;
1147
- // 3. 分离辅助列和非辅助列(使用原始数据)
1148
- const nonAuxiliaryColumns = originalColumns.filter((col) => !col.auxiliaryColumn);
1149
- // 4. 按照injectColumns的顺序重新排列非辅助列
1150
- const reorderedNonAuxiliaryColumns = [];
1151
- const remainingNonAuxiliaryColumns = [...nonAuxiliaryColumns]; // 复制一份用于查找和移除
1152
- processedInjectColumns.forEach((injectCol) => {
1153
- // 查找非辅助列中对应的下标
1154
- const existingColIndex = remainingNonAuxiliaryColumns.findIndex((col) => col.prop === injectCol.prop);
1155
- if (existingColIndex !== -1) {
1156
- // 如果找到相同列,用injectColumns中的属性覆盖原始columns中的属性
1157
- const existingCol = remainingNonAuxiliaryColumns[existingColIndex];
1158
- const updatedCol = { ...existingCol, ...injectCol };
1159
- reorderedNonAuxiliaryColumns.push(updatedCol);
1160
- // 从剩余数组中移除,避免重复
1161
- remainingNonAuxiliaryColumns.splice(existingColIndex, 1);
1162
- }
1163
- else {
1164
- // 如果没找到对应的列,直接添加injectColumns中的列
1165
- reorderedNonAuxiliaryColumns.push(injectCol);
1166
- }
1167
- });
1168
- // 5. 处理剩余的原始columns中的非辅助列(在injectColumns中未出现的列)
1169
- // 这些列应该被隐藏(设置hideInTable为true)
1170
- remainingNonAuxiliaryColumns.forEach((remainingCol) => {
1171
- const hiddenCol = { ...remainingCol, hideInTable: true };
1172
- reorderedNonAuxiliaryColumns.push(hiddenCol);
1173
- });
1174
- // 6. 按照原始columns的顺序重新组合:保持辅助列的原始位置和属性
1175
- const finalColumns = [];
1176
- let nonAuxiliaryIndex = 0;
1177
- let auxiliaryIndex = 0;
1178
- originalColumns.forEach((originalCol) => {
1179
- if (originalCol.auxiliaryColumn) {
1180
- // 如果是辅助列,使用原始列数据,完全不进行任何修改
1181
- finalColumns.push(originalCol);
1182
- auxiliaryIndex++;
1183
- }
1184
- else {
1185
- // 如果是非辅助列,使用重新排序后的列,但保留原始列的所有属性
1186
- const reorderedCol = reorderedNonAuxiliaryColumns[nonAuxiliaryIndex];
1187
- if (reorderedCol) {
1188
- // 合并原始列和重新排序后的列,确保保留所有原始属性
1189
- const mergedCol = { ...originalCol, ...reorderedCol };
1190
- finalColumns.push(mergedCol);
1191
- }
1192
- else {
1193
- // 如果重新排序后的列不存在,使用原始列
1194
- finalColumns.push(originalCol);
1195
- }
1196
- nonAuxiliaryIndex++;
1197
- }
1062
+ // 远程列直接作为来源,过滤掉本地不存在的列,并从本地列补充渲染配置
1063
+ const remoteColumns = injectColumns
1064
+ .filter((injectCol) => this.columns.some((col) => col.prop === injectCol.prop))
1065
+ .map((injectCol) => {
1066
+ const localCol = this.columns.find((col) => col.prop === injectCol.prop);
1067
+ return { ...localCol, ...injectCol };
1198
1068
  });
1199
- this.formateInnerColumns(finalColumns);
1069
+ // 辅助列取自本地,保持在原始位置(首部 / 尾部)
1070
+ const firstNonAuxIndex = this.columns.findIndex((c) => !c.auxiliaryColumn);
1071
+ const lastNonAuxIndex = this.columns.length -
1072
+ 1 -
1073
+ [...this.columns].reverse().findIndex((c) => !c.auxiliaryColumn);
1074
+ const headAuxiliary = this.columns.filter((col, i) => col.auxiliaryColumn && i < firstNonAuxIndex);
1075
+ const tailAuxiliary = this.columns.filter((col, i) => col.auxiliaryColumn && i > lastNonAuxIndex);
1076
+ this.formateInnerColumns([...headAuxiliary, ...remoteColumns, ...tailAuxiliary]);
1200
1077
  }
1201
1078
  // 通过 Proxy 监听 _searchParams 的属性变更
1202
1079
  createSearchParamsProxy(target) {