Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search: add button to export results #212

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion projects/ng-core-tester/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,27 @@ export class HomeComponent {
key: 'documents',
label: 'Documents',
component: DocumentComponent
,
exportFormats: [
{
label: 'JSON',
format: 'json'
}
],
},
{
key: 'organisations',
label: 'Organisations'
label: 'Organisations',
exportFormats: [
{
label: 'JSON',
format: 'json'
},
{
label: 'CSV',
format: 'csv'
}
],
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ <h5 *ngIf="types.length == 1 && showLabel">
<i class="fa fa-plus mr-0 mr-sm-1"></i>
<span class="d-none d-sm-inline">{{ 'Add' | translate }}</span>
</a>
<a role="button" class="btn btn-outline-primary ml-2" *ngIf="exportFormats && exportFormats.length == 1"
[href]="getExportFormatUrl(exportFormats[0].format)" >
<i class="fa fa-download mr-1"></i> {{ 'Export as' | translate }} {{ exportFormats[0].label }}
</a>
<div class="btn-group ml-2" dropdown *ngIf="exportFormats && exportFormats.length > 1">
<button
id="button-export-basic"
dropdownToggle
type="button"
class="btn btn-outline-primary dropdown-toggle"
>
<i class="fa fa-download mr-1"></i> {{ 'Export as' | translate }} ... <span class="caret"></span>
</button>
<ul
id="dropdown-export-basic"
*dropdownMenu
class="dropdown-menu"
role="menu"
aria-labelledby="button-export-basic"
>
<li role="menuitem" *ngFor="let format of exportFormats">
<a class="dropdown-item" [href]="getExportFormatUrl(format.format)" >{{ format.label }}</a>
</li>
</ul>
</div>
<div class="btn-group ml-2" dropdown>
<button
id="button-basic"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ActionStatus } from '../action-status';
import { RecordUiService } from '../record-ui.service';
import { RecordService } from '../record.service';
import { AggregationsFilter, RecordSearchService } from './record-search.service';
import { ApiService } from '../../api/api.service';

@Component({
selector: 'ng-core-record-search',
Expand Down Expand Up @@ -172,13 +173,15 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
* @param _recordSearchService Record search service.
* @param _translateService Translate service.
* @param _spinner Spinner service.
* @param _apiService Api service.
*/
constructor(
private _recordService: RecordService,
private _recordUiService: RecordUiService,
private _recordSearchService: RecordSearchService,
private _translateService: TranslateService,
private _spinner: NgxSpinnerService
private _spinner: NgxSpinnerService,
private _apiService: ApiService
) { }

/**
Expand Down Expand Up @@ -455,6 +458,35 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
return null;
}

/**
* Get Export formats for the current resource given by configuration.
* @return Array of export format to generate an `export as` button or an empty array.
*/
get exportFormats(): Array<any> {
if (this._config && this._config.exportFormats) {
return this._config.exportFormats;
}
return [];
}

/**
* Get export format url.
* @param format - string, export format
* @return formatted url for an export format.
*/
getExportFormatUrl(format: string) {
const baseUrl = this._apiService.getEndpointByType(this.currentType);
let url = `${baseUrl}?&format=${format}&size=${RecordService.MAX_REST_RESULTS_SIZE}`;
if (this.aggregationsFilters) {
this.aggregationsFilters.map(filter => {
filter.values.map(v => {
url += `&${filter.key}=${v}`;
});
});
}
return url;
}

/**
* Aggregations order (facets)
* @param aggr - Aggregations dictonary
Expand Down