SlickGrid functions for CSV export?

In a Django application, I use SlickGrid to bind some XHR data to a client side spreadsheet:

 var grid;
 var review_url = '/api/reviews/?t=' + current_tcode;
 $.getJSON(review_url, function(data) {
   grid = new Slick.Grid("#myGrid", data, columns, options);
 });

I would also like to give the user the ability to download data as a CSV file. What is the best way to do this?

  • Just a link to a CSV file that I create myself (using Piston, which I already use for the API).
  • Do something smart using SlickGrid to output client side CSV data.
  • Something else.

SlickGrid feels full enough so that it can have something built in for CSV output, but I cannot find anything in a quick search.

+5
source share
4 answers

SlickGrid - . , . , , .

, - . , CSV, :

1. CSV, data. json, - .

- -

2. CSV- url, , review_url.

, , , , - .

+2

To export to CSV you can use this function:

$("#exporticon").click(function() {
    var processRow = function (row) {
        var finalVal = '';
        for (var j = 0; j < row.length; j++) {
            var innerValue = row[j] === null ? '' : row[j].toString();
            if (row[j] instanceof Date) {
                innerValue = row[j].toLocaleString();
            };
            var result = innerValue.replace(/"/g, '""');
            if (result.search(/("|,|;|\n)/g) >= 0)
                result = '"' + result + '"';
            if (j > 0)
                finalVal += ',';
                finalVal += result;
        }
        return finalVal + '\n';
    };

    var csvFile = '';
    var rows = [];
    var colname = [];
    for (var j = 0, len = grid.getColumns().length; j < len; j++) {
        colname.push(grid.getColumns()[j].name);
    }
    rows.push(colname);
    var singlerow = [];
    for (var i = 0, l = dataView.getLength(); i < l; i++) {
        for (var j = 0, len = grid.getColumns().length; j < len; j++) {
            singlerow.push(grid.getDataItem(i)[grid.getColumns()[j].field]);
        }
        rows.push(singlerow);
        singlerow = [];
    }

    for (var i = 0; i < rows.length; i++) {
        csvFile += processRow(rows[i]);
    }

    var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });
    if (navigator.msSaveBlob) { // IE 10+
        navigator.msSaveBlob(blob, "filename.csv");
    } else {
        var link = document.createElement("a");
        if (link.download !== undefined) { // feature detection
            // Browsers that support HTML5 download attribute
            var url = URL.createObjectURL(blob);
            link.setAttribute("href", url);
            link.setAttribute("download", "filename.csv");
            link.style.visibility = 'hidden';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    }
});
+2
source

In my opinion, if you want only CSV - visualize it on the server.
You can also find this episode of railscasts - find tools suitable for Django for this.

0
source

All Articles