Skip to content

Instantly share code, notes, and snippets.

@petyosi
Created February 12, 2018 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petyosi/6ab23cc5ebd1d532d46754c79dbdf2de to your computer and use it in GitHub Desktop.
Save petyosi/6ab23cc5ebd1d532d46754c79dbdf2de to your computer and use it in GitHub Desktop.
<app-grid></app-grid>
import {Component} from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
}
<ag-grid-angular style="width: 100%; height: 800px;"
class="ag-theme-fresh"
(gridReady)="onGridReady($event)"
[columnDefs]="columnDefs"
[rowData]="rowData">
</ag-grid-angular>
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