Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed Dec 13, 2019
1 parent ffe91b8 commit f3f6327
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
18 changes: 12 additions & 6 deletions src/components/model/Table.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div>
<search v-model="search" :model="model" :items="searchItems" :exclude="_baseQueries" ref="search" v-if="showSearch"
<search v-model="search" :model="model" :items="searchItems" :exclude="_baseQueries" ref="search"
v-if="showSearch"
@change="onSearch"></search>
<batch-actions :items="batchActionItems" :count="selection.length" @done="refresh" :context="{selection}"
v-if="batchActionItems.length>0"></batch-actions>
Expand All @@ -17,7 +18,7 @@
</el-drawer>

<remote-table :items="tableItems" :url="model.getListUrl()" ref="table" v-if="optionLoaded"
v-bind="rtAttrs" v-on="$listeners">
v-bind="rtAttrs" v-on="$listeners" @selection-change="onSelectionChange">

<template slot="left" v-if="$slots.left">
<slot name="left"></slot>
Expand All @@ -35,6 +36,7 @@
import array_normalize from '../../utils/array_normalize'
import server_response from '../../mixins/server_response'
import Qs from 'qs'
import {get} from 'lodash'
import BatchActions from '../layout/BatchActions.vue'
import TrueFlag from '../widgets/TrueFlag.vue'
Expand All @@ -50,7 +52,7 @@
appModel: String,
items: Array,
searchItems: Array,
showSearch: true,
showSearch: {type: Boolean, default: true},
baseQueries: {
type: Object, default: () => {
return {}
Expand Down Expand Up @@ -131,7 +133,7 @@
methods: {
init () {
this.model.loadOptionsAndViewsConfig().then((data) => {
if(this.$refs.search) {
if (this.$refs.search) {
this.$refs.search.init()
}
this.parentQueries = Object.assign({}, this.getParentQueries())
Expand Down Expand Up @@ -198,12 +200,16 @@
})
let qns = Object.keys(this._baseQueries)
let orderingFields = get(this.model.options, 'actions.SEARCH.ordering_fields', [])
let rs = array_normalize(config.listItems, this.model.fieldConfigs, (a) => {
Object.assign(a, {field: this.model.fieldConfigs[a.name]})
if (!a.useFormWidget) {
a.widget = a.widget || this.defaultWidget(a)
}
if (orderingFields.includes(a.name)) {
a.sortable = true
}
return a
}).filter(a => !qns.includes(a.name))
if (this.batchActionItems.length > 0) {
Expand Down Expand Up @@ -340,17 +346,17 @@
}
})
}
let avairableActions = {...this.avairableActions, ...bactions}
return {
topActions: ['refresh', 'create', ['download'].concat(Object.keys(bactions))],
rowActions: ['edit', [this.parentMultipleRelationField ? 'removeFromParent' : 'delete']],
excelFormat: this.excelFormat,
permissionFunction: this.checkPermission,
dblClickAction: 'edit',
'selection-change': this.onSelectionChange,
title: this.model.config.verbose_name,
...this.$attrs,
baseQueries: this._baseQueries,
avairableActions: this.avairableActions
avairableActions
}
},
_baseQueries () {
Expand Down
2 changes: 1 addition & 1 deletion src/components/table/Column.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</el-table-column>
<el-table-column :type="f.type" v-else-if="f.type === 'selection'"></el-table-column>
<el-table-column :prop="f.name" :column-key="f.name" :label="f.label || f.name"
v-bind="f" :class-name="f.type" :key="f.name"
v-bind="f" :class-name="f.type" :key="f.name" :sortable="f.sortable"
v-else>

<template slot="header" slot-scope="scope">
Expand Down
40 changes: 32 additions & 8 deletions src/components/table/RemoteTable.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div v-loading="loading" :element-loading-text="loading">
<x-table :items="items" v-model="data" v-on="$listeners" v-bind="tAttrs">
<x-table :items="items" v-model="data" v-on="$listeners" v-bind="tAttrs" @sort-change="onSortChange">
<template slot="left" v-if="$slots.left">
<slot name="left"></slot>
</template>
Expand All @@ -22,13 +22,15 @@

</template>
<script>
import {DEFAULT_PAGE_SIZE, mergeOptions} from './Table'
import {DEFAULT_PAGE_SIZE, DEFAULT_MAX_PAGE_SIZE, mergeOptions} from './Table'
import Qs from 'qs'
import XTable from './Table.vue'
import server_response from '../../mixins/server_response'
import serverResponse from '../../mixins/server_response'
import array_normalize from '../../utils/array_normalize'
import queueLimit from '../../utils/async_queue'
import {range} from 'lodash'
export default{
mixins: [server_response],
mixins: [serverResponse],
props: {
items: {type: Array, default: () => []},
value: Array,
Expand All @@ -44,6 +46,7 @@
}
},
pageSize: {type: Number, default: () => DEFAULT_PAGE_SIZE},
maxPageSize: {type: Number, default: () => DEFAULT_MAX_PAGE_SIZE}
},
data () {
return {
Expand Down Expand Up @@ -95,12 +98,33 @@
this.load()
},
excelGetAllData(){
let d = Object.assign({}, this.queries, {page: 1, page_size: this.count})
this.loading = '正在获取数据'
return this.$http.get(`${this.url}?${Qs.stringify(d, {arrayFormat: 'comma'})}`).then(({data}) => {
let promise = Promise.resolve()
let page = Math.ceil(this.count/this.maxPageSize)
if(page>1) {
promise = this.$confirm(`此次导出记录数预计${this.count},超出最大限制${this.maxPageSize},基于网络性能考虑,程序会尝试分${page}页每次请求${this.maxPageSize}条记录的方式下载数据, 最终合并数据导出. 若期间后台数据有更新的话,则最终下载的数据可能会有重复或缺漏等风险,请注意检查.`,'分页导出提醒', {type:'warning', confirmButtonText:'好的,开始分页导出'})
}
let downloadFunc = (page) => {
let d = Object.assign({}, this.queries, {page, page_size: this.maxPageSize})
return this.$http.get(`${this.url}?${Qs.stringify(d, {arrayFormat: 'comma'})}`).then(({data}) => {
return data.results
})
}
let allData = []
return promise.then( () => {
this.loading = '正在获取数据'
return queueLimit(range(1, page+1),1,(p) => {
if(p>1){
this.loading = `${p}`
}
return downloadFunc(p).then(table => allData=allData.concat(table))
})
}).then(() => {
this.loading = false
return data.results
return allData
}).catch(this.onServerResponseError)
},
onSortChange ({order, prop}) {
this.updateQueries({ordering: (order == 'descending' ? '-' : '') + prop})
}
},
computed: {
Expand Down
6 changes: 4 additions & 2 deletions src/components/table/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import {mergeWith, isArray} from 'lodash'
export const DEFAULT_PAGE_SIZE = 20
export const DEFAULT_MAX_PAGE_SIZE = 1000

function customizer(objValue, srcValue) {
if (isArray(objValue)) {
Expand Down Expand Up @@ -68,11 +69,12 @@ export function csv2array(s, delimiter) {
return s.split('\n').map(l => l.split(delimiter))
}

export function guessDelimit(l, allDelimits){
allDelimits = allDelimits || [',','\t', '|']
export function guessDelimit(l, allDelimits) {
allDelimits = allDelimits || [',', '\t', '|']
return allDelimits.map((a) => {
return [l.split(a).length, a]
}).sort().reverse()[0][1]
}

export function rowIsEmpty(obj) {
return Object.values(obj).find(a => a === 0 || ![undefined, null, ''].includes(a)) === undefined
Expand Down
8 changes: 7 additions & 1 deletion src/components/table/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import Actions from '../layout/Actions.vue'
import {genSpanMap, flatten} from './Table'
import array_normalize from '../../utils/array_normalize'
import serverResponse from '../../mixins/server_response'
export default{
mixins: [serverResponse],
props: {
value: Array,
cellWidget: [Function, Object],
Expand Down Expand Up @@ -79,6 +81,10 @@
let excelGetAllData = this.$attrs.excelGetAllData || this.excelGetAllData
let excelFormat = this.$attrs.excelFormat || this.excelFormat
excelGetAllData().then((data) => {
if(data === undefined) {
this.loading = false
return
}
this.loading = '正在加载excel模块'
import('xlsx').then(XLSX => {
this.loading = '正在导出excel'
Expand All @@ -89,7 +95,7 @@
XLSX.writeFile(wb, `${this.$attrs.title || '导出数据'}.xlsx`)
this.loading = false
})
})
}).catch(this.onServerResponseError)
},
Expand Down

0 comments on commit f3f6327

Please sign in to comment.