How to detect first or last iteration of apex:repeat
I rethought and this seems the best way so deleting the previous contents:
Create a
map<account,index>
Put the account, index and also populate the last index in your method, and check if the index = last index and render the ,
Page:
<apex:page controller="testing_repeat_controller" sidebar="false">
<apex:repeat value="{!Itemsforuser}" var="item">
<apex:outputPanel >
<apex:outputText value="{!item.Name}"/>
{!IF(account_index_map[item] == lastindexofaccount,'',',')}
</apex:outputPanel>
</apex:repeat>
</apex:page>
Controller:
public class testing_repeat_controller {
public integer indexofaccount {get;set;}
public integer lastindexofaccount {get;set;}
public map<account,integer> account_index_map {get;set;}
public testing_repeat_controller(){
indexofaccount = 0;
lastindexofaccount = 0;
account_index_map = new map <account,integer>();
}
public List<account> getItemsforuser(){
list<account> acc_list = [select id,name from account limit 10];
lastindexofaccount = acc_list.size();
for(account a: acc_List){
indexofaccount = indexofaccount +1;
account_index_map.put(a,indexofaccount);
}
return acc_List;
}
}
Output:
abc ,abc ,TEST ,test123 ,abc ,abc ,abc ,TEST ,Test_Pass ,abc
I think a more elegant solution would be to render items as an unordered list and then use css to render the commas.
<style type="text/css">
.cs-list {
display: inline;
list-style: none;
padding-left: 0px;
}
.cs-list li {
display: inline;
margin-left: 3px;
}
.cs-list li:after {
content: ", ";
}
.cs-list li:last-child:after {
content: "";
}
</style>
...
<ul class='cs-list'>
<apex:repeater var='item' value='{!myStringList}'>
<li>{!item}</li>
</apex:repeater>
</ul>
You might need to tweak margin and padding a bit to get it to look perfect (this example came from an project that was loading bootstrap css).