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.
pull/2233/head
Prasoon Dwivedi 2021-06-15 22:41:33 +05:30
parent db63ee978d
commit 99f1b9d3c4
1 changed files with 40 additions and 6 deletions

View File

@ -239,7 +239,11 @@ function load() {
escapeHtml(group.name), escapeHtml(group.name),
escapeHtml(group.num_targets), escapeHtml(group.num_targets),
moment(group.modified_date).format('MMMM Do YYYY, h:mm:ss a'), moment(group.modified_date).format('MMMM Do YYYY, h:mm:ss a'),
"<div class='pull-right'><button class='btn btn-primary' data-toggle='modal' data-backdrop='static' data-target='#modal' onclick='edit(" + group.id + ")'>\ "<div class='pull-right'>\
<button class='btn btn-info' id='exportGroupButton" + group.id + "' onclick='exportGroupAsCSV(" + group.id + ',\"' + group.name +"\")'>\
<i class='fa fa-file-excel-o'></i>\
</button>\
<button class='btn btn-primary' data-toggle='modal' data-backdrop='static' data-target='#modal' onclick='edit(" + group.id + ")'>\
<i class='fa fa-pencil'></i>\ <i class='fa fa-pencil'></i>\
</button>\ </button>\
<button class='btn btn-danger' onclick='deleteGroup(" + group.id + ")'>\ <button class='btn btn-danger' onclick='deleteGroup(" + group.id + ")'>\
@ -292,3 +296,33 @@ $(document).ready(function () {
}); });
$("#csv-template").click(downloadCSVTemplate) $("#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('<i class="fa fa-spinner fa-spin"></i>')
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)
}