Browse Source

表格列缓存机制

yuliang 3 years ago
parent
commit
349acab801
3 changed files with 46 additions and 4 deletions
  1. 2 0
      src/Defaults.ts
  2. 13 2
      src/Scope.ts
  3. 31 2
      src/controls/grid.js

+ 2 - 0
src/Defaults.ts

@@ -138,6 +138,8 @@ export const grid = {
     hideExport: false,
     hideAutoSize: false,
     hideClearFilter: false,
+    hideSaveGridUIConfig: false,
+    hideClearGridUIConfig: false,
     autoLoad: true,
     remoteSort: false,
     remoteFilter: false,

+ 13 - 2
src/Scope.ts

@@ -117,7 +117,7 @@ export class Scope {
         this.viewModel = new Ext.app.ViewModel(vmodel);
         this.viewModel.yvanScope = this
         this._applyWatchList()
-
+        this["scopeKey"] = "dialog-" + getVjsonHash(JSON.stringify(this.vjson) + JSON.stringify(vjsonOption))
         const config = _.defaultsDeep({
             animateTarget: sender,
             viewModel: this.viewModel,
@@ -183,6 +183,7 @@ export class Scope {
         this.viewModel.yvanScope = this
         this._applyWatchList()
 
+        this["scopeKey"] = "page-" + getVjsonHash(JSON.stringify(this.vjson) + JSON.stringify(vjsonOption))
         // 根级不能设置id
         delete that.vjson.id
 
@@ -228,7 +229,7 @@ export class Scope {
         const vmodel = _.defaultsDeep({
             data: {}
         }, that.model, dataOption)
-
+        this["scopeKey"] = "render-" + getVjsonHash(JSON.stringify(this.vjson) + JSON.stringify(vjsonOption))
         this.viewModel = new Ext.app.ViewModel(vmodel);
         this.viewModel.yvanScope = this
         this._applyWatchList()
@@ -343,4 +344,14 @@ export function watch(tplExpress, deep = false) {
         target._addWatch({bindTo: tplExpress, deep}, target[propertyKey])
         return target[propertyKey]
     }
+}
+
+// 获取vjson的hash值
+function getVjsonHash(str: string): string {
+    let hash =   3465217896,i,ch;
+    for (i = str.length - 1; i >= 0; i--) {
+        ch = str.charCodeAt(i);
+        hash ^= ((hash << 5) + ch + (hash >> 2));
+    }
+    return  (hash & 0x7FFFFFFF) + "";
 }

+ 31 - 2
src/controls/grid.js

@@ -28,7 +28,6 @@ export default function () {
         constructor(config) {
             const me = this
             const {dataSource} = config
-
             this.columnConfigCacheKey = this.makeColumnConfigCacheKey(config)
             if (Array.isArray(config.columns) && config.columns.length > 0) {
                 const cacheData = this.getColumnConfigCache()
@@ -44,6 +43,7 @@ export default function () {
                                     column.width = itData.width
                                 }
                                 column.hidden = itData.hidden
+                                column.locked = itData.locked
                                 newColumns.push(column)
                                 break
                             }
@@ -100,6 +100,22 @@ export default function () {
                     handler: this.clearFilter
                 })
             }
+            if (!newConfig.hideSaveGridUIConfig) {
+                buttons.push({
+                    xtype: 'button',
+                    tooltip: '保存表格自定义配置',
+                    iconCls: 'x-fa fa-save',
+                    handler: this.saveGridUIConfig
+                })
+            }
+            if (!newConfig.hideClearGridUIConfig) {
+                buttons.push({
+                    xtype: 'button',
+                    tooltip: '清空表格自定义配置',
+                    iconCls: 'x-fa fa-close',
+                    handler: this.clearGridUIConfig
+                })
+            }
 
             if (!newConfig.hideFootbar) {
                 if (newConfig.pagination) {
@@ -414,7 +430,8 @@ export default function () {
         // 生成列自定义的缓存key
         makeColumnConfigCacheKey(config) {
             const me = this
-            let key = "gridColumnCache-" + window.location.href
+            const scope = config.$initParent.yvanScope || config.$initParent.lookupReferenceHolder().yvanScope;
+            let key = "gridColumnCache-" + scope.scopeKey + "-"
             if (config.reference) {
                 key += config.reference
             } else {
@@ -451,6 +468,7 @@ export default function () {
                         dataIndex: column.dataIndex,
                         width: column.width,
                         hidden: column.hidden,
+                        locked: column.locked,
                         index
                     })
                     index++
@@ -477,6 +495,17 @@ export default function () {
             sender.up('grid').filters.clearFilters();
         },
 
+        saveGridUIConfig(sender) {
+            const grid = sender.up('grid')
+            grid.setColumnConfigCache()
+        },
+
+        clearGridUIConfig(sender) {
+            const grid = sender.up('grid')
+            const key = grid.columnConfigCacheKey
+            localStorage.setItem(key, "")
+        },
+
         setLoading(value) {
             if (value) {
                 this.mask('读取中')