+ Start a Discussion
Mike ArthurMike Arthur 

How to apply formatting in the controller to data to be displayed in VF table

Hi

I was expecting to find this information easily but I've not found anything.

In my controller I run a SOQL statement with 'group by rollup' and I then display the records in a visualforce table.

Each time that a subtotal is displayed, I want to either indent or right justify the word 'Subtotal' and do the same for the grand total.  I can't find anything that explains how to apply this formatting in the controller.

Any help would be greatly appreciated.

Here's the controller:
 
public class SummariseQuotedWorks {
    private final Quote qte;
    
    public SummariseQuotedWorks(ApexPages.StandardController controller) {
        this.qte = (Quote)controller.getRecord();
        }
    
/* Use a wrapper class to store the aggregate values in a way that is accessible to Visualforce */

public class WISum{
        public String Acct {get; set;}
        public String WorkItem {get; set;}
        public Decimal TotalCost {get; set;}
        public WISum(string a,string w,decimal c){
                this.Acct=a;
                this.WorkItem=w;
                this.TotalCost=c;
        }
}

public List<WISum> WISumList = new List<WISum>();

public List<WISum> getWISumOut(){

		String theAcct = '';

        AggregateResult[] AgR = [SELECT Account__r.Name AC, WorkItem__r.Name WI, sum(cost1__c) TC FROM Quoted_Work__c
where Quote__c=:qte.Id
group by rollup(Account__r.Name, WorkItem__r.Name)]; 

        for (AggregateResult WIRecs : AgR) {


       
 /*      WISumList.add(new WISum((String) WIRecs.get('AC'), (String) WIRecs.get('WI'), (Decimal) WIRecs.get('TC'))); */
 	        WISumList.add(new WISum((String) ((WIRecs.get('AC') == theAcct) ? '' : WIRecs.get('AC')), (String) (WIRecs.get('WI') == null ? 'Subtotal' : WIRecs.get('WI')), (Decimal) WIRecs.get('TC')));
 
        	theAcct = (String)WIRecs.get('AC');
 
        }
        return WISumList;
}
    
}

Here's the VF:
 
<apex:page standardController="Quote" extensions="SummariseQuotedWorks">
	<apex:pageBlock title="Summary of Works for Quote: {!Quote.Name}">
		<apex:pageBlockSection >
			<apex:pageBlockTable value="{!WISumOut}" var="WISummary">
				<apex:column value="{!WISummary.Acct}">
					<apex:facet name="header">Site</apex:facet>
				</apex:column>
				<apex:column value="{!WISummary.WorkItem}">
					<apex:facet name="header">Work Item</apex:facet>
				</apex:column>
				<apex:column value="{!WISummary.TotalCost}">
					<apex:facet name="header">Total Cost</apex:facet>
				</apex:column>
			</apex:pageBlockTable>
		</apex:pageBlockSection>
	</apex:pageBlock>
</apex:page>

Here's the ouput:

User-added image

Wherever 'Subtotal' appears, I want it indented or right justified.

I've not modifed the controller yet to display 'Grand Total' in the last line but will do and will want that formatted too.

Many Thanks,
Mike.

 
Best Answer chosen by Mike Arthur
Nagendra Prasad ChNagendra Prasad Ch
Hi Mike,

Try this. If you want any additional formating to Subtotal text add  it in Style attribute.

<apex:column >
<apex:facet name="header">Work Item</apex:facet>
                                    <apex:outputText value="{!WISummary.WorkItem}" style="font-weight:bold;color:red;text-align: right;" rendered="{!WISummary.WorkItem == 'Subtotal'}" />
                                    <apex:outputText value="{!WISummary.WorkItem}" rendered="{!cmp.Recordtype.Name <> 'Subtotal'}" />
 </apex:column>​

 
<apex:page standardController="Quote" extensions="SummariseQuotedWorks">
	<apex:pageBlock title="Summary of Works for Quote: {!Quote.Name}">
		<apex:pageBlockSection >
			<apex:pageBlockTable value="{!WISumOut}" var="WISummary">
				<apex:column value="{!WISummary.Acct}">
					<apex:facet name="header">Site</apex:facet>
				</apex:column>
				
				<apex:column ><apex:facet name="header">Work Item</apex:facet>
                                    <apex:outputText value="{!WISummary.WorkItem}" style="font-weight:bold;color:red;text-align: right;" rendered="{!WISummary.WorkItem == 'Subtotal'}" />
                                    <apex:outputText value="{!WISummary.WorkItem}" rendered="{!cmp.Recordtype.Name <> 'Subtotal'}" />
                                </apex:column>
				
				
				<apex:column value="{!WISummary.TotalCost}">
					<apex:facet name="header">Total Cost</apex:facet>
				</apex:column>
			</apex:pageBlockTable>
		</apex:pageBlockSection>
	</apex:pageBlock>
</apex:page>

 

All Answers

Nagendra Prasad ChNagendra Prasad Ch
Hi Mike,

Try this. If you want any additional formating to Subtotal text add  it in Style attribute.

<apex:column >
<apex:facet name="header">Work Item</apex:facet>
                                    <apex:outputText value="{!WISummary.WorkItem}" style="font-weight:bold;color:red;text-align: right;" rendered="{!WISummary.WorkItem == 'Subtotal'}" />
                                    <apex:outputText value="{!WISummary.WorkItem}" rendered="{!cmp.Recordtype.Name <> 'Subtotal'}" />
 </apex:column>​

 
<apex:page standardController="Quote" extensions="SummariseQuotedWorks">
	<apex:pageBlock title="Summary of Works for Quote: {!Quote.Name}">
		<apex:pageBlockSection >
			<apex:pageBlockTable value="{!WISumOut}" var="WISummary">
				<apex:column value="{!WISummary.Acct}">
					<apex:facet name="header">Site</apex:facet>
				</apex:column>
				
				<apex:column ><apex:facet name="header">Work Item</apex:facet>
                                    <apex:outputText value="{!WISummary.WorkItem}" style="font-weight:bold;color:red;text-align: right;" rendered="{!WISummary.WorkItem == 'Subtotal'}" />
                                    <apex:outputText value="{!WISummary.WorkItem}" rendered="{!cmp.Recordtype.Name <> 'Subtotal'}" />
                                </apex:column>
				
				
				<apex:column value="{!WISummary.TotalCost}">
					<apex:facet name="header">Total Cost</apex:facet>
				</apex:column>
			</apex:pageBlockTable>
		</apex:pageBlockSection>
	</apex:pageBlock>
</apex:page>

 
This was selected as the best answer
Mike ArthurMike Arthur
Thank you - that's what I was looking for :-)

Unfortunately the right align isn't taking effect.  I've Googled the issue, others have experienced it but I didn't find a solution.

I tried adding the style attribute to the column tag and that works, the whole column becomes right aligned
<apex:column style="text-align:right;">
I tried left aligning the column and right aligning the subtotal cell but it doesn't affect the individual cell.

Maybe it only works at the column level and not for individual cells.

The bold and red helps to set it apart so that will meet my needs but if it would right align too that would be perfect!
 
<apex:column>
					<apex:facet name="header">Work Item</apex:facet>
					<apex:outputText value="{!WISummary.WorkItem}" style="font-weight:bold;color:red;text-align:right;" rendered="{!WISummary.WorkItem == 'Subtotal'}" /> 
					<apex:outputText value="{!WISummary.WorkItem}" rendered="{!WISummary.WorkItem <> 'Subtotal'}" /> 
				</apex:column>


Mike.
Nagendra Prasad ChNagendra Prasad Ch
As you said right align may not work on individual components.
Anyways, select the best answer from the comments, so it will be helpful to the others who are searching for the similar solutions.
Mike ArthurMike Arthur
Thanks – I will do. I tried setting the work item string to ‘       Subtotal’ but it is rendered as I have written it, i.e. the characters ‘ ’ appear as text. Do you know how I can make white space appear? Thank you.
Mike ArthurMike Arthur
In the above I had written '& n b s p ; & n b s p;' before 'Subtotal' - but here it shows as white space!  On my vf page, the text '& n b s p' is rendered before 'Subtotal' (but without the spaces!)
Nagendra Prasad ChNagendra Prasad Ch
There is a way by using  escape="false" attribute. This will render Html components perfectly.
 
<apex:column>
<apex:facet name="header">Work Item</apex:facet>
<apex:outputText  escape="false"  value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Subtotal" style="font-weight:bold;color:red;" rendered="{!WISummary.WorkItem == 'Subtotal'}" /> 
<apex:outputText value="{!WISummary.WorkItem}" rendered="{!WISummary.WorkItem <> 'Subtotal'}" /> 
</apex:column>




 
Mike ArthurMike Arthur
Got it – just the job. Thanks.