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

how to dispaly recordID or rowindex in pageblocktable

In a pageblock table I want to render more than one records returned by statndardcontroller or extension controoler.

How can I specify the recordId or the record index in pageblock table?



sr no.                   name                  age              city

1)                          tom                   22               boston

2)                           jim                       33              ny

3)                         shaun                     34               LA


I can retrieve name,aga and city.. but my controller can not get record index

i.e 1 for Tom

2 for Jim

3 for shaun etc.

Is there any way to render dynamically in visualforce?

Prafull G.Prafull G.

You can make use of apex:variable component.

<apex:variable var="sr" value="{!1}"/>



<!-- columns code here -->


<apex:variable var="sr" value="{!sr + 1}"/>



Give it a try and let me know if it works for you.


It increments once to show same number on all rows...

2 ..................






any other clue/hint?


Prafull G.Prafull G.

You have to made some changes into your code.

The variable doesn't work with datatable and pageblocktable. However it works perfect with the repeat and datalist component.


check the link


This is exactly what you want.



Pratibh PrakashPratibh Prakash



Create a javascript funciton in page as:


var index =1;
function getValue(){
     index = index +1;


and then use a outputText as:


<apex:outputText value="<script>document.write(index);getValue();</script>" escape="false"/>






You have to put the statement which increments the variable inside a column for some reason, but if the table refreshes the variable doesn't necessarily reset as well (maybe it will if you refresh the defining statement as well, I didn't bother to test this just switched to a different method)


<apex:variable [whatever the rest of this is] />
          <apex:variable [incrementing code] />
          <!-- whatever normal content you want displayed in this column -->


<apex:page standardcontroller="Contact" recordSetVar="Con">
<apex:sectionHeader title="All Contact With Serial Number"/>
 <apex:form >
                    <apex:variable var="call" value="{!0}" />
                        <apex:repeat var="C" value="{!Con}">
                                <td><apex:variable var="call" value="{!call+1}"/>
                                    <apex:outputText value="{!call}"/>


  Try this code 


<apex:variable var="sr" value="{!0}"/>



<!-- columns code here -->

.<apex:column value="Serial No">


     <apex:variable var="sr" value="{!sr + 1}"/>






Did this post answers your question If so please mark as solution  and give kudos for this post 



Akhilesh DahatAkhilesh Dahat
Correct Code to achive this is below.
            <apex:variable var="rowNum" value="{!0}"/>
                 <apex:column headerValue="No">
                     <apex:variable var="rowNum" value="{!rowNum + 1}"/>

If you doing re-select data (for example custom report with picklist) DO NOT FORGET to refresh counter 'ID' - otherwise it brings 'broken' numbers! :)
I mean something like this: 

    <div style="padding-top: 20px;">
    <apex:outputPanel id="thePanel" styleClass="printButtonClass" >
    <apex:outputLabel style="font-weight:bold;color:black" id="plUnionlabel" value="{!$Label.Select_Union}" for="plUnion" rendered="{!bShow}" />&nbsp;&nbsp;
    <apex:selectList style="width:140px;" id="plUnion" value="{!selectedUnion}" size="1" multiselect="false" rendered="{!bShow}" >
    <apex:selectOptions value="{!unionOptions}" /> 
    <apex:actionSupport event="onchange" action="{!changeOptionUnion}" rerender="counter,error,theTable,summary" />
    <input id="buttonPrint" type="button" value="{!$Label.Print}" onclick="printForm();" class="printButtonClass"/>

    <!-- skippped -->
    <apex:variable id="counter" var="i" value="{!1}"/>
Jack D PondJack D Pond
Regarding the header problem, I found an answer to this here ( .  The short answer is to create the dynamic columns first, then after you've defined them, define headers  and hide the created empty cells.
<!--Define the headers after you define the dynamic columns, then hide the empty cells that are created -->
<apex:repeat var="header" value="{!headers}">
    <apex:column headerValue="{!header}" styleClass="empty-column-content" />

Jeremiah BaylesJeremiah Bayles
Is there any way to do this in a standard report?
Ganesh ThigariGanesh Thigari

Final working code for Autoincrement the Table Column values

              <th style="background-color:#58D68D">Sample Data1</th>
              <th style="background-color:#58D68D">Sample Data2</th>
              <th style="background-color:#58D68D">Sample Data3</th>
              <th style="background-color:#58D68D">Sample Data4</th>
              <th style="background-color:#58D68D">Sample Data5</th>
              <th style="background-color:#58D68D">Sample Data6</th>
              <th style="background-color:#58D68D">  Sample Data7</th>
         <apex:variable var="count" value="{!0}" />
         <apex:repeat value="{!VariableName}" var="pass">
         <apex:variable var="count" value="{!count+1}"/>
              <td><apex:outputText value="{!count}"/></td>
              <td> {!pass.Variable1}  </td>
              <td> {!pass.Variable2}    </td> 
              <td> {!pass.Variable3}       </td>
              <td> {!pass.Variable4}       </td>
              <td> {!pass.Variable5}         </td>
              <td> {!pass.Variable6}   </td>
karthickeyan trainingkarthickeyan training
These solution will work fine only if there is no pagination. If we  use pagination the sirial number will reset to intial value. can some one provoid an better solution for that.