So if we wanted to take these styles and actually write them in CSS instead of inline in JavaScript, we could do that by creating a customer name CSS class with those styles. And we can pass in the name of the class as an array to the cell class property on the column definition. So now these cells will be rendered with that customer name class and we'll have full ability to style it using CSS by hooking off of that class name.
And then finally, I think the more interesting one is the cell class property. It can be a function that's invoked as well. So we can take a look at like, hey, is the value a zero, for example? well, let's show an error class or let's show a customer name class. And so we can do some advanced class application here by doing some inspection on the value to the return or determine which class to actually apply.
AG Grid goes above and beyond by giving us cell class rules, which is just formalizing that approach I previously showed. So for cell class rules, I give it a dictionary object of every CSS class that I might want to apply to the cell. And then I give it a function that returns true or false as to whether or not this class should be applied to the cell. So, for example, in this one here, I'm applying a cell value negative class if the value is below zero and a cell value positive class if it's greater than or equal to zero. And you can have as many values in this dictionary as you would like.
A few things to note that I want to highlight. So cell styles add and override existing styles. These styles are never going to be removed. These are sort of static styles that are applied. Cell class can add classes but never remove any classes, right? We can give it classes to apply, but we can't tell which classes shouldn't be applied. Cell class rules is the most advanced of the three options. It allows us to add and remove classes with those predicate functions that control whether or not a class should be applied.
Row styling. So that's for cells, but how can we want to actually style some rows? Well for rows, we can use the row style to add styles to all the rows. We can do GetRowStyle to conditionally add styles, and then just like we had for cells, we have row class, getRowClass, and row class rules for managing what styles are applied. So for example, to do some row styling, First off, because we're talking about rows now and not columns, we're going to apply these on the agGridReact component directly. So for agGridReact, we're going to say, okay, for row class, here's the class I want to apply, or for row class rules, again, that could be a dictionary, we can apply that in, or for row styles, if we just want to apply some static styles, we can do that here. Here's an example of showing those row class rules. These can be just dictionaries again with those predicate functions that apply styles, styles whether or not these functions return true or that getRowClass can be a function that kind of looks at the whole row data and chooses whether or not to apply a class to it.
Finally, I want to talk about some of the built-in themes. AG Grid has several provided themes. Valham is the original theme that AG Grid first shipped with. It's really dense, so it's great if you're trying to show a large amount of data. It's got that dense style to it. Alpine is the we've been doing today. It's a fairly modern and fresh look to it but it's less dense in the Balham style. So it shows some data but not as much data as the Balham one and it also has a built-in material style and that's the least dense. Balham and Alpine also include dark mode options. So if we want to go over here we wanted to change the way this was styled here. I could go to my CSS here or is this an index? Yeah and I could change the AG theme like if I want this to be material instead, I could pull that in, and now we have a material look. If I wanted to do the BALHAM one, I could do that as well and get that really dense look to it. I think I spelled that right. Yeah, I did. That's how you control that. You just import the theme that you want to apply to your AG grid. And those are the three themes that are built into it. You can customize any of these themes. They all have CSS variables and CSS rules. Additionally, if you're using SAS, there are SAS mixins and functions for controlling or applying these themes. For example, if we were to try and customize the Alpine theme, we could do so with SAS. We could import the AGThemeAlpine mixin, and then we could apply it to our grid, and we can control things like the grid size, the active colors, whether or not borders are being displayed. A lot of customization options in here. Definitely would recommend referencing the documentation to see how you can customize these themes.
Comments