Not understanding the code written in angular 7

34 views
Skip to first unread message

Akshay malvankar

unread,
Oct 7, 2019, 2:59:04 AM10/7/19
to Angular and AngularJS discussion
Hey i am little beat confused what code is actually doing following is 2 function 

downloadFile(data, filename = 'data') {
        let csvData = this.ConvertToCSV(data, [
            'name', 'age', 'average', 'approved', 'description'
        ]);
        console.log(csvData)
        let blob = new Blob(['\ufeff' + csvData], {
            type: 'text/csv;charset=utf-8;'
        });
        let dwldLink = document.createElement("a");
        let url = URL.createObjectURL(blob);
        // let isSafariBrowser = navigator.userAgent.indexOf(
        //     'Safari') != -1 & amp; & amp;
        navigator.userAgent.indexOf('Chrome') == -1;
        // /if Safari open in new window to save file with random filename. 
        // if (isSafariBrowser) {
        //     / 
        //     dwldLink.setAttribute("target", "_blank");
        // }
        dwldLink.setAttribute("href", url);
        dwldLink.setAttribute("download", filename + ".csv");
        dwldLink.style.visibility = "hidden";
        document.body.appendChild(dwldLink);
        dwldLink.click();
        document.body.removeChild(dwldLink);
    }

    ConvertToCSV(objArray, headerList) {
        var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
        var str = '';
        var row = "";

        for (var index in objArray[0]) {
            //Now convert each value to string and comma-separated
            row += index + ',';
        }
        row = row.slice(0, -1);
        //append Label row with line break
        str += row + '\r\n';

        for (var i = 0; i < array.length; i++) {
            var line = '';
            for (var index in array[i]) {
                if (line != '') line += ','

                line += array[i][index];
            }
            str += line + '\r\n';
        }
        return str;
    }
}

can anyone explain me what this 2 function is doing by line by line please

Sander Elias

unread,
Oct 7, 2019, 12:19:22 PM10/7/19
to Angular and AngularJS discussion
Hi Akshay,

There is nothing angular specific in that code.
The first function creates a link that downloads a file that is created from a string.
The second creates an csv syring from an array.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages