6

I want to show row number dynamically in pageblock table Based on no. of records present in page block.

1
  • Have you tried anything yet ? Commented Feb 25, 2016 at 21:39

3 Answers 3

7

You may also be able to use the CSS counter syntax to generate the numbers purely client-side:

#myTable {
    counter-reset: section;
}
#myTable .row {
    counter-increment: section;
}
#myTable .row .counter::before {
    content: counter(section);
}

Where "#myTable" is the ID for the table you're generating, "row" is a class for each row element, and "counter" is a class for each counter:

<table id="myTable">
<tbody>
<apex:repeat value="{!rows}" var="row">
    <tr class="row">
        <td>
            <span class="counter"></span>
        </td>
    </tr>
</apex:repeat>
</tbody>
</table>

Edit: Here's a fully functional example using a standard controller:

<apex:page standardController="Account" recordSetVar="records">
    <style>
    .myTable {
        counter-reset: rowCounter;
    }
    .myTable .row {
        counter-increment: rowCounter;
    }
    .myTable .row .counter::before {
        content: counter(rowCounter);
    }
    </style>
    <apex:pageBlock>
        <apex:pageBlockTable value="{!records}" var="record" styleClass="myTable" rowClasses="row">
            <apex:column>
                <span class="counter"></span>
            </apex:column>
            <apex:column value="{!record.Name}" />
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
2
  • Great stuff it is nice to know such things! Commented Feb 25, 2016 at 19:50
  • Any Idea why it was not working, I was using this code: <apex:variable var="rowNumber" value="{!0}"/> <apex:column headerValue="No." style="width:20px; text-align:center;" headerClass="centertext"> <apex:outputText value="{0}" style="text-align:center;"> <apex:param value="{!rowNumber+1}" /> </apex:outputText> </apex:column> <apex:variable var="rowNumber" value="{!rowNumber+1}"/> Commented Feb 28, 2016 at 11:13
6

You can use an apex:variable, as shown in the code below, to show a row number.

<apex:variable var="index" value="{!1}"/>
<apex:repeat value="{!yourList}" var="ob">
    <apex:outputLabel value="{!index}"/>
    <apex:variable var="index" value="{!index+1}"/>
</apex:repeat>
0

Jeff has a nice post on that in his blog here. Also if you use wrapper class you can just create another variable there and display it as a first column.

Here is an example:

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

<apex:dataList value="{!myCollection}" var="item">  
  <apex:outputText value="{!FLOOR(rowNum)}"/> - <apex:outputField value="{!item.Name}"/>
  <apex:variable var="rowNum" value="{!rowNum + 1}"/>
</apex:dataList> 
2
  • Please include the actual answer within your post. You can refer to blogs or documentation for extended information, but we risk losing your answer entirely if there's nothing in your post and the external reference goes offline. Commented Feb 25, 2016 at 21:44
  • @SamuelDeRycke thanks, I`ve updated my answer. Commented Feb 26, 2016 at 6:46

Not the answer you're looking for? Browse other questions tagged .