function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
megmooPDXmegmooPDX 

conditionally highlight row based on index position

I want to change the background color for the first row of data in a table displayed on a visual force page. I am thinking the best way to do this is change the style based on a test of which record is being displayed. However, I'm not sure how to determine which record is being displayed. 

Here's the code in a portion of my VF page:

<apex:pageBlock >   
    <apex:pageBlockTable value="{!cRecords}" var="cRecord">
       
        <apex:column value="{!cRecord.Name}">
            <apex:facet name="header">HCC</apex:facet>
        </apex:column>
       
        <apex:column value="{!cRecord.HCC_strNum__c}">
            <apex:facet name="header">HCC</apex:facet>
        </apex:column>
       
        <apex:column value="{!cRecord.Type__c}">
            <apex:facet name="header">Type</apex:facet>
        </apex:column>
   
        <apex:column value="{!cRecord.Confidence_Level__c}">
            <apex:facet name="header">Confidence Level</apex:facet>
        </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>

I thought the best way to do this is to use the index number of the array to determine whether to highlight the field, but I don't know how to retrieve the index number. Is there way to achieve something like the line below? I know cRecords.indexnumber = 0 is not correct/valid, but I'm looking for what that syntax might be. 

<apex:column value="{!cRecord.Name}" style="{!IF(cRecords.indexnumber = 0),"background-color: green;",""background-color: white);"
Best Answer chosen by megmooPDX
Sonam_SFDCSonam_SFDC
Hi,

Are you looking for something as explained in the below blog:
https://sites.google.com/site/infallibletechie/code-for-alternate-background-color-for-rows-in-a-pageblocktable
Code on the above link will help you color the bckgrd of the pageblocktable which you have created.

All Answers

Sonam_SFDCSonam_SFDC
Hi,

Are you looking for something as explained in the below blog:
https://sites.google.com/site/infallibletechie/code-for-alternate-background-color-for-rows-in-a-pageblocktable
Code on the above link will help you color the bckgrd of the pageblocktable which you have created.
This was selected as the best answer
megmooPDXmegmooPDX
@Sonam_SFDC - that's exactly what I needed. Thank you! I'm posting my updated code below since I'm doing it slightly differently from highlighting every other row.

<apex:pageBlock >
    <apex:variable var="i" value="{!0}"/> 
    <apex:pageBlockTable value="{!cRecords}" var="cRecord">
      
        <apex:column >
            <apex:variable var="i" value="{!i+1}"/>
        </apex:column>
      
        <apex:column value="{!cRecord.Name}" style="background-color:{!IF(i=1,'#00FF00','')}">
            <apex:facet name="header">HCC</apex:facet>
        </apex:column>
      
        <apex:column value="{!cRecord.HCC_strNum__c}" style="background-color:{!IF(i=1,'#00FF00','')}">
            <apex:facet name="header">HCC</apex:facet>
        </apex:column>
      
        <apex:column value="{!cRecord.Type__c}" style="background-color:{!IF(i=1,'#00FF00','')}">
            <apex:facet name="header">Type</apex:facet>
        </apex:column>
  
        <apex:column value="{!cRecord.Confidence_Level__c}" style="background-color:{!IF(i=1,'#00FF00','')}">
            <apex:facet name="header">Confidence Level</apex:facet>
        </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
megmooPDXmegmooPDX
For anyone who might reference this post later, one warning. It appears as though using the <apex:variable> Standard Component as a counter is unsupported by Salesforce: http://www.salesforce.com/us/developer/docs/pages/