Browse Source

add export method to model

master
Bing Sun 5 years ago
parent
commit
8a04d92ecb
Signed by: sunb GPG Key ID: F7795F8C590626AB
2 changed files with 38 additions and 40 deletions
  1. +0
    -40
      src/postgrest.js
  2. +38
    -0
      src/util/make_model.js

+ 0
- 40
src/postgrest.js View File

@@ -1,43 +1,3 @@
// utilities
// generate downloadable csv
const download = (options={}) => {
options.filename = options.filename || 'file'

if (options.type == 'csv') {
let headers = Object.keys(options.data[0])

let body = options.data.map(row => headers.map(key => row[key]).join(',')).join('\n')

options.data = '\ufeff' + headers.join(',') + '\n' + body
options.type = 'text/csv'
} else if (options.type == 'json') {
}

let blob = new Blob([options.data], {type: options.type})
let url = URL.createObjectURL(blob)

if (options.timestamp)
options.filename = options.filename + (new Date()).toLocaleTimeString(undefined, {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false
})

let anchor = document.createElement('a')
anchor.href = url
anchor.target = '_blank'
anchor.download = `${options.filename}.csv`

anchor.click()

URL.revokeObjectURL(url)
anchor.remove()
}

// authentication model
const _make_auth = api_root => {



+ 38
- 0
src/util/make_model.js View File

@@ -208,6 +208,44 @@ export default (options={}) => {
})

return _promise
},

export(options={}) {
options.filename = options.filename || _configs.endpoint
let _data = this.data()

if (options.type == 'csv') {
let headers = _configs.selects && _configs.selects.map(select => select.alias || select.label) || Object.keys(_data[0])

let body = _data.map(row => headers.map(key => row[key]).join(',')).join('\n')

_data = '\ufeff' + headers.join(',') + '\n' + body
options.type = 'text/csv'
}

let blob = new Blob([_data], {type: options.type})
let url = URL.createObjectURL(blob)

if (options.timestamp)
options.filename = options.filename + (new Date()).toLocaleTimeString(undefined, {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false
})

let anchor = document.createElement('a')
anchor.href = url
anchor.target = '_blank'
anchor.download = `${options.filename}.csv`

anchor.click()

URL.revokeObjectURL(url)
anchor.remove()
}
}



Loading…
Cancel
Save