diff --git a/portal-2/src/app/landingPages/dataProvider/dataProvider.component.html b/portal-2/src/app/landingPages/dataProvider/dataProvider.component.html index 300f6fa1..596383cf 100644 --- a/portal-2/src/app/landingPages/dataProvider/dataProvider.component.html +++ b/portal-2/src/app/landingPages/dataProvider/dataProvider.component.html @@ -33,6 +33,7 @@
  • - + {{tabs[0].name}} {{searchPublicationsComponent.totalResults}} @@ -24,7 +25,8 @@ import {RefineResultsService} from '../../services/servicesUtils/refineResuts.se
  • - + {{tab.name}} {{searchPublicationsComponent.totalResults}} @@ -38,6 +40,8 @@ import {RefineResultsService} from '../../services/servicesUtils/refineResuts.se
    +
    + @@ -54,16 +58,20 @@ import {RefineResultsService} from '../../services/servicesUtils/refineResuts.se [datasources]="datasources">
    +
    +
    + - trgfrg +
    +
    ` }) @@ -71,6 +79,7 @@ import {RefineResultsService} from '../../services/servicesUtils/refineResuts.se export class TabsComponent { @Input() id: string; + @Input() resultsBy: string; @Input() tabs: {"name": string, "content": string}[]; //@Input() publications; //@Input() datasets; @@ -78,6 +87,9 @@ export class TabsComponent { @Input() projects; @Input() datasources; + private reloadPublications: boolean = true; + private reloadDatasets: boolean = true; + subPublications: any; subDatasets: any; @@ -93,25 +105,43 @@ export class TabsComponent { } ngOnInit() { - this.subPublications = this.route.queryParams.subscribe(params => { - this.searchPublications(); - }); - - this.subDatasets = this.route.queryParams.subscribe(params => { - this.searchDatasets(); - }); + if(this.tabs != []) { + this.search(this.tabs[0].content); + } } ngOnDestroy() { - this.subPublications.unsubscribe(); - this.subDatasets.unsubscribe(); + if(this.subPublications != undefined) { + this.subPublications.unsubscribe(); + } + if(this.subDatasets != undefined) { + this.subDatasets.unsubscribe(); + } + } + + private search(content: string) { + if(content=='publicationsTab') { + this.subPublications = this.route.queryParams.subscribe(params => { + this.searchPublications(); + }); + } else if(content=='datasetsTab') { + this.subDatasets = this.route.queryParams.subscribe(params => { + this.searchDatasets(); + }); + } } private searchPublications() { - this.searchPublicationsComponent.getResultsForEntity("dataprovider", this.id, 1, 10); + if(this.reloadPublications) { + this.searchPublicationsComponent.getResultsForDataproviders(this.id, this.resultsBy, 1, 10); + } + this.reloadPublications = false; } private searchDatasets() { - this.searchDatasetsComponent.getResultsForEntity("dataprovider", this.id, 1, 10); + if(this.reloadDatasets) { + this.searchDatasetsComponent.getResultsForEntity("dataprovider", this.id, 1, 10); + } + this.reloadDatasets = false; } } diff --git a/portal-2/src/app/landingPages/organization/organization.component.html b/portal-2/src/app/landingPages/organization/organization.component.html index 1ff64376..a50a7256 100644 --- a/portal-2/src/app/landingPages/organization/organization.component.html +++ b/portal-2/src/app/landingPages/organization/organization.component.html @@ -131,18 +131,13 @@ Share - Bookmark
  • -
  • - - + +
  • + Download projects report (CSV) for {{key}}
  • -
  • - - + +
  • + Download publications report (CSV) for {{key}}
  • diff --git a/portal-2/src/app/landingPages/organization/organization.component.ts b/portal-2/src/app/landingPages/organization/organization.component.ts index bb7a679c..6d8c2515 100644 --- a/portal-2/src/app/landingPages/organization/organization.component.ts +++ b/portal-2/src/app/landingPages/organization/organization.component.ts @@ -7,8 +7,11 @@ import {OrganizationInfo} from '../../utils/entities/organizationInfo'; import { SearchDataprovidersComponent } from '../../searchPages/searchDataproviders.component'; import { SearchDataprovidersService } from '../../services/searchDataproviders.service'; import {SearchResultComponent} from '../../searchPages/searchUtils/searchResult.component'; +import { SearchPublicationsComponent } from '../../searchPages/searchPublications.component'; +import { SearchPublicationsService } from '../../services/searchPublications.service'; import {OpenaireProperties} from '../../utils/properties/openaireProperties'; +import {ExportCSVComponent} from '../../utils/exportCSV.component'; @Component({ selector: 'organization', @@ -24,7 +27,7 @@ export class OrganizationComponent { private emptyFundersSet: boolean = true; public warningMessage = ""; public errorMessage = ""; - private data : any = { "export":[] }; + private csvData: any = {}; sub: any; subDataproviders: any; @@ -32,12 +35,15 @@ export class OrganizationComponent { private searchDataprovidersComponent : SearchDataprovidersComponent; private linkToSearchDataproviders = ""; + private searchPublicationsComponent : SearchPublicationsComponent; constructor (private _organizationService: OrganizationService, private route: ActivatedRoute, - private _searchDataprovidersService: SearchDataprovidersService) { + private _searchDataprovidersService: SearchDataprovidersService, + private _searchPublicationsService: SearchPublicationsService) { console.info('organization constructor'); this.searchDataprovidersComponent = new SearchDataprovidersComponent(this.route, this._searchDataprovidersService); + this.searchPublicationsComponent = new SearchPublicationsComponent(this.route, this._searchPublicationsService); } ngOnInit() { @@ -52,16 +58,20 @@ export class OrganizationComponent { } }); +/* this.subDataprovidersCount = this.route.queryParams.subscribe(params => { - this._searchDataprovidersService.numOfEntityDataproviders(this.organizationId, "organizations/").subscribe( + this._searchDataprovidersService.numOfDataproviders("organizations/"+this.organizationId+"/datasources/count").subscribe( data => { this.searchDataprovidersComponent.totalResults = data; + console.info("this.searchDataprovidersComponent.totalResults = "+this.searchDataprovidersComponent.totalResults); }, err => { console.error(err); } ); }) +*/ + this.searchDataprovidersComponent.getNumForEntity("organization", this.organizationId); } @@ -70,7 +80,7 @@ export class OrganizationComponent { if(this.subDataproviders != undefined) { this.subDataproviders.unsubscribe(); } - this.subDataprovidersCount.unsubscribe(); + //this.subDataprovidersCount.unsubscribe(); } private getOrganizationInfo () { @@ -90,22 +100,6 @@ export class OrganizationComponent { this.organizationInfo.projects.forEach(function (value, key, map) { projectsNum += value.length; this.fundersSet.add(key); - - for(let project of value) { - this.data.export[this.data.export.length] = - { - 'Project title': this.quote(project.name), - 'Project Acronym': this.quote(project.acronym), - 'Project ID': this.quote(project.code), - 'Funder': this.quote(project.funder), - 'Funding Stream':this.quote(project.fundingStream), - 'Funding Substream level 1': this.quote(project.fundingLevel1), - 'Funding Substream level 2': this.quote(project.fundingLevel2), - 'SC39': this.quote(project.sc39), - 'Start Date': this.quote(project.startDate), - 'End Date': this.quote(project.endDate) - }; - } }.bind(this)); } this.projectsNum = projectsNum; @@ -119,7 +113,7 @@ export class OrganizationComponent { ); } - handleClick(funder: string) { + private handleClick(funder: string) { if(this.emptyFundersSet) { this.fundersSet.clear(); this.emptyFundersSet = false; @@ -141,10 +135,77 @@ export class OrganizationComponent { } } - quote(word: string): string { + private exportProjects(key: string) { + let projectsData : any = { "columnNames": [], "export":[] }; + let projectName = "Projects"+key; + + if(!(projectName in this.csvData)) { + projectsData.columnNames = ['Project title', 'Project Acronym', 'Project ID', + 'Funder', 'Funding Stream', + 'Funding Substream level 1', 'Funding Substream level 2', + 'SC39', 'Start Date', 'End Date']; + for(let project of this.organizationInfo.projects.get(key)) { + projectsData.export[projectsData.export.length] = + /*{ + 'Project title': this.quote(project.name), + 'Project Acronym': this.quote(project.acronym), + 'Project ID': this.quote(project.code), + 'Funder': this.quote(project.funder), + 'Funding Stream':this.quote(project.fundingStream), + 'Funding Substream level 1': this.quote(project.fundingLevel1), + 'Funding Substream level 2': this.quote(project.fundingLevel2), + 'SC39': this.quote(project.sc39), + 'Start Date': this.quote(project.startDate), + 'End Date': this.quote(project.endDate) + }; + */ + [ + this.quote(project.name), + this.quote(project.acronym), + this.quote(project.code), + this.quote(project.funder), + this.quote(project.fundingStream), + this.quote(project.fundingLevel1), + this.quote(project.fundingLevel2), + this.quote(project.sc39), + this.quote(project.startDate), + this.quote(project.endDate) + ]; + } + + this.csvData[projectName] = projectsData; + } + ExportCSVComponent.downloadCSV(this.csvData[projectName], "projects "+key+".csv"); + } + + private exportPublications(key: string) { + let publicationsData : any = { "columnNames": [], "export":[] }; + let publicationName = "Publications"+key; + + if(!(publicationName in this.csvData)) { + publicationsData.columnNames = ['Title', 'Authors', 'Publication Year', + 'DOI', 'Download From', 'Publication type', + 'Journal', 'Funder', 'Project Name (GA Number)', 'Access']; + + for(let project of this.organizationInfo.projects.get(key)) { + let result = this.searchPublicationsComponent.getCSVResultsForEntity("project", project.id); + //publicationsData.export.concat(["aaaa", "ooo"], ["iiii", "eeee"]]); + result.every(function(item) {publicationsData.export.push(item);}); + //console.info("publications export: "+publicationsData.export); + //console.info("publications export: "+result); + } + this.csvData[publicationName] = publicationsData; + } + ExportCSVComponent.downloadCSV(this.csvData[publicationName], "publications "+key+".csv"); + } + + private quote(word: string): string { return '"'+word+'"'; } + //private getProjectsData(key: string): any { + //return this.projectsData; + //} private searchDataproviders() { this.searchDataprovidersComponent.getResultsForEntity("organization", this.organizationId, 1, 10); diff --git a/portal-2/src/app/landingPages/project/project.component.ts b/portal-2/src/app/landingPages/project/project.component.ts index f4a36dab..1330aa40 100644 --- a/portal-2/src/app/landingPages/project/project.component.ts +++ b/portal-2/src/app/landingPages/project/project.component.ts @@ -97,20 +97,20 @@ export class ProjectComponent implements OnInit{ private searchPublications() { this.searchPublicationsComponent.getResultsForEntity("project", this.projectId, 1, 10); this.linkToSearchPublications = OpenaireProperties.getLinkToSearchPublications(); - } + } - private searchDatasets() { - this.searchDatasetsComponent.getResultsForEntity("project", this.projectId, 1, 10); - this.linkToSearchDatasets = OpenaireProperties.getLinkToSearchDatasets(); - } + private searchDatasets() { + this.searchDatasetsComponent.getResultsForEntity("project", this.projectId, 1, 10); + this.linkToSearchDatasets = OpenaireProperties.getLinkToSearchDatasets(); + } - private searchDatasetsInit() { - if(this.subDatasets == undefined && this.searchDatasetsComponent.totalResults > 0) { - this.subDatasets = this.route.queryParams.subscribe(params => { + private searchDatasetsInit() { + if(this.subDatasets == undefined && this.searchDatasetsComponent.totalResults > 0) { + this.subDatasets = this.route.queryParams.subscribe(params => { this.searchDatasets(); - }); - } - } + }); + } + } getProjectInfo (id:string) { console.info("inside getProjectInfo of component"); diff --git a/portal-2/src/app/searchPages/find/search.component.ts b/portal-2/src/app/searchPages/find/search.component.ts index af05aea0..96b0eb30 100644 --- a/portal-2/src/app/searchPages/find/search.component.ts +++ b/portal-2/src/app/searchPages/find/search.component.ts @@ -173,7 +173,7 @@ export class SearchComponent { this.subPublicationsCount.unsubscribe(); this.subDatasetsCount.unsubscribe(); this.subProjectsCount.unsubscribe(); - this.subDataprovidersCount.unsubscribe(); + //this.subDataprovidersCount.unsubscribe(); this.subOrganizationsCount.unsubscribe(); this.subPeopleCount.unsubscribe(); } @@ -289,16 +289,7 @@ export class SearchComponent { ); }) - this.subDataprovidersCount = this.route.queryParams.subscribe(params => { - this._searchDataprovidersService.numOfSearchDataproviders(this.keyword).subscribe( - data => { - this.searchDataProvidersComponent.totalResults = data; - }, - err => { - console.error(err); - } - ); - }) + this.searchDataProvidersComponent.getNumForSearch(this.keyword); this.subOrganizationsCount = this.route.queryParams.subscribe(params => { this._searchOrganizationsService.numOfSearchOrganizations(this.keyword).subscribe( diff --git a/portal-2/src/app/searchPages/searchDataproviders.component.ts b/portal-2/src/app/searchPages/searchDataproviders.component.ts index 819e81a5..2a68f603 100644 --- a/portal-2/src/app/searchPages/searchDataproviders.component.ts +++ b/portal-2/src/app/searchPages/searchDataproviders.component.ts @@ -67,6 +67,54 @@ export class SearchDataprovidersComponent { } } + public getNumForEntity(entity: string, id:string) { + console.info("getNumForEntity : Dataproviders Component"); + var parameters=""; + + if(entity == "organization") { + parameters = "organizations/"+id+"/datasources/count"; + } + + if(parameters != "") { + + this._searchDataprovidersService.numOfDataproviders(parameters).subscribe( + data => { + this.totalResults = data; + }, + err => { + console.error(err); + //TODO check erros (service not available, bad request) + // if( ){ + // this.status = ErrorCodes.ERROR; + // } + var errorCodes:ErrorCodes = new ErrorCodes(); + this.status = errorCodes.ERROR; + } + ); + } + } + + public getNumForSearch(keyword: string) { + var parameters="datasources/count"; + if(keyword != "") { + parameters += "?q="+keyword; + } + this._searchDataprovidersService.numOfDataproviders(parameters).subscribe( + data => { + this.totalResults = data; + }, + err => { + console.error(err); + //TODO check erros (service not available, bad request) + // if( ){ + // this.status = ErrorCodes.ERROR; + // } + var errorCodes:ErrorCodes = new ErrorCodes(); + this.status = errorCodes.ERROR; + } + ); + } + public getResultsForEntity(entity:string, id:string, page: number, size: number){ var parameters = ""; diff --git a/portal-2/src/app/searchPages/searchPublications.component.ts b/portal-2/src/app/searchPages/searchPublications.component.ts index 4a10fc29..21badc1e 100644 --- a/portal-2/src/app/searchPages/searchPublications.component.ts +++ b/portal-2/src/app/searchPages/searchPublications.component.ts @@ -96,8 +96,6 @@ export class SearchPublicationsComponent { } - - console.info(params); if(params[filter.filterId] != undefined) { let values = params[filter.filterId].split(","); @@ -147,6 +145,29 @@ export class SearchPublicationsComponent { } */ +public getCSVResultsForEntity(entity:string, id:string): any { + let exportPublications = []; + exportPublications.push(//[exportPublications.length] = + [ + this.quote("bla"), + this.quote("bla"), + this.quote("bla"), + this.quote("bla"), + this.quote("bla"), + this.quote("bla"), + this.quote("bla"), + this.quote("bla"), + this.quote("bla"), + this.quote("blaaaaa") + ] + ); + return exportPublications; +} + +private quote(word: string): string { + return '"'+word+'"'; +} + public getResultsForEntity(entity:string, id:string, page: number, size: number){ var parameters = ""; @@ -154,8 +175,6 @@ public getResultsForEntity(entity:string, id:string, page: number, size: number) parameters = "projects/"+id; } else if(entity == "person") { parameters = "people/"+id; - } else if(entity == "dataprovider") { - parameters = "datasources/"+id; } if(parameters != "") { @@ -185,6 +204,41 @@ public getResultsForEntity(entity:string, id:string, page: number, size: number) } } +public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){ + var parameters; + if(resultsFrom == "collectedFrom") { + parameters = "publications?"+resultsFrom+"="+id+"&cl=and"; + } else if(resultsFrom == "hostedBy") { + parameters = "publications?"+resultsFrom+"="+id+"&hs=and"; + } + + if(parameters != "") { + + this._searchPublicationsService.searchPublicationsForDataproviders(parameters, page, size).subscribe( + data => { + this.totalResults = data[0]; + console.info("search Datasets for Dataproviders: [Parameters:"+parameters+" ] [total results:"+this.totalResults+"]"); + this.results = data[1]; + + var errorCodes:ErrorCodes = new ErrorCodes(); + this.status = errorCodes.DONE; + if(this.totalResults == 0 ){ + this.status = errorCodes.NONE; + } + }, + err => { + console.error(err); + //TODO check erros (service not available, bad request) + // if( ){ + // this.status = ErrorCodes.ERROR; + // } + var errorCodes:ErrorCodes = new ErrorCodes(); + this.status = errorCodes.ERROR; + } + ); + } +} + public getResults(keyword:string,refine:boolean, page: number, size: number){ var parameters = ""; if(keyword.length > 0){ diff --git a/portal-2/src/app/services/dataProvider.service.ts b/portal-2/src/app/services/dataProvider.service.ts index 982aa107..30fdfd9a 100644 --- a/portal-2/src/app/services/dataProvider.service.ts +++ b/portal-2/src/app/services/dataProvider.service.ts @@ -64,6 +64,12 @@ export class DataProviderService { if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) { this.dataProviderInfo.tabs.push({"name": "Datasources", "content": "datasourcesTab"}); } + + if(this.dataProviderInfo.resultTypes.collectedFrom.has(data[1].classid)) { + this.dataProviderInfo.resultsBy = "collectedFrom"; + } else if(this.dataProviderInfo.resultTypes.hostedBy.has(data[1].classid)) { + this.dataProviderInfo.resultsBy = "hostedBy"; + } } if(data[2] != null) { diff --git a/portal-2/src/app/services/organization.service.ts b/portal-2/src/app/services/organization.service.ts index b927ef3c..0dd0a6ce 100644 --- a/portal-2/src/app/services/organization.service.ts +++ b/portal-2/src/app/services/organization.service.ts @@ -71,7 +71,7 @@ export class OrganizationService { if(mydata['funding'].hasOwnProperty("funder")) { if(this.organizationInfo.projects == undefined) { this.organizationInfo.projects = new Map(); @@ -79,7 +79,7 @@ export class OrganizationService { if(!this.organizationInfo.projects.has(mydata['funding']['funder'].name)) { this.organizationInfo.projects.set(mydata['funding']['funder'].name, - new Array<{ "name": string, "url": string, "code": string, + new Array<{ "name": string, "id": string, "url": string, "code": string, "acronym": string, "funder": string, "fundingStream": string, "fundingLevel1": string, "fundingLevel2": string, "sc39": string, "startDate": string, "endDate": string }>()); @@ -87,13 +87,14 @@ export class OrganizationService { counter = this.organizationInfo.projects.get(mydata['funding']['funder'].name).length; this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter] = - { "name": "", "url": "", "code": "", + { "name": "", "id": "", "url": "", "code": "", "acronym": "", "funder": "", "fundingStream": "", "fundingLevel1": "", "fundingLevel2": "", "sc39": "", "startDate": "", "endDate": "" }; let url = ""; if(mydata['to'].content != null && mydata['to'].content != "") { + this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['id'] = mydata['to'].content; url = OpenaireProperties.getsearchLinkToProject()+mydata['to'].content; } this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['name'] = mydata.title; diff --git a/portal-2/src/app/services/searchDataproviders.service.ts b/portal-2/src/app/services/searchDataproviders.service.ts index 8a3a2225..73f2fd40 100644 --- a/portal-2/src/app/services/searchDataproviders.service.ts +++ b/portal-2/src/app/services/searchDataproviders.service.ts @@ -99,22 +99,12 @@ export class SearchDataprovidersService { return results; } - numOfEntityDataproviders(id: string, entity: string):any { + numOfDataproviders(params: string):any { + console.info("getOfDataproviders : Dataproviders Service + params="+params); //OpenaireProperties.getSearchAPIURL() //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/" - let url = OpenaireProperties.getSearchAPIURL()+entity+id+"/datasources/count" - - return this.http.get(url) - .map(res => res.json()) - .map(res => res.total); - } - - numOfSearchDataproviders(params: string):any { - - //OpenaireProperties.getSearchAPIURL() - //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/" - let url = OpenaireProperties.getSearchAPIURL()+"datasources/count?q="+params; + let url = OpenaireProperties.getSearchAPIURL()+params; return this.http.get(url) .map(res => res.json()) diff --git a/portal-2/src/app/services/searchPublications.service.ts b/portal-2/src/app/services/searchPublications.service.ts index 228ede10..6d320762 100644 --- a/portal-2/src/app/services/searchPublications.service.ts +++ b/portal-2/src/app/services/searchPublications.service.ts @@ -40,6 +40,15 @@ export class SearchPublicationsService { .map(res => [res['meta'].total, this.parseResults(res['results'])]); } + searchPublicationsForDataproviders(params: string, page: number, size: number):any { + let link = OpenaireProperties.getSearchAPIURL(); + let url = link+params; + return this.http.get(url) + .map(res => res.json()) + .map(res => [res['meta'].total, this.parseResults(res['results'])]); + } + + parseResults(data: any): SearchResult[] { let results: SearchResult[] = []; diff --git a/portal-2/src/app/utils/entities/dataProviderInfo.ts b/portal-2/src/app/utils/entities/dataProviderInfo.ts index 336ba831..4de012bf 100644 --- a/portal-2/src/app/utils/entities/dataProviderInfo.ts +++ b/portal-2/src/app/utils/entities/dataProviderInfo.ts @@ -40,6 +40,28 @@ export class DataProviderInfo { "datasourcesTab": new Set(["entityregistry::repositories"]) }; + resultsBy: string; + resultTypes = { + "collectedFrom": new Set( + [ "aggregator::datarepository", + "aggregator::pubsrepository::institutional", + "aggregator::pubsrepository::unknown", + "aggregator::pubsrepository::journals", + "entityregistry::projects", + "infospace", + "scholarcomminfra", + "pubscatalogue::unknown" + ]), + "hostedBy": new Set( + [ "crissystem", + "datarepository::unknown", + "pubsrepository::institutional", + "pubsrepository::journal", + "pubsrepository::unknown", + "pubsrepository::thematic" + ]) + }; + organizations: {"name": string, "url": string}[]; //publications: any; datasets: any; diff --git a/portal-2/src/app/utils/entities/organizationInfo.ts b/portal-2/src/app/utils/entities/organizationInfo.ts index db6a9509..47d57671 100644 --- a/portal-2/src/app/utils/entities/organizationInfo.ts +++ b/portal-2/src/app/utils/entities/organizationInfo.ts @@ -3,7 +3,7 @@ export class OrganizationInfo { name: string; country: string; - projects: Map; diff --git a/portal-2/src/app/utils/exportCSV.component.ts b/portal-2/src/app/utils/exportCSV.component.ts index d2eb5316..de640394 100644 --- a/portal-2/src/app/utils/exportCSV.component.ts +++ b/portal-2/src/app/utils/exportCSV.component.ts @@ -4,7 +4,7 @@ import {Component, Input} from '@angular/core'; selector: 'export', template: ` + (click)="downloadCSV(data, filename)"> {{linkname}} @@ -18,24 +18,47 @@ import {Component, Input} from '@angular/core'; export class ExportCSVComponent { stockData : any = { + "columnNames": + ["Symbol", "Company", "Price"], "export": - [ - { - Symbol: "AAPL", - Company: "Apple Inc.", - Price: "132.54" - }, - { - Symbol: "INTC", - Company: "Intel Corporation", - Price: "33.45" - }, - { - Symbol: "GOOG", - Company: "Google Inc", - Price: "554.52" - }, - ], +/* + [ + { + Symbol: "AAPL", + Company: "Apple Inc.", + Price: "132.54" + }, + { + Symbol: "INTC", + Company: "Intel Corporation", + Price: "33.45" + }, + { + Symbol: "GOOG", + Company: "Google Inc", + Price: "554.52" + }, + ], +*/ + + [ + [ + "AAPL", + "Apple Inc.", + "132.54" + ], + [ + "INTC", + "Intel Corporation", + "33.45" + ], + [ + "GOOG", + "Google Inc", + "554.52" + ], + ], + "columnDelimiter": ',', "lineDelimiter": '\n' @@ -52,12 +75,13 @@ export class ExportCSVComponent { ngOnInit() { } - convertArrayOfObjectsToCSV(args) { + public static convertArrayOfObjectsToCSV(args) { console.info("convertArrayOfObjectsToCSV"); var result, ctr, keys, columnDelimiter, lineDelimiter, data; data = args.export || null; + if (data == null || !data.length) { return null; } @@ -65,12 +89,13 @@ export class ExportCSVComponent { columnDelimiter = args.columnDelimiter || ','; lineDelimiter = args.lineDelimiter || '\n'; - keys = Object.keys(data[0]); + //keys = Object.keys(data[0]); + keys = args.columnNames; result = ''; result += keys.join(columnDelimiter); result += lineDelimiter; - +/* data.forEach(function(item) { ctr = 0; keys.forEach(function(key) { @@ -80,28 +105,39 @@ export class ExportCSVComponent { }); result += lineDelimiter; }); +*/ + + for(let line of data) { + ctr = 0; + for(let column of line) { + if (ctr > 0) result += columnDelimiter; + result += column; + ctr++; + } + result += lineDelimiter; + } return result; } - downloadCSV() { + public static downloadCSV(data: any, filenameArg: string) { console.info("downloadCSV"); - var data, filename, link; + var encodedData, filename, link; - var csv = this.convertArrayOfObjectsToCSV(this.data); + var csv = this.convertArrayOfObjectsToCSV(data); if (csv == null) return; - filename = this.filename || 'export.csv'; + filename = filenameArg || 'export.csv'; if (!csv.match(/^data:text\/csv/i)) { csv = 'data:text/csv;charset=utf-8,' + csv; } - data = encodeURI(csv); + encodedData = encodeURI(csv); //link = document.createElement('a'); link = document.getElementsByTagName('a'); - link[0].setAttribute('href', data); + link[0].setAttribute('href', encodedData); link[0].setAttribute('download', filename); //document.body.appendChild(link); link[0].click();