(888) 685-3101 ext. 2

Sometimes you just want to make a form as client side as possible. Below is an example of how to export an Editable Grid’s data to CSV. This can be done from a quickform. This is just an “On Client Click” action for a Standard Button control.

dojo.byId("TabControl_element_myFormName_element_view_myFormName_myFormName_btnExportCSV").disabled = true; // disable the button
var grid = dijit.byId("myFormNamegrdGrid"); // grab the grid control
var where = grid.contextualCondition(); // grab our where clause
var select = []; // get the columns without the ID field. you may need to filter this more depending on your data
for (var i = 0; i 5000) { // don't let the user export too much data
    alert("To prevent database performance problems, you may only export 5000 records.");
    maxRecords = 5000;
}
var sdataUrl = dojo.string.substitute('slxdata.ashx/slx/dynamic/-/myEntity?format=json&count=${0}&where=${1}&select=${2}', [maxRecords, where, select.join(',')]);
// query sdata using the above information
dojo.xhrGet({
    url: sdataUrl,
    preventCache: true,
    handleAs: 'json',
    scope: this,
    load: function (response) {
        var csv = [];
        csv.push('"' + select.join('","') + '"'); // header row
        for (var i = 0; i < response.$resources.length; i++) {
            var currentRecord = response.$resources[i];
            var csvLine = [];
            for (var j = 0; j < select.length; j++) {
                if (("" + currentRecord[select[j]]).indexOf("/Date") == 0) // if it is a date field, format it like a date
                    csvLine.push(Sage.Utility.Convert.toDateFromString(currentRecord[select[j]]).toLocaleDateString().replace(/u200e/g, '')); // removing a strange unicode character that gets added under certain conditions
                else
                    csvLine.push(("" + currentRecord[select[j]]).replace('"', '""')); // adding double quotes to preserve CSV formatting
            }
            csv.push('"' + csvLine.join('","') + '"');
        }

        try {
            if (navigator.msSaveOrOpenBlob) { // for IE, use this
                var csvContent = csv.join('n');
                var fileData = [csvContent];
                var blobObject = new Blob(fileData);
                window.navigator.msSaveOrOpenBlob(blobObject, "export.csv");
            } else { // else, try this
                var csvContent = "data:text/csv;charset=utf-8," + csv.join('n');
                var encodedUri = encodeURI(csvContent);
                var link = document.createElement("a");
                link.setAttribute("href", encodedUri);
                link.setAttribute("download", "export.csv");
                link.click();
            }
        } catch (e) { // if we get an error the user probably isn't on a modern web browser...
            alert("The export to CSV feature requires Internet Explorer 10+, Chrome or Firefox.");
        }
        dojo.byId("TabControl_element_myFormName_element_view_myFormName_myFormName_btnExportCSV").disabled = false; // enable the button
    }
});

Share This