Skip to content

Commit

Permalink
feat: export data tables to json or csv file
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Jan 8, 2021
1 parent e351c90 commit 0cbea9d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/renderer/components/ModalSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
<h4>{{ appName }}</h4>
<p>
{{ $t('word.version') }}: {{ appVersion }}<br>
<a class="c-hand" @click="openOutside('https://github.com/Fabio286/antares')">GitHub</a><br>
<a class="c-hand" @click="openOutside('https://github.com/Fabio286/antares')">GitHub</a> | <a class="c-hand" @click="openOutside('https://github.com/Fabio286/antares/blob/master/CHANGELOG.md')">CHANGELOG</a><br>
<small>{{ $t('word.author') }}: <a class="c-hand" @click="openOutside('https://github.com/Fabio286')">Fabio Di Stasio</a></small><br>
<small>{{ $t('message.madeWithJS') }}</small>
</p>
Expand Down
10 changes: 10 additions & 0 deletions src/renderer/components/WorkspaceQueryTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@

<script>
import { uidGen } from 'common/libs/uidGen';
import arrayToFile from '../libs/arrayToFile';
import { LONG_TEXT, BLOB } from 'common/fieldTypes';
import BaseVirtualScroll from '@/components/BaseVirtualScroll';
import WorkspaceQueryTableRow from '@/components/WorkspaceQueryTableRow';
Expand Down Expand Up @@ -354,6 +355,15 @@ export default {
},
selectResultset (index) {
this.resultsetIndex = index;
},
downloadTable (format, filename) {
if (!this.sortedResults) return;
arrayToFile({
type: format,
content: this.sortedResults,
filename
});
}
}
};
Expand Down
39 changes: 35 additions & 4 deletions src/renderer/components/WorkspaceTableTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,30 @@
</div>
</div>
</div>
<button
class="btn btn-dark btn-sm"
@click="showAddModal"
>

<button class="btn btn-dark btn-sm" @click="showAddModal">
<span>{{ $t('word.add') }}</span>
<i class="mdi mdi-24px mdi-playlist-plus ml-1" />
</button>
<div class="dropdown export-dropdown">
<button
:disabled="isQuering"
class="btn btn-dark btn-sm dropdown-toggle mr-0 pr-0"
tabindex="0"
>
<span>{{ $t('word.export') }}</span>
<i class="mdi mdi-24px mdi-file-export ml-1" />
<i class="mdi mdi-24px mdi-menu-down" />
</button>
<ul class="menu text-left">
<li class="menu-item">
<a class="c-hand" @click="downloadTable('json')">JSON</a>
</li>
<li class="menu-item">
<a class="c-hand" @click="downloadTable('csv')">CSV</a>
</li>
</ul>
</div>
</div>
<div class="workspace-query-info">
<div v-if="results.length && results[0].rows">
Expand Down Expand Up @@ -222,7 +239,21 @@ export default {
this.reloadTable();
}, this.autorefreshTimer * 1000);
}
},
downloadTable (format) {
this.$refs.queryTable.downloadTable(format, this.table);
}
}
};
</script>
<style lang="scss" scoped>
.export-dropdown {
.menu {
min-width: 100%;
.menu-item a:hover {
background: $bg-color-gray;
}
}
}
</style>
3 changes: 2 additions & 1 deletion src/renderer/i18n/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ module.exports = {
parameters: 'Parameters',
function: 'Function | Functions',
deterministic: 'Deterministic',
context: 'Context'
context: 'Context',
export: 'Export'
},
message: {
appWelcome: 'Welcome to Antares SQL Client!',
Expand Down
37 changes: 37 additions & 0 deletions src/renderer/libs/arrayToFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const arrayToFile = args => {
let mime;
let content;

switch (args.type) {
case 'csv': {
mime = 'text/csv';
const csv = [];

if (args.content.length)
csv.push(Object.keys(args.content[0]).join(';'));

for (const row of args.content)
csv.push(Object.values(row).map(col => typeof col === 'string' ? `"${col}"` : col).join(';'));

content = csv.join('\n');
break;
}
case 'json':
mime = 'application/json';
content = JSON.stringify(args.content, null, 3);
break;

default:
break;
}

const file = new Blob([content], { mime });
const downloadLink = document.createElement('a');
downloadLink.download = `${args.filename}.${args.type}`;
downloadLink.href = window.URL.createObjectURL(file);
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
downloadLink.click();
};

export default arrayToFile;
13 changes: 1 addition & 12 deletions src/renderer/store/modules/workspaces.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,7 @@ export default {
uid,
connected: false,
selected_tab: 0,
tabs: [{
uid: 'data',
type: 'table',
fields: [],
keyUsage: []
},
{
uid: 'prop',
type: 'table',
fields: [],
keyUsage: []
}],
tabs: [],
structure: {},
variables: [],
collations: [],
Expand Down

0 comments on commit 0cbea9d

Please sign in to comment.