Alternating Row Styles with List Formatting

An easy way to make your list views far more readable is to provide alternating styles between rows. This is especially helpful for wide lists with lots of columns.

Importantly, the alternating colors need to alternate regardless of the content of the list items. This means that even with changing sorts and filters applied, the alternating styles should remain consistent. Up until a few days ago, this wasn’t possible with List Formatting. However, thanks to a new operation and magic string, applying alternating styles is super easy!

Although you can use this same basic concept for advanced visualizations within column formatting or row format style view formatting, the most common usage is to apply a color across the whole row for every other row. That’s what I’ll demonstrate here using a row class style view format.

@rowIndex

A new magic string has been added that will provide you the index (relative to the view) for a given row. The index starts at 0. So, when a view is rendered, the row at the top has a @rowIndex of 0, the next is 1, the next is 2 and so on. If the sort changes, whatever row just became the new top row has a @rowIndex of 0.

We can see this in action with a simple column format:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@rowIndex"
}
The @rowIndex value remains consistent based on render position!

Awesome! But just showing the index isn’t very helpful. Fortunately, a new operation has been added that makes using this value in an expression super easy.

Modulus (Remainder)

The modulus operator is the % sign. This operator returns the remainder left over when one operand is divided by a second operand. Here are some examples:

ExpressionResult
“=13 % 5”3
“=0 % 2”0
“=1 % 2”1
“=2 % 2”0
“=3 % 2”1

It’s those last 4 examples that are particularly relevant for us. By using the remainder operator with the second operand of 2, we can consistently get results for every other value!

Alternating Row Class

View formats provide an additionalRowClass property that allows us to apply a class(es) to whole rows. We can even do this conditionally! So, using what we learned above we can use this simple format to apply a class to every other row:

alternating-rowclass:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
  "additionalRowClass": "=if(@rowIndex % 2 == 0,'ms-bgColor-themeLighter ms-bgColor-themeLight--hover','')"
}

We are checking to see if the remainder of dividing the @rowIndex by 2 is 0 (even numbers will be 0 and odd numbers will be 1). When it’s 0, we use the UI Fabric theme classes to apply a theme color across the whole row. We also add an additional class to add a hover effect as well. Here’s the result:

So beautiful!
On the fly sorts? No Problem!
On the fly filters? No Problem!

That’s all there is to it!

8 thoughts on “Alternating Row Styles with List Formatting

    • Hello,

      been looking for this too, but understand how to implement this in my SharePoint 2019. If i put this in a Column to Format it, the only one effect is that all values in the Column are no more visible (like a filter). If i delete the Format the values are shown.

      What do i wrong?

  1. Hi,
    dumbass question perhaps, but do I need to copy this into the column formatting box in edit column under list settings?
    Do I then have to edit each column in my list to put the code in or is it only required in one place once?
    Thanks, Marc

    • This format needs to be copied into the view formatting. So, in the view menu you can select “Format this view” to paste it and it will apply to the entire view (rather than individual columns). However, it’s worth noting that alternating rows are now part of the new wizards available (at least in targeted release) so you may be able to skip the formatting code altogether if you’re happy enough with how the wizard works.

      Let me know if any of that was unclear, thanks!

      • Hi thanks that made sense, and it seems as you mention when I go to format the current view alternating row styles is already an option, so no code required.
        Thanks.

Leave a reply to SharePoint PnP Dev General SIG Call – March 7th, 2019 – Screenshot Summary - Warner Digital Cancel reply