Friday, July 6, 2007

Alternating Colors in a List

A bit of display code I came up with.

The problem: Your (database-driven) web page has a very long list of items and you want to make it easier to view.

The solution: Alternate background colors on each row, or preferably every third row, or fourth.

It's easy enough to alternate colors every other row, with code like this:

rowcount = 0

if (rowcount++ % 2) rowcolor = grey else rowcolor = white
...
next item

But I find that looks too busy. What if I want to have three lines of grey and then three lines of white? Given that I'm keeping the variable rowcount, I can simply modify the equation from:

if (rowcount % 2)

which, given the modulus of 2 will return a 0 if the rowcount is even, and 1 if the rowcount is odd; instead, try this:

if ((rowcount / 3) % 2)

What does this do? Well, first it divides the current row number by 3, no remainder. that means the first 3 rows (if you start with 0) divided by 3 will be 0, the next 3 will be 1, the next 3 will be 2, etc. The result of this divide will be alternating odd or even. That's where the modulus 2 comes in. Then you have a binary value alternating between 0 and 1 every 3 rows.

You can also divide by 4, 5, or any number to increase the number of rows in each color block.

I'll be looking for an example of a site that does this. The one I did is internal only, so no link here.

1 comment:

7zjhy8uhuq said...

Changes to a hardened mould by standard drilling and milling usually require annealing to soften the mould, followed by heat remedy to harden it again. EDM is an easy process in which a formed electrode, usually made of copper or graphite, could be very slowly lowered onto the mould surface over a period of many hours, which is immersed in paraffin oil . A voltage utilized between software and mold causes spark erosion of the mould Satin Panties surface in the inverse shape of the electrode. Manufacturers go to great lengths to guard customized moulds end result of} their high average costs. The perfect temperature and humidity level is maintained to make sure the longest attainable lifespan for each customized mould. Custom moulds, corresponding to those used for rubber injection moulding, are saved in temperature and humidity controlled environments to stop warping.