From 99f1b9d3c4b88a7dea6aa4163b4059762b0da38c Mon Sep 17 00:00:00 2001 From: Prasoon Dwivedi Date: Tue, 15 Jun 2021 22:41:33 +0530 Subject: [PATCH] Add feature to export group targets as CSV A new feature has been added through which targets in a group can be exported as CSV. For this a new button has been added to the group page for every group. On clicking this button for a group, group targets will be downloaded as CSV file. This will enable an admin to download group targets from one system and import them on other to create a group. --- static/js/src/app/groups.js | 46 ++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/static/js/src/app/groups.js b/static/js/src/app/groups.js index 1bf701d8..d2a58da2 100644 --- a/static/js/src/app/groups.js +++ b/static/js/src/app/groups.js @@ -239,12 +239,16 @@ function load() { escapeHtml(group.name), escapeHtml(group.num_targets), moment(group.modified_date).format('MMMM Do YYYY, h:mm:ss a'), - "
\ -
" + "
\ + \ + \ +
" ]) }) groupTable.rows.add(groupRows).draw() @@ -292,3 +296,33 @@ $(document).ready(function () { }); $("#csv-template").click(downloadCSVTemplate) }); + +// Exports group contents as a CSV file +function exportGroupAsCSV(groupId, groupName) { + var exportHTML = $("#exportGroupButton"+groupId).html() + var filename = groupName + '.csv' + var groupDetails = api.groupId.get(groupId) + if (!groupDetails) { + return + } + $("#exportGroupButton"+groupId).html('') + var csvString = Papa.unparse(groupDetails.responseJSON.targets, { + 'escapeFormulae': true + }) + + var csvData = new Blob([csvString], { + type: 'text/csv;charset=utf-8;' + }); + if (navigator.msSaveBlob) { + navigator.msSaveBlob(csvData, filename); + } else { + var csvURL = window.URL.createObjectURL(csvData); + var dlLink = document.createElement('a'); + dlLink.href = csvURL; + dlLink.setAttribute('download', filename) + document.body.appendChild(dlLink) + dlLink.click(); + document.body.removeChild(dlLink) + } + $("#exportGroupButton"+groupId).html(exportHTML) +} \ No newline at end of file