0

I'm using the ag-grid component in my code and would like to ensure that date columns are formatted according to the customers needs when exported as CSV. A default format for a js Date object is used currently. The code can be found here:

https://github.com/ceolter/ag-grid/blob/master/src/ts/csvCreator.ts

I could make the following change to the code directly, but this is obviously bad practice. I'm fairly new to JavaScript and was wondering if there is a standard way to extend/override functionality in a library like this.

Proposed change (Note this shows the change I made to the js version not the github version which uses ts):

--- Common/scripts/agGrid/ag-grid.js    (revision b0e7d54e61e6371b0cab94428cb4329f9f62db11)
+++ Common/scripts/agGrid/ag-grid.js    (revision )
@@ -1848,7 +1848,11 @@
+                var exportDateAs = function(dt){if (dt instanceof Date)
+                    return dt.getFullYear() + "/" + (dt.getMonth()+1) + "/" + dt.getDate();
+                };

@@ -1883,6 +1887,9 @@
+                            if (valueForCell instanceof Date){
+                                valueForCell = exportDateAs(valueForCell);
+                            }

2 Answers 2

1

It looks like this functionality was added in a newer version: https://www.ag-grid.com/javascript-grid-export/

Sign up to request clarification or add additional context in comments.

Comments

0

My solution was to update the function in the ag grid component manually as follows. It appears to work, but not sure if it is best practice. Anyone willing to comment?

Replace function in object with my own:

ag.grid.CsvCreator.prototype.getDataAsCsv = agCustom.getDataAsCsv;

New function

var agCustom;
(function (agCustom) {

    // This is a modified version of the getDataAsCsv from
    // agGrid to allow date formatting in csv export
    agCustom.getDataAsCsv = function (params) {
        var LINE_SEPARATOR = '\r\n';
...
        var exportDateAs = function(dt){if (dt instanceof Date)
            return dt.getFullYear() + "/" + (dt.getMonth()+1) + "/" + dt.getDate();
...
                else {
                    valueForCell = _this.valueService.getValue(column.colDef, node.data, node);
                    if (valueForCell instanceof Date){
                        valueForCell = exportDateAs(valueForCell);
                    }
...
})(agCustom || (agCustom = {}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.