Select specific columns in angularjs

3.7k views Asked by At

I am working on a webproject. It is using angularjs on clientside. One of the requirement is to display some data in a grid and give options to export the data into csv file.

I am trying to use ngCsv module for this functionality. I display data in the grid from a collection. While displaying I have control on what columns I can display. But while exporting the data to CSV all that I can use is the collection. When I use the collection it is exporting all the columns from the collection in the exported csv files. But I want to export only specific columns. There may be a way to select only specific columns from the collection in angularjs but I could not find a way to do it.

This is the function that loads apps on the controller

function loadApps() 
{
    appService.getApps().then(function (result) {
    vm.assets = result.data.items;});
}

This function is used to export the apps

vm.exportApps = function () {
        return vm.apps.filter(function (a) {
            return a.selected;
        });
    }

This is is from html

<button class="btn btn-sm btn-primary" ng-csv="vm.exportApps" filename="SelectedAppsList.csv">Export Apps </button>

Can somebody suggest as to what I can do to just select specific columns to export ?

1

There are 1 answers

1
romko_bomko On

You can try to use csv-column-order attribute in your html code.

<button class="btn btn-sm btn-primary" ng-csv="vm.exportApps" csv-column-order="vm.columns" filename="SelectedAppsList.csv">Export Apps </button>

and specify what columns do you need

vm.columns = function () {
    return ['column1', 'column2', 'column3'];
};

they are from your array of objects

vm.apps = [{column1:10, column2:12, column3:13}, {column1:14, column2:15, column3:16}]