Angular Grid with Rows in Alternate Colors

Created: 20 October 2017

Updated: 29 April 2019

In general, all rows in IntegralUI Grid component for Angular, have the same color style. Depending on current row state, a different color is displayed as its background. In cases when you have many rows, to improve the visual appearance of the Grid and to easily scan through the rows, it is better if even and odd row are displayed in a different color. This article demonstrates that: alternate colors for even/odd rows.

Continents/Countries
Population
Date
Land in km2
Capital
Egypt
88,311,000
06 Apr 2015
995,450
Cairo
Nigeria
185,043,000
01 Jul 2015
910,768
Abuja
South Africa
54,002,000
01 Jul 2014
1,214,470
Pretoria
China
1,369,140,000
06 Apr 2015
9,326,410
Beijing
India
1,269,545,000
01 Jul 2015
2,864,021
New Delhi
Japan
126,910,000
01 Mar 2015
364,485
Tokyo
Saudi Arabia
31,521,418
01 Jul 2015
2,149,690
Riyadh
South Korea
51,342,881
01 Jan 2015
100,032
Seoul
France
66,109,000
01 Mar 2015
640,427
Paris
Germany
80,925,000
30 Jun 2014
348,672
Berlin
Italy
60,788,845
30 Nov 2014
294,140
Rome
Macedonia
2,065,769
31 Dec 2013
25,433
Skopje
Canada
35,702,707
01 Jan 2015
9,093,507
Ottawa
Mexico
121,005,815
01 Jul 2015
1,943,945
Mexico City
USA
320,651,000
07 Apr 2015
9,161,966
Washington
Argentina
43,131,966
01 Jul 2015
2,736,690
Buenos Aires
Brazil
204,134,000
08 Apr 2015
8,460,415
Brasília
Grid component is part of IntegralUI Web
a suite of UI Components for development of web apps

If you have any questions, don't hesitate to contact us at support@lidorsystems.com

How to Display Grid Rows in Alternate Colors

The appearance of the Grid component is determined by a set of CSS classes. You can modify the attribute of each class on your side, but a better approach is to override the existing CSS classes with your own. For this purpose, you need to use the controlStyle property, which holds a reference to an object that contains the names of all classes for each part of the Grid.

You don't have to add new classes for all parts, you only need to change the ones used by the Grid rows. In this case, under row section, only change the general settings, and apply a different CSS class for each row state: normal, hovered and selected. For example:

public gridStyle: any = {
    general: {
        normal: 'grid-eor-normal'
    },
    row: {
        general: {
            normal: 'grid-eor-row-normal',
            hovered: 'grid-eor-row-hovered',
            selected: 'grid-eor-row-selected'
        }
    }
}
                            

Using above code, you have changed the appearance of all grid rows by creating a custom CSS class. Furthermore, to make sure an alternate color is in use based on whether a row order is even or odd, you need to use the CSS selector: nth-of-type(2n). This sets a different color for each row, depending on its order in the list.

When applying custom CSS classes to the grid rows, the default ones are overridden. In this case, the built-in styles applied when a row is hovered or selected are removed. This happens mainly because the CSS selector has higher priority than a class selector. To solve this, just apply a CSS class for hovered and selected state of a row:

public gridStyle: any = {
    general: {
        normal: 'grid-eor-normal'
    },
    row: {
        general: {
            normal: 'grid-eor-row-normal',
            hovered: 'grid-eor-row-hovered',
            selected: 'grid-eor-row-selected'
        }
    }
}
                            

As a result, you have a Grid where rows are displayed in alternate colors. In addition, when row is hovered or selected a different style is applied.

Conclusion

Using custom CSS classes and selectors, you can change the appearance of the Angular Grid component. Displaying even/odd rows in alternate colors is very simple, you only need to create a class and apply it to the general row settings under controlStyle. You can add classes for each row state: disabled, normal, hovered and selected.

The Grid component is part of IntegralUI Web.

Newsletter


Sign-up to our newsletter and you will receive news on upcoming events, latest articles, samples and special offers.
Name: Email: *
*By checking this box, I agree to receive a newsletter from Lidor Systems in accordance with the Privacy Policy. I understand that I can unsubscribe from these communications at any time by clicking on the unsubscribe link in all emails.