Search code examples
exportag-gridag-grid-angular

ag-grid export select rows by default


I'm using ag-grid (angular) and I would like to export the select rows to CSV or Excel. From what i researched on documentation this feature seems to be possible only using an external button (yellow on image) and not from the export inside of the table (red underline in image).

Is this possible to export selected line through the table itself (red underline in image)?

I'm imagining a multi selection feature in table and if I've no elements selected so the ag-grid export all data and if i've some elements selected then the ag-grid only export the selected one.

enter image description here


Solution

  • This is possible and can be achieved defining your custom function under context menu:

    • indicate that you will customise your Context Menu:
    var gridOptions = {
        columnDefs: columnDefs,
        getContextMenuItems: getContextMenuItems,
        rowSelection: 'multiple',
        ...
    };
    
    • define your action exportDataAsExcel and pass onlySelected: true param to reduce export lines:
    function getContextMenuItems(params) {
        var result = [
           {
                name: "Excel Export (.xlsx)",
                action: () => params.api.exportDataAsExcel(
                  {onlySelected: true}
                )
            },
        ];
    
        return result;
    }