cmat 0.0.97 → 0.0.98

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/fesm2022/cmat.mjs CHANGED
@@ -4965,6 +4965,28 @@ class HttpDataPickerProvider {
4965
4965
  };
4966
4966
  }));
4967
4967
  }
4968
+ /**
4969
+ * 按 key 批量加载完整对象
4970
+ */
4971
+ loadByKeys(keys) {
4972
+ const { url, loadByUrl, method = 'post', params = {}, keysParam = 'keys', loadByKeysMapper } = this._config;
4973
+ // 使用专用的 loadByUrl 或主 url
4974
+ const targetUrl = loadByUrl ?? url;
4975
+ const requestParams = { ...params };
4976
+ requestParams[keysParam] = keys;
4977
+ const request$ = method.toLowerCase() === 'get'
4978
+ ? this._dataService.get(targetUrl, requestParams)
4979
+ : this._dataService.post(targetUrl, requestParams);
4980
+ return request$.pipe(map((res) => {
4981
+ // 如果提供了自定义映射函数,优先使用
4982
+ if (loadByKeysMapper) {
4983
+ return loadByKeysMapper(res);
4984
+ }
4985
+ // 默认提取:尝试常见格式
4986
+ const record = res;
4987
+ return (record['data'] ?? record['detail'] ?? record['items'] ?? record['list'] ?? res);
4988
+ }));
4989
+ }
4968
4990
  }
4969
4991
  /**
4970
4992
  * 创建HTTP数据提供者的工厂函数
@@ -5527,18 +5549,30 @@ class CmatDataPickerComponent {
5527
5549
  this.selectedItems.set(new Map());
5528
5550
  }
5529
5551
  else if (Array.isArray(val)) {
5530
- const newMap = new Map();
5531
- val.forEach((item) => {
5532
- const key = item[this.keyField()];
5533
- newMap.set(key, item);
5534
- });
5535
- this.selectedItems.set(newMap);
5552
+ // 检测是否为纯 key 数组(需要异步解析)
5553
+ if (val.length > 0 && this._isPrimitiveValue(val[0])) {
5554
+ this._resolveKeys(val);
5555
+ }
5556
+ else {
5557
+ const newMap = new Map();
5558
+ val.forEach((item) => {
5559
+ const key = item[this.keyField()];
5560
+ newMap.set(key, item);
5561
+ });
5562
+ this.selectedItems.set(newMap);
5563
+ }
5536
5564
  }
5537
5565
  else {
5538
- const newMap = new Map();
5539
- const key = val[this.keyField()];
5540
- newMap.set(key, val);
5541
- this.selectedItems.set(newMap);
5566
+ // 单值情况:检测是否为纯 key
5567
+ if (this._isPrimitiveValue(val)) {
5568
+ this._resolveKeys([val]);
5569
+ }
5570
+ else {
5571
+ const newMap = new Map();
5572
+ const key = val[this.keyField()];
5573
+ newMap.set(key, val);
5574
+ this.selectedItems.set(newMap);
5575
+ }
5542
5576
  }
5543
5577
  this.stateChanges.next();
5544
5578
  this._onChange(val);
@@ -5603,18 +5637,30 @@ class CmatDataPickerComponent {
5603
5637
  this.selectedItems.set(new Map());
5604
5638
  }
5605
5639
  else if (Array.isArray(value)) {
5606
- const newMap = new Map();
5607
- value.forEach((item) => {
5608
- const key = item[this.keyField()];
5609
- newMap.set(key, item);
5610
- });
5611
- this.selectedItems.set(newMap);
5640
+ // 检测是否为纯 key 数组(需要异步解析)
5641
+ if (value.length > 0 && this._isPrimitiveValue(value[0])) {
5642
+ this._resolveKeys(value);
5643
+ }
5644
+ else {
5645
+ const newMap = new Map();
5646
+ value.forEach((item) => {
5647
+ const key = item[this.keyField()];
5648
+ newMap.set(key, item);
5649
+ });
5650
+ this.selectedItems.set(newMap);
5651
+ }
5612
5652
  }
5613
5653
  else {
5614
- const newMap = new Map();
5615
- const key = value[this.keyField()];
5616
- newMap.set(key, value);
5617
- this.selectedItems.set(newMap);
5654
+ // 单值情况:检测是否为纯 key
5655
+ if (this._isPrimitiveValue(value)) {
5656
+ this._resolveKeys([value]);
5657
+ }
5658
+ else {
5659
+ const newMap = new Map();
5660
+ const key = value[this.keyField()];
5661
+ newMap.set(key, value);
5662
+ this.selectedItems.set(newMap);
5663
+ }
5618
5664
  }
5619
5665
  this.stateChanges.next();
5620
5666
  }
@@ -5830,6 +5876,38 @@ class CmatDataPickerComponent {
5830
5876
  this.value = newValue;
5831
5877
  this.selectionChange.emit(newValue);
5832
5878
  }
5879
+ /**
5880
+ * 检测值是否为原始类型(string/number),即纯 key 值
5881
+ */
5882
+ _isPrimitiveValue(val) {
5883
+ return typeof val === 'string' || typeof val === 'number';
5884
+ }
5885
+ /**
5886
+ * 异步解析纯 key 数组为完整对象
5887
+ * 调用 dataProvider.loadByKeys 获取完整数据后更新 selectedItems
5888
+ */
5889
+ _resolveKeys(keys) {
5890
+ const provider = this.dataProvider();
5891
+ if (!provider?.loadByKeys) {
5892
+ console.warn('cmat-data-picker: 传入了纯 key 值作为初始值,但 dataProvider 未实现 loadByKeys 方法。' +
5893
+ '请实现 loadByKeys 或传入完整对象。');
5894
+ return;
5895
+ }
5896
+ provider.loadByKeys(keys).subscribe({
5897
+ next: (items) => {
5898
+ const newMap = new Map();
5899
+ items.forEach((item) => {
5900
+ const key = item[this.keyField()];
5901
+ newMap.set(key, item);
5902
+ });
5903
+ this.selectedItems.set(newMap);
5904
+ this.stateChanges.next();
5905
+ },
5906
+ error: (err) => {
5907
+ console.error('cmat-data-picker: loadByKeys 解析失败', err);
5908
+ }
5909
+ });
5910
+ }
5833
5911
  /**
5834
5912
  * Format template string
5835
5913
  * 支持两种格式: