首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >银栅中的最高行数

银栅中的最高行数
EN

Stack Overflow用户
提问于 2021-08-30 15:15:14
回答 2查看 145关注 0票数 0

我在找银栅中最高的行号。每当在数据库中插入新行并显示在网格上时,我必须用某种颜色突出显示最新的行。我已经在提供的链接中看到了一些代码,其中可以根据列中的某个值突出显示一行,但我正在试图确定该值是否是最高的行号。

如何根据列中的某个值为ag网格中的整行提供背景色?

任何帮助都将不胜感激。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-30 18:01:22

您需要有一个获得“元素最大值”的函数。因此,想象一下这样的行数据

代码语言:javascript
运行
复制
  rowData = [
    { make: 'Toyota', model: 'Celica', price: 35000 },
    { make: 'Ford', model: 'Mondeo', price: 32000 },
    { make: 'Porsche', model: 'Boxter', price: 72000 }
  ];

所以你有

代码语言:javascript
运行
复制
  @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中添加类,而不是在组件中)

代码语言:javascript
运行
复制
  getRowClass = params => {
    return params.data === this.maximum ? 'red' : null;
  };

喜欢.css

代码语言:javascript
运行
复制
.red{
  background-color:red!important;
  color:white!important;
}

.red .ag-input-field-input.ag-text-field-input{
  color:#181d1f!important 
}

现在,您需要考虑调用函数getGreaterElement时的情况

代码语言:javascript
运行
复制
  //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

代码语言:javascript
运行
复制
<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>

你可以在这一堆闪电战中看到

票数 0
EN

Stack Overflow用户

发布于 2021-08-30 15:39:37

如果你在找不算的话。对于最后一行的行或行节点对象,您可以通过网格API对象获得它。这是指南:https://www.ag-grid.com/javascript-data-grid/grid-api/

希望您已经通过(gridReady)存储了grid对象

代码语言:javascript
运行
复制
<ag-grid-angular
   (gridReady)="functionToStoreGridAPI($event)"
   // ...
/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68986428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档