I want to show row number dynamically in pageblock table Based on no. of records present in page block.
3 Answers
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>
-
-
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
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>
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>
-
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
-