Would want to Extract Data from Columns of a Grid - Java Script

拟墨画扇 提交于 2021-01-29 14:38:39

问题


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 [Data from the console[2]

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!