你需要一个函数来获取“元素最大值”。所以想象一个像
这样的行数据
rowData = [
{ make: 'Toyota', model: 'Celica', price: 35000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Porsche', model: 'Boxter', price: 72000 }
];
所以你有
@ViewChild('agGrid',{static:true}) agGrid: AgGridAngular; //<--to get the grid
maximum: any; //<--here we store the greater element
//your function getGreaterElement
getGreaterElement()
{
//get the element with bigger price
this.maximum=this.rowData.reduce((a,b)=>+b.price>+a.price?b:a)
if (this.agGrid.api) //if exist the api
this.agGrid.api.redrawRows(); //redraw the grid
}
你添加类的函数(记住你需要在styles.css中添加类,而不是在组件中)
getRowClass = params => {
return params.data === this.maximum ? 'red' : null;
};
.css 之类的
.red{
background-color:red!important;
color:white!important;
}
.red .ag-input-field-input.ag-text-field-input{
color:#181d1f!important
}
现在你需要在调用函数 getGreaterElement 时考虑一下
//in the ngOnInit
ngOnInit() {
this.getGreaterElement();
}
//when stop edit the cell "price"
cellEditingStopped(event: any) {
if (event.column.colId == 'price') this.getGreaterElement();
}
//this is a function to add a row
addRow() {
const index = this.rowData.length;
this.rowData.push({ make: '', model: '', price: 0 });
this.agGrid.api.applyTransaction({
add: [this.rowData[index]]
});
this.agGrid.api.startEditingCell({
rowIndex: index,
colKey: 'make'
});
}
还有.html
<button (click)="addRow()">add</button>
<ag-grid-angular #agGrid
style="width: 500px; height: 200px;"
class="ag-theme-alpine"
[rowData]="rowData"
[columnDefs]="columnDefs"
[defaultColDef]="defaultColDef"
[getRowClass]="getRowClass"
(cellEditingStopped)="cellEditingStopped($event)"
>
</ag-grid-angular>
你可以在this stackblitz看到