Skip to content

Commit

Permalink
excel dumps json
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed Apr 13, 2020
1 parent 56be179 commit 9b3b77e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 33 deletions.
45 changes: 29 additions & 16 deletions src/components/model/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@
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.formatter) {
a.formatter = this.genDefaultFormatter(a)
}
if (!a.useFormWidget) {
a.widget = a.widget || this.defaultWidget(a)
}
Expand Down Expand Up @@ -277,21 +279,32 @@
}
}
},
excelFormat(data){
let ds = data.map((d) => {
return this.tableItems.map((a) => {
let v = d[a.name]
if (a.choices) {
return a.choices.find(a => a.value === v).display_name
} else if (a.model) {
return d[`${a.name}_name`]
}
return v
})
})
return [this.tableItems.map((a) => a.label)].concat(ds)
genDefaultFormatter (f) {
if (f.choices) {
return (d, n, v) => f.choices.find(a => a.value === v).display_name
} else if (f.model) {
return (d, n, v) => d[`${f.name}_name`]
}
},
// excelFormat(data){
// let ds = data.map((d) => {
// console.log(this.tableItems)
// return this.tableItems.map((a) => {
// let v = d[a.name]
// if (a.choices) {
// v= a.choices.find(a => a.value === v).display_name
// } else if (a.model) {
// v = d[`${a.name}_name`]
// }
// if (a.formatter) {
// v = a.formatter(d, a.name, v)
// }
// return v
//
// })
// })
// return [this.tableItems.map((a) => a.label)].concat(ds)
// },
checkPermission(p, m){
m = m || this
return this.$store.state.user.model_permissions[m.appModel].includes(p)
Expand Down Expand Up @@ -368,7 +381,7 @@
return {
topActions,
rowActions,
excelFormat: this.excelFormat,
// excelFormat: this.excelFormat,
permissionFunction: this.checkPermission,
dblClickAction: 'edit',
title: this.model.config.verbose_name,
Expand Down
4 changes: 2 additions & 2 deletions src/components/table/DataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
let rowspan = m[rowIndex][columnIndex]
return {rowspan, colspan: rowspan > 0 ? 1 : 0}
},
genDefaultFormater (f) {
genDefaultFormatter (f) {
let df = (v) => v
let func = ['decimal', 'number', 'integer'].includes(f.type) && toThousandslsFilter || ['percent'].includes(f.type) && percent || df
return (row, column, cellValue, index) => func(cellValue)
Expand All @@ -121,7 +121,7 @@
f.align = f.align || ['decimal', 'number', 'percent', 'integer'].includes(f.type) && 'right' || 'left'
f.widget = f.widget || this.cellWidget
f.headerWidget = f.headerWidget || this.headerWidget
f.formatter = f.formatter || this.genDefaultFormater(f)
f.formatter = f.formatter || this.genDefaultFormatter(f)
if (f.subColumns) {
f.subColumns = f.subColumns.map(i => this.normalizeField(i))
}
Expand Down
57 changes: 44 additions & 13 deletions src/components/table/Table.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<el-table :data="_value" ref="table" v-loading="loading"
:element-loading-text="loading" v-on="elListeners" v-bind="elAttrs">
:element-loading-text="loading" v-on="elListeners" v-bind="elAttrs">
<slot name="left"></slot>
<column :field="f" v-for="f in _items" :key="f.name"></column>
<column :field="f" v-for="f in _items" v-if="f.hidden !== true" :key="f.name"></column>
<el-table-column label="" align="right" fixed="right"
v-if="rowActions && rowActions.length>0 || topActions && topActions.length>0">
<template slot="header" slot-scope="scope" v-if="topActions">
Expand All @@ -20,7 +20,7 @@
</el-table>
</template>
<script>
import {percent, toThousandslsFilter} from '../../utils/filters'
import {percent, toThousandslsFilter, json} from '../../utils/filters'
import {sortBy} from 'lodash'
import Column from './Column.vue'
import Actions from '../layout/Actions.vue'
Expand Down Expand Up @@ -69,9 +69,25 @@
methods: {
excelFormat(data){
let ds = data.map((d) => {
return this.fieldNames.map((a) => d[a])
let rs = []
this.fields.forEach(f => {
let fd = d[f.name]
if (f.items) {
f.items.forEach(a => {
rs.push(fd[a.name])
})
} else {
let r = d[f.name]
if (f.formatter) {
r = f.formatter(d, f.name, r)
}
rs.push(r)
}
})
return rs
})
return [this.fieldNames].concat(ds)
return [this.fieldLabels].concat(ds)
},
excelGetAllData () {
return Promise.resolve(this.value)
Expand All @@ -81,7 +97,7 @@
let excelGetAllData = this.$attrs.excelGetAllData || this.excelGetAllData
let excelFormat = this.$attrs.excelFormat || this.excelFormat
excelGetAllData().then((data) => {
if(data === undefined) {
if (data === undefined) {
this.loading = false
return
}
Expand All @@ -107,7 +123,10 @@
let rowspan = m[rowIndex][columnIndex]
return {rowspan, colspan: rowspan > 0 ? 1 : 0}
},
genDefaultFormater (f) {
genDefaultFormatter (f) {
if (f.type === 'field') {
return (row, column, cellValue, index) => json(cellValue, f.items)
}
let df = (v) => v
let func = ['decimal', 'number', 'integer'].includes(f.type) && toThousandslsFilter || ['percent'].includes(f.type) && percent || df
return (row, column, cellValue, index) => func(cellValue)
Expand All @@ -122,14 +141,14 @@
})
},
normalizeItem(f){
if(typeof f === 'string'){
f = {name:f}
if (typeof f === 'string') {
f = {name: f}
}
f.type = f.type || 'string'
f.align = f.align || ['decimal', 'number', 'percent', 'integer'].includes(f.type) && 'right' || 'left'
f.widget = f.widget || this.cellWidget
f.headerWidget = f.headerWidget || this.headerWidget
f.formatter = f.formatter || this.genDefaultFormater(f)
f.formatter = f.formatter || this.genDefaultFormatter(f)
if (f.subColumns) {
f.subColumns = f.subColumns.map(i => this.normalizeItem(i))
}
Expand Down Expand Up @@ -159,9 +178,21 @@
return this.normalizeItem(i)
})
},
fieldNames (){
let fs = flatten(this._items, 'subColumns')
return fs.map(a => a.name)
fields () {
return flatten(this._items, 'subColumns')
},
fieldNames () {
return this.fields.map(a => a.name)
},
fieldLabels () {
let rs = []
this.fields.map(f => {
let ls = f.items ? f.items : [f]
ls.forEach(a => {
rs.push(a.label || a.name)
})
})
return rs
},
_value(){
if (this.group > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/widgets/JsonDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<el-row class="json-display">
<template v-for="a in items">
<el-col :span="8" class="label">{{a.label || a.name}}</el-col>
<el-col :span="16">{{a.value}}</el-col>
<el-col :span="16">{{a.value}}&nbsp;</el-col>
</template>
</el-row>
</template>
Expand Down
16 changes: 15 additions & 1 deletion src/utils/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ export function duration (a) {
return `${m}'${s}''`
}

export function json(value, items) {
console.log(value)
if(items) {
return items.map(a => {
return `${a.label || a.name}: ${value[a.name]}`
}).join('\n')
} else {
return Object.keys(value).map(k => {
return `${k}: ${value[k]}`
}).join('\n')
}
}

export default {
dateTime,
date2now,
Expand All @@ -151,5 +164,6 @@ export default {
html2Text,
toThousandslsFilter,
percent,
duration
duration,
json
}

0 comments on commit 9b3b77e

Please sign in to comment.