我在找银栅中最高的行号。每当在数据库中插入新行并显示在网格上时,我必须用某种颜色突出显示最新的行。我已经在提供的链接中看到了一些代码,其中可以根据列中的某个值突出显示一行,但我正在试图确定该值是否是最高的行号。
任何帮助都将不胜感激。谢谢。
发布于 2021-08-30 18:01:22
您需要有一个获得“元素最大值”的函数。因此,想象一下这样的行数据
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>
你可以在这一堆闪电战中看到
发布于 2021-08-30 15:39:37
如果你在找不算的话。对于最后一行的行或行节点对象,您可以通过网格API对象获得它。这是指南:https://www.ag-grid.com/javascript-data-grid/grid-api/
希望您已经通过(gridReady)存储了grid对象
<ag-grid-angular
(gridReady)="functionToStoreGridAPI($event)"
// ...
/>
https://stackoverflow.com/questions/68986428
复制相似问题