Brian C. Peters
Brian C. Peters

Reputation: 18

Angular - Exporting a template CSV file using aG-Grid

In my project I've already created a function which will export the current grid data to a CSV file. I'd also like to have the ability to export an empty CSV file containing only the column definitions and NO data, so that a user may fill in data and upload it themselves. I've already gotten the upload to work, but am now struggling with what feels like should be an easy problem. I can't find a way to create a template file. I considered using the exportDataAsCsv function:

      templateExport() {
        const params = {
          columnKeys: ['Column1', 'Column2', 'Column3', 'Column4'],
          fileName: 'table-template'
        };
        this.gridOptions.api.exportDataAsCsv(params);
      }

I export the grid data normally by just using 'allColumns: true'

I should also add that the columns I'm exporting are not exactly the same as what shows in the grid. Some columns that appear in the grid will not need to be entered by the user, so some will need to be skipped in the export.

Is there some way I can use exportDataAsCsv to export a blank template which will skip certain columns? The webpage has multiple tables which are all similar, and using this function could potentially make the code much simpler to implement. I understand the problem is probably coming from the 'this.gridOptions. ...'. Since 'this' grid is empty, there is no 'data' to export.

I'm not very experienced with Angular, so if there is some easier way to do this that I'm missing, I'm open to suggestions.

Upvotes: 0

Views: 4129

Answers (1)

thirtydot
thirtydot

Reputation: 228182

@thirtydot My question is about whether it is possible to use ag-grid's export functionality to export a CSV file with only column headers and no data.

You should use the shouldRowBeSkipped callback:

gridOptions.api.exportDataAsCsv({
    shouldRowBeSkipped: function(node, api, context) {
        // skip every data row
        return true;
    },
});

(adjust gridOptions.api to whatever is needed in your project)

You can also use columnKeys to control which columns are exported:

columnKeys: Provide a list (an array) of column keys if you want to export specific columns.
shouldRowBeSkipped: Allows you to skip entire rows from the export.

Upvotes: 2

Related Questions