Explorar el Código

column缓存配置兼容本地加减字段更新

zhangqing hace 1 año
padre
commit
0531ae8702
Se han modificado 1 ficheros con 46 adiciones y 1 borrados
  1. 46 1
      src/mixins/tableColumnsMixin.js

+ 46 - 1
src/mixins/tableColumnsMixin.js

@@ -37,7 +37,13 @@ export default {
     async getTabColumns() {
       const res = await this.getByTableId(this.cacheKeyUrl);
       if (res?.columnConfig?.length > 0) {
-        this.setStorage(this.cacheKeyUrl + 'Cols', res.columnConfig);
+        //对比接口返回和本地columns
+        let { nlist, type } = this.columnsContrast(res.columnConfig);
+        //有更新则更新服务缓存配置
+        if (type) {
+          this.saveColumns(nlist);
+        }
+        this.setStorage(this.cacheKeyUrl + 'Cols', nlist);
         // 更新列
         if (this._computedWatchers && this._computedWatchers.columns) {
           // console.log('columns 是计算属性');
@@ -49,6 +55,45 @@ export default {
         }
       }
     },
+    //服务器和本地配置columns对比
+    columnsContrast(list) {
+      const key = 'label';
+      var updateType = 0;
+      let sList = list.filter((d, i, r) => {
+        return d[key];
+      });
+      let devColumns = this.newColumns?.length > 0 || this.columns;
+      let dList = devColumns.filter((d, i, r) => {
+        return d[key] && d[key] !== '序号';
+      });
+      const keysA = new Set(sList.map((item) => item[key]));
+      const keysB = new Set(dList.map((item) => item[key]));
+      // 本地 比 缓存服务端 多的对象(新增)
+      const added = dList.filter((item) => !keysA.has(item[key]));
+      // 本地 比 缓存服务端 少的对象(删除)
+      const removed = sList.filter((item) => !keysB.has(item[key]));
+      const removedPropSet = new Set(removed.map((item) => item[key]));
+      // 删除 缓存中 中被移除的对象
+      const keptA = list.filter((item) => !removedPropSet.has(item[key]));
+      added.forEach((item) => {
+        //新增columns字段prop参数为必填
+        if (item.prop) {
+          item.id = item.prop;
+        } else if (item.columnKey) {
+          item.id = item.columnKey;
+        } else {
+          item.prop = item.label;
+          item.id = item.label;
+        }
+        item.checked = true;
+      });
+
+      if (added.length > 0 || removed.length > 0) {
+        updateType = 1;
+      }
+      // 合并保留的对象和新增的对象
+      return { nlist: [...keptA, ...added], type: updateType };
+    },
 
     // 提交columns配置
     async saveColumns(e) {