Angular - 使用 aG-Grid 导出模板 CSV 文件
Angular - Exporting a template CSV file using aG-Grid
在我的项目中,我已经创建了一个将当前网格数据导出到 CSV 文件的函数。我还希望能够导出一个仅包含列定义且没有数据的空 CSV 文件,以便用户可以填写数据并自行上传。我已经开始上传了,但现在我正在努力解决一个应该很容易的问题。我找不到创建模板文件的方法。我考虑过使用 exportDataAsCsv 函数:
templateExport() {
const params = {
columnKeys: ['Column1', 'Column2', 'Column3', 'Column4'],
fileName: 'table-template'
};
this.gridOptions.api.exportDataAsCsv(params);
}
我用'allColumns: true'
就可以正常导出网格数据
我还应该补充一点,我导出的列与网格中显示的不完全相同。出现在网格中的一些列不需要用户输入,因此在导出时需要跳过一些列。
有没有什么方法可以使用 exportDataAsCsv 导出空白模板,从而跳过某些列?该网页有多个相似的表格,使用此功能可能会使代码更易于实现。我知道问题可能出在 'this.gridOptions. ...'。由于 'this' 网格为空,因此没有 'data' 可导出。
我对 Angular 不是很有经验,所以如果有一些更简单的方法来做到这一点,我想念,我愿意接受建议。
@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.
你应该使用 shouldRowBeSkipped
callback:
gridOptions.api.exportDataAsCsv({
shouldRowBeSkipped: function(node, api, context) {
// skip every data row
return true;
},
});
(根据项目需要调整 gridOptions.api
)
您还可以使用columnKeys
来控制导出哪些列:
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.
在我的项目中,我已经创建了一个将当前网格数据导出到 CSV 文件的函数。我还希望能够导出一个仅包含列定义且没有数据的空 CSV 文件,以便用户可以填写数据并自行上传。我已经开始上传了,但现在我正在努力解决一个应该很容易的问题。我找不到创建模板文件的方法。我考虑过使用 exportDataAsCsv 函数:
templateExport() {
const params = {
columnKeys: ['Column1', 'Column2', 'Column3', 'Column4'],
fileName: 'table-template'
};
this.gridOptions.api.exportDataAsCsv(params);
}
我用'allColumns: true'
就可以正常导出网格数据我还应该补充一点,我导出的列与网格中显示的不完全相同。出现在网格中的一些列不需要用户输入,因此在导出时需要跳过一些列。
有没有什么方法可以使用 exportDataAsCsv 导出空白模板,从而跳过某些列?该网页有多个相似的表格,使用此功能可能会使代码更易于实现。我知道问题可能出在 'this.gridOptions. ...'。由于 'this' 网格为空,因此没有 'data' 可导出。
我对 Angular 不是很有经验,所以如果有一些更简单的方法来做到这一点,我想念,我愿意接受建议。
@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.
你应该使用 shouldRowBeSkipped
callback:
gridOptions.api.exportDataAsCsv({
shouldRowBeSkipped: function(node, api, context) {
// skip every data row
return true;
},
});
(根据项目需要调整 gridOptions.api
)
您还可以使用columnKeys
来控制导出哪些列:
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.