Skip to content
This repository was archived by the owner on Aug 21, 2018. It is now read-only.

Commit

Permalink
Merge branch 'Survey' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
SabaBaig committed Apr 4, 2016
2 parents 4c952c3 + 9fe695d commit d8b209c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
81 changes: 81 additions & 0 deletions app/nation/app/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3291,6 +3291,87 @@ $(function() {
});
},

downloadCommunitySurveys: function (surveyNo, communityNames) {
if(communityNames.length > 0) {
alert(surveyNo);
communityNames = communityNames.split(',');
$.ajax({
url:'/surveyresponse/_design/bell/_view/surveyResBySurveyNo?include_docs=true',
type: 'GET',
dataType: 'json',
async: false,
success: function (json) {
console.log(json);
var jsonRows = json.rows;
var surveyResModels = [];
for(var i = 0 ; i < jsonRows.length ; i++) {
if(jsonRows[i].value.SurveyNo == surveyNo && communityNames.indexOf(jsonRows[i].value.communityName) > -1) {
surveyResModels.push(jsonRows[i].value);
}
}
console.log(surveyResModels);
},
error: function (err) {
console.log(err);
}
});
} else {
alert("No survey responses yet to download");
}
},

JSONToCSVConvertor: function (JSONData, ReportTitle, label) {
//If JSONData is not an object then JSON.parse will parse the JSON string in an Object
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var CSV = '';
//Set Report title in first row or line
CSV += label + '\r\n\n';
//This will generate the Label/Header
var row = "";
//This loop will extract the label from 1st index of on array
for (var index in arrData[0]) {
//Now convert each value to string and comma-seprated
row += index + ',';
}
row = row.slice(0, -1);
//append Label row with line break
CSV += row + '\r\n';
//1st loop is to extract each row
for (var i = 0; i < arrData.length; i++) {
var row = "";
//2nd loop will extract each column and convert it in string comma-seprated
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
//add a line break after each row
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
//Generate a file name
var fileName = "";
//this will remove the blank-spaces from the title and replace it with an underscore
fileName += ReportTitle.toString().replace(/ /g,"_");
//Initialize file format you want csv or xls
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
// Now the little tricky part.
// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = fileName + ".csv";
//this part will append the anchor tag and remove it after automatic click
this.$el.append(link);
link.click();
},

underConstruction: function() {
App.$el.children('.body').html('<div id="underConstruction" style="margin:0 auto"><h4>This Functionality is under construction.</h4></div>')
},
Expand Down
5 changes: 3 additions & 2 deletions app/nation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,9 @@ <h3><%=languageDict.attributes.Surveys %>
<a href="#surveydetail/<%= _id %>" class="btn btn-warning">
<%= languageDict.attributes.Details %>
</a>
</td>
<td>
<a class='btn btn-info' onclick="App.Router.downloadCommunitySurveys('<%= SurveyNo %>', '<%= submittedBy %>')">
Download in CSV
</a>
<a class='btn btn-danger destroy'>
<%= languageDict.attributes.DeleteLabel %>
</a>
Expand Down

0 comments on commit d8b209c

Please sign in to comment.