From a36dc2bb64378ea8e893d55b3f41fe49199ac8d7 Mon Sep 17 00:00:00 2001 From: Bhuridech Sudsee Date: Mon, 2 Apr 2018 23:05:51 +0700 Subject: [PATCH 1/4] Update JsonExcel.vue add prop to simply hide header --- JsonExcel.vue | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/JsonExcel.vue b/JsonExcel.vue index bb7fc7c..6863fec 100644 --- a/JsonExcel.vue +++ b/JsonExcel.vue @@ -18,6 +18,10 @@ export default { type: String, default: "xls" }, + 'header' : { + type: Boolean, + default: true + }, // Json to download 'data':{ type: Array, @@ -77,7 +81,7 @@ export default { */ jsonToXLS: function (data) { let xlsTemp = ' ${table}
' - let xlsData = '' + let xlsData = '' if( this.title != null ){ if( Array.isArray(this.title) ){ @@ -90,12 +94,13 @@ export default { } } - for (let key in data[0]) { - xlsData += '' + key + '' + if(this.header){ + for (let key in data[0]) { + xlsData += '' + key + '' + } + xlsData += '' + xlsData += '' } - xlsData += '' - xlsData += '' - data.map(function (item, index) { xlsData += '' for (let key in item) { @@ -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 @@ -161,7 +168,6 @@ export default { } newData.push(newItem) }) - return newData }, getKeys: function(data, header){ @@ -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) { From d5376869e63c22686cead52687c35cfe70d20c94 Mon Sep 17 00:00:00 2001 From: Bhuridech Sudsee Date: Mon, 2 Apr 2018 23:09:10 +0700 Subject: [PATCH 2/4] Update README.md fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index af2fa9d..926e43e 100644 --- a/README.md +++ b/README.md @@ -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 From baaead1a5f8e77f1542cafca0ca7db79f4064a14 Mon Sep 17 00:00:00 2001 From: Bhuridech Sudsee Date: Mon, 2 Apr 2018 23:11:02 +0700 Subject: [PATCH 3/4] Update README.md fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 926e43e..9ce87cf 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ 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 From 8f0a21e970a44862685def27a7ca00c02c5718ea Mon Sep 17 00:00:00 2001 From: Bhuridech Sudsee Date: Mon, 2 Apr 2018 23:33:59 +0700 Subject: [PATCH 4/4] Update README.md add readme for hide header --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9ce87cf..5aad63a 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ OPTIONAL - type: xls or csv, xls is the default value. - name: filename of the document you donwload. - title: Add a title above the datagrid +- 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":