Select specific columns in angularjs

若如初见. 提交于 2019-12-07 23:53:08

问题


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:


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}]


来源:https://stackoverflow.com/questions/30768600/select-specific-columns-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!