Forráskód Böngészése

feat(table): manual paging when using singlePage

Casper Dai 3 éve
szülő
commit
0332401605

+ 0 - 1
src/components/table/GridTable/index.vue

@@ -79,7 +79,6 @@
         :item-render="itemRender"
       />
       <pagination
-        v-if="needPagination"
         :total="options.totalCount"
         :page.sync="options.params.pageNum"
         :limit.sync="options.params.pageSize"

+ 0 - 1
src/components/table/Table/index.vue

@@ -88,7 +88,6 @@
       />
     </el-table>
     <pagination
-      v-if="needPagination"
       :total="options.totalCount"
       :page.sync="options.params.pageNum"
       :limit.sync="options.params.pageSize"

+ 22 - 26
src/components/table/mixins/table.js

@@ -70,8 +70,8 @@ export default {
         }
       }
     },
-    needPagination () {
-      return !this.schema.singlePage
+    isManualPagination () {
+      return this.schema.singlePage
     }
   },
   watch: {
@@ -111,9 +111,9 @@ export default {
       return {
         loading: false,
         error: false,
-        params: { ...(this.needPagination ? { pageSize: this.pageSize, pageNum: 1 } : null), ...condition },
-        list: [],
-        totalCount: 0
+        params: { pageSize: this.pageSize, pageNum: 1, ...condition },
+        totalCount: 0,
+        list: []
       }
     },
     filterInvokes (data) {
@@ -128,23 +128,24 @@ export default {
           ({ data, totalCount }) => {
             options.loading = false
             options.error = false
-            data = transform ? data.map(transform) : data
-            if (this.needPagination) {
-              if (data.length === 0) {
-                const { pageNum, pageSize } = options.params
-                if (pageNum > 1) {
-                  return this.pageTo(totalCount ? Math.ceil(totalCount / pageSize) : 1)
-                }
+            options.totalCount = totalCount || data.length
+            if (this.isManualPagination) {
+              const { pageNum, pageSize } = options.params
+              data = data.slice((pageNum - 1) * pageSize, pageNum * pageSize)
+            }
+            if (data.length === 0) {
+              const { pageNum, pageSize } = options.params
+              if (pageNum > 1) {
+                return this.pageTo(totalCount ? Math.ceil(totalCount / pageSize) : 1)
               }
             }
-            options.list = data
-            options.totalCount = totalCount || data.length
+            options.list = transform ? data.map(transform) : data
           },
           () => {
             options.loading = false
             options.error = true
-            options.list = []
             options.totalCount = 0
+            options.list = []
           }
         )
       }
@@ -177,10 +178,7 @@ export default {
       return this.options.params
     },
     mergeCondition (condition) {
-      const params = { ...this.options.params, ...condition }
-      if (this.needPagination) {
-        params.pageNum = 1
-      }
+      const params = { ...this.options.params, pageNum: 1, ...condition }
       if (this.options.loading) {
         this.options = this.createOptions(params)
       } else {
@@ -214,17 +212,15 @@ export default {
       this.onChange()
     },
     pageTo (pageNum) {
-      if (this.needPagination && pageNum >= 1) {
+      if (pageNum >= 1) {
         return this.mergeCondition({ pageNum })
       }
-      this.mergeCondition()
+      this.mergeCondition({ pageNum: this.options.params.pageNum })
     },
     decrease (count) {
-      if (this.needPagination) {
-        const options = this.options
-        if (options.list.length <= count) {
-          return this.pageTo(options.params.pageNum - 1)
-        }
+      const options = this.options
+      if (options.list.length <= count) {
+        return this.pageTo(options.params.pageNum - 1)
       }
       this.pageTo()
     }

+ 13 - 4
src/views/device/group/index.vue

@@ -147,15 +147,23 @@ export default {
       })
     },
     onViewDevices ({ id }) {
+      this.$devices = null
       this.$refs.subDeviceDialog.show({ id })
     },
     getDevicesByGroup ({ id }) {
-      return getDevicesByGroup(id)
+      if (this.$devices) {
+        return Promise.resolve({ data: this.$devices })
+      }
+      return getDevicesByGroup(id).then(({ data }) => {
+        this.$devices = data
+        return { data }
+      })
     },
-    onDelDevice (device, index) {
+    onDelDevice (device) {
       const table = this.$refs.subDeviceDialog.getTable()
       deleteDeviceFromGroup(table.getCondition().id, device).then(() => {
-        table.getData().splice(index, 1)
+        this.$devices = null
+        table.decrease(1)
       })
     },
     onAddDevice () {
@@ -165,7 +173,8 @@ export default {
       const table = this.$refs.subDeviceDialog.getTable()
       addDeviceToGroup(table.getCondition().id, id).then(() => {
         done()
-        table.pageTo()
+        this.$devices = null
+        table.pageTo(1)
       })
     }
   }