Created
February 12, 2018 11:31
-
-
Save petyosi/6ab23cc5ebd1d532d46754c79dbdf2de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<app-grid></app-grid> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from '@angular/core'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ag-grid-angular style="width: 100%; height: 800px;" | |
class="ag-theme-fresh" | |
(gridReady)="onGridReady($event)" | |
[columnDefs]="columnDefs" | |
[rowData]="rowData"> | |
</ag-grid-angular> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, OnInit} from '@angular/core'; | |
import {ColDef, ColumnApi, GridApi} from 'ag-grid'; | |
import {AthleteService} from '../services/athlete.service'; | |
import {Athlete} from '../model/athlete.model'; | |
@Component({ | |
selector: 'app-grid', | |
templateUrl: './grid.component.html', | |
styleUrls: ['./grid.component.css'] | |
}) | |
export class GridComponent implements OnInit { | |
// row data and column definitions | |
private rowData: Athlete[]; | |
private columnDefs: ColDef[]; | |
// gridApi and columnApi | |
private api: GridApi; | |
private columnApi: ColumnApi; | |
// inject the athleteService | |
constructor(private athleteService: AthleteService) { | |
this.columnDefs = this.createColumnDefs(); | |
} | |
// on init, subscribe to the athelete data | |
ngOnInit() { | |
this.athleteService.findAll().subscribe( | |
athletes => { | |
this.rowData = athletes | |
}, | |
error => { | |
console.log(error); | |
} | |
) | |
} | |
// one grid initialisation, grap the APIs and auto resize the columns to fit the available space | |
onGridReady(params): void { | |
this.api = params.api; | |
this.columnApi = params.columnApi; | |
this.api.sizeColumnsToFit(); | |
} | |
// create some simple column definitions | |
private createColumnDefs() { | |
return [ | |
{field: 'id'}, | |
{field: 'name'}, | |
{field: 'country', valueGetter: (params) => params.data.country.name}, | |
{field: 'results', valueGetter: (params) => params.data.results.length} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment