问题
Trying to select first row by default in ag-grid. As per ag-grid documents, I should be able to do this using NodeSelection Api(https://www.ag-grid.com/javascript-grid-selection/?framework=all#gsc.tab=0). But I am not able to get to node object at all. HTML file
<div class="pulldown panel panel-default">
<div class="panel-heading">{{rulesSummaryTitle}}</div>
<ag-grid-angular #agGrid class="ag-fresh ag-bootstrap"
[gridOptions]="gridOptions"
[rowData]="rowData"
[columnDefs]="columnDefs"
[enableSorting]="true"
rowSelection="single"
[pagination]="true"
[suppressCellSelection]="true"
(gridReady)="onGridReady($event)"
(rowSelected)="onRowSelect($event)">
</ag-grid-angular>
</div>
I am calling node selection api in "onGridReady" method but errors out with error message "cant call setSelected on undefined".
public onGridReady(event: any): void {
event.api.sizeColumnsToFit();
this.gridOptions.api.node.setSelected(true);
}
回答1:
There isn't a node
attribute on the gridOptions.api
object. You will want to do something more like this:
public onGridReady(event: any): void {
event.api.sizeColumnsToFit();
gridOptions.api.forEachNode(node=> node.rowIndex ? 0 : node.setSelected(true))
}
This will check over each node in the data and see if the rowIndex is 0, when it is, it uses the node object to set the selected attribute
回答2:
Found solution, problem was with "onGridReady" function being called well before row data being populated from Observables. So there were actually no rows that select statement could select.
import { Component } from '@angular/core';
import {Observable} from "rxjs/Observable";
export class Hero {
id: number;
name: string;
}
@Component({
selector: 'my-app',
template: `
<ag-grid-angular style="width: 500px; height: 115px;" class="ag-fresh"
[rowData]="rowData"
(gridReady)="onReady($event)"
[columnDefs]="columnDefs">
</ag-grid-angular>
<ag-grid-angular style="width: 500px; height: 115px;" class="ag-fresh"
[rowData]="rowData2"
(gridReady)="onReady($event)"
[columnDefs]="columnDefs">
</ag-grid-angular>
<ag-grid-angular style="width: 500px; height: 115px;" class="ag-fresh"
[gridOptions]="gridOptions"
[rowData]="rowData3"
(gridReady)="onReady($event)"
(rowDataChanged)="onRowDataChanged()"
[columnDefs]="columnDefs">
</ag-grid-angular>
`
})
export class AppComponent {
columnDefs;
rowData;
rowData2;
rowData3;
constructor() {
this.gridOptions = {
rowData: this.rowData3
};
console.log("in here");
console.log("in here");
console.log("in here");
this.columnDefs = [
{headerName: "Make", field: "make"},
{headerName: "Model", field: "model"},
{headerName: "Price", field: "price"}
];
this.rowData = [
{make: "Toyota", model: "Celica", price: 35000},
{make: "Ford", model: "Mondeo", price: 32000},
{make: "Porsche", model: "Boxter", price: 72000}
]
let val = [
{make: "Toyota", model: "Celica", price: 35000},
{make: "Ford", model: "Mondeo", price: 32000},
{make: "Porsche", model: "Boxter", price: 72000}
];
let res : Observable<any> = Observable.create(observer => {
setTimeout(()=>{
observer.next(val);
},1);
});
res.subscribe(
resposne => {
this.rowData2 = resposne;
});
let res1 : Observable<any> = Observable.create(observer => {
setTimeout(()=>{
observer.next(val);
},2000);
});
res1.subscribe(
resposne => {
this.rowData3 = resposne;
});
}
/**
* Select the first row as default...
*/
public onRowDataChanged(): void {
this.gridOptions.api.forEachNode(node => node.rowIndex ? 0 : node.setSelected(true));
}
private onReady(params) {
params.api.sizeColumnsToFit();
params.api.forEachNode(node => node.rowIndex ? 0 : node.setSelected(true));
}
}
https://plnkr.co/edit/PSKnSjAo6omDAo5bjm1J.
Added plunker with three ag-grid. First grid has predefined rowdata values, second grid has rowdata is being populated from Observables but with very less latency, third grid has row data values being populated from observables with higher latency. In case of first and second "onGridReady" function gets called and first row get selected, but in case of third grid we have to provide select row statement on to "rowDataChanged" event for row to be selected.
回答3:
On onGridReady, you can use this code for selecting first row by default.
let rowIndex = 0;
this.GridOptions.api.paginationGoToFirstPage(); // If pagination is implemented
this.GridOptions.api.selectIndex(rowIndex, false, false);
this.GridOptions.api.setFocusedCell(0, "FirstName");
回答4:
try using gridOptions.api.setFocusedCell(0, [column name]) where column name can be ne visible column
来源:https://stackoverflow.com/questions/45700025/programmatically-select-a-row-ag-grid-angular-2