问题
I have the below data pushing from a Query we are passing. I have successfully passed the Query into a CSV but we need data to be extracted from Columns rather than Query. Below is a pseudo code how I would want to extract data.
The data which is coming out of the Query in a Console is [
The output I am receiving in the Excel sheet is
The red Highlighted header in the excel sheet is the field in the Query, But I need the Name of from the column. Below is the code i have written to achieve this
var exportGrid = function() {
var headerRow = '';
var headers = [];
var str = '';
var row ='';
for (var i = 0; i < data.length; i++) {
var line = '';
for (var index in data[i]) {
if(((typeof data[i][index] == "object" && data[i][index]==null)||(typeof data[i][index] != "object")) && (index.indexOf('type')==-1)) {
if(i==0){
if(!headers[index]) {
headers.push(index);
if(headerRow != '') headerRow += ',';
headerRow += index;
}}
if(line != '')
line += ',';
line += '"' + data[i][index] + '"';
}
}
str += line + '\r\n';
}
var finalStr = headerRow + '\r\n' + str;
//To Extract the CSV
var csv=finalStr;
var downloadLink=document.createElement("a");
var blob=new Blob(["\ufeff",csv]);
var url=URL.createObjectURL(blob);
downloadLink.href=url;
downloadLink.download="data.csv";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
The above function is taking all the values I am passing through my Query but I only need those Columns which are getting populated from the Grid. As the Pseudo code, I would want to write two for oops where One loops through Headers and One Extracts Data from the Columns.
Thanks
来源:https://stackoverflow.com/questions/60668978/would-want-to-extract-data-from-columns-of-a-grid-java-script