Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide header #43

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions JsonExcel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export default {
type: String,
default: "xls"
},
'header' : {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about using showHeader?

type: Boolean,
default: true
},
// Json to download
'data':{
type: Array,
Expand Down Expand Up @@ -77,7 +81,7 @@ export default {
*/
jsonToXLS: function (data) {
let xlsTemp = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 11"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>${table}</table></body></html>'
let xlsData = '<thead><tr>'
let xlsData = '<thead>'

if( this.title != null ){
if( Array.isArray(this.title) ){
Expand All @@ -90,12 +94,13 @@ export default {
}
}

for (let key in data[0]) {
xlsData += '<th>' + key + '</th>'
if(this.header){
for (let key in data[0]) {
xlsData += '<th>' + key + '</th>'
}
xlsData += '</tr></thead>'
xlsData += '<tbody>'
}
xlsData += '</tr></thead>'
xlsData += '<tbody>'

data.map(function (item, index) {
xlsData += '<tbody><tr>'
for (let key in item) {
Expand Down Expand Up @@ -124,12 +129,14 @@ export default {
}
}

for (let key in data[0]) {
csvData += key + ','
}
if(this.header){
for (let key in data[0]) {
csvData += key + ','
}

csvData = csvData.slice(0, csvData.length - 1)
csvData += '\r\n'

}
data.map(function (item) {
for (let key in item) {
let escapedCSV = item[key] + '' // cast Numbers to string
Expand Down Expand Up @@ -161,7 +168,6 @@ export default {
}
newData.push(newItem)
})

return newData
},
getKeys: function(data, header){
Expand All @@ -175,26 +181,13 @@ export default {
}
return keys
},
callItemCallback: function(field, itemValue) {
if (typeof field === 'object' && typeof field.callback === 'function') {
return field.callback(itemValue);
}

return itemValue;
},
getNestedData: function(key, item) {
const field = (typeof key === 'object') ? key.field : key;

let valueFromNestedKey = null
let keyNestedSplit = field.split(".")

let keyNestedSplit = key.split(".")
valueFromNestedKey = item[keyNestedSplit[0]]
for (let j = 1; j < keyNestedSplit.length; j++) {
valueFromNestedKey = valueFromNestedKey[keyNestedSplit[j]]
}

valueFromNestedKey = this.callItemCallback(key, valueFromNestedKey);

return valueFromNestedKey;
},
base64ToBlob: function (data, mime) {
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Download your JSON data as an excel file directly from the browser. This component it's based on the solution proposed on this thread https://stackoverflow.com/questions/17142427/javascript-to-export-html-table-to-excel

# FIX
[20-30-2018] Thanks to @gucastiliao and @gusehr for json callback feature, now you can preproccess the data using a callback function. See the description on how to use it.
[20-03-2018] Thanks to @gucastiliao and @gusehr for json callback feature, now you can preproccess the data using a callback function. See the description on how to use it.

[02-01-2018] Thanks to @gucastiliao for json nested support, @ryatziv for the multiples fixes, @DrLongGhost for CSV data escaping, @davodaslanifakor for key header fix

Expand Down Expand Up @@ -105,12 +105,13 @@ let json_fields = {
}
```
OPTIONAL
- type: xls o csv, xls is the default value.
- type: xls or csv, xls is the default value.
- name: filename of the document you donwload.
- title: Add a title above the datagrid, you can also pass a array with differents titles like:
```js
title = ["user: 000001","USER REPORT", "Title 3"]
```
- and you can also hide header by add this prop `:header="false"`

## Export CSV
To export JSON to CSV file just add the prop type with value "csv":
Expand Down