简体   繁体   English

如何通过单击 ag-grid 中的按钮来更改行样式/类?

[英]How to change row style/class by clicking button in ag-grid?

Is there any way to make row change background by clicking on button?有没有办法通过点击按钮来改变行背景? I have the following code:我有以下代码:

 rowClassRules: { "a-grade": params => params.api.getValue("Grade", params.node) == 'A', "b-grade": params => params.api.getValue("Grade", params.node) == 'B', "c-grade": params => params.api.getValue("Grade", params.node) == 'C', },

It changes row color based on value in the column Grade.它会根据等级列中的值更改行颜色。 But I want to do the same only after pressing a button, and also other button should turn it off (like Turn On/Off highlighting).但我只想在按下按钮后做同样的事情,其他按钮也应该将其关闭(如打开/关闭突出显示)。 I have no idea how to turn this code into a function which I can use in the button's onclick.我不知道如何将此代码转换为 function,我可以在按钮的 onclick 中使用它。

if you want to have actions like mouse-click in ag-grid you should use a component renderer:如果您想在 ag-grid 中进行鼠标单击等操作,您应该使用组件渲染器:

https://www.ag-grid.com/javascript-data-grid/component-cell-renderer/ https://www.ag-grid.com/javascript-data-grid/component-cell-renderer/

There you can set a hidden property like 'isHighlighting' in row data using params.api with any proper method like: setData(), applyTransaction(), or applyTransactionAsync().在那里,您可以使用 params.api 和任何适当的方法(如 setData()、applyTransaction() 或 applyTransactionAsync())在行数据中设置隐藏属性,如“isHighlighting”。

Also, consider using a simpler way to write your rowClassRules:此外,考虑使用更简单的方法来编写 rowClassRules:

https://ag-grid.com/angular-data-grid/row-styles/#row-class-rules https://ag-grid.com/angular-data-grid/row-styles/#row-class-rules

The final code should be something like this:最终的代码应该是这样的:

 rowClassRules: { 'a-grade': function(params) { return params.data.isHighlighting && params.data.Grade === 'A'; }, 'b-grade': function(params) { return params.data.isHighlighting && params.data.Grade === 'B'; }, 'c-grade': function(params) { return params.data.isHighlighting && params.data.Grade === 'C'; } };

If the button is not inside the grid you can use this code:如果按钮不在网格内,您可以使用以下代码:

 isHighlighting = false; onClick() { isHighlighting =;isHighlighting. gridApi;redrawRows() }

and

'a-grade': function(params) { return isHighlighting && params.data.Grade === 'A'; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM