+ Start a Discussion
jasonwanBellajasonwanBella 

Use Javascript Function in Visualforce

I am trying to simply call on a javascript to edit the display for a set within a dataTable.

Code:
<script>
function Currency(Double field){
return "hello";
var CurrencyLook = "";
var tempField=field;
if(field<0){tempfield = -field; CurrencyLook += "("; }
if(tempfield>=0){
CurrencyLook += "$"+tempfield;
var CurrencyIndex = CurrencyLook.indexOf(".");
if(CurrencyIndex!=-1 && CurrencyIndex!=null){
if(CurrencyIndex + 3 != CurrencyLook.length() ) CurrencyLook += CurrencyLook+"0";
}
}
if(field<0){CurrencyLook += ")";}
return CurrencyLook;
}
</script>

<apex:outputText rendered="{!ProductGSize>0}">
<apex:repeat value="{!productG}" var="prodG">
<tr><td class="regText" width="">{!prodG.PricebookEntry.Product2.Name}</td>
<td class="regText" width="">{!prodG.Quantity}</td>
<td class="regText" width="">({!prodG.UnitPrice})</td>
<td class="regText" width="">Currency({!prodG.TotalPrice})</td>
</apex:repeat>
</apex:outputText>

 Obviously it doesnt invoke the javascript that I have written. Instead it displays the text as is... 

Any ideas how to get it to run the script and return the modified string value??   Hopefully simple answer. Couldnt get it to work in the controller either cause I have to use <apex:repeat>

Thanks!!!

Ron HessRon Hess
a few thoughts.

first you can do this in a controller, rather than repeat across a list of sobjects, just run your repeat across a class of your own creation.

This class is sometimes called a wrapper, it holds your sobject, but also holds a few properties that you can use to format currency and such.

or,
Did you try
<apex : outputField value="{!prodG.TotalPrice}" />


if you must format with JavaScript, i think you can use :
<script>Currency({!prodG.TotalPrice});</script>


Message Edited by Ron Hess on 01-06-2009 02:30 PM
jasonwanBellajasonwanBella
Thanks, Ron for the initial insight. Wrapper class works really well...  For those also interested, i posted a snippet of my code to show a little about a wrapper class since I couldnt really find it on other posts.

Added this extra code/wrapper class into my main class.

Code:
    public class cProduct{
     public String CurrencyView(Decimal field){
         String CurrencyLook = ''; Decimal tempField=field;
         if(field<0){tempfield = -field; CurrencyLook += '(';    }
         if(tempfield>=0){
             CurrencyLook += '$'+tempfield;
             Integer CurrencyIndex = CurrencyLook.indexOf('.');
             if(CurrencyIndex!=-1 && CurrencyIndex!=null){
                 if(CurrencyIndex + 3 != CurrencyLook.length() ) CurrencyLook += CurrencyLook+'0';
             }
         }
         if(field<0){CurrencyLook += ')';}       
         return CurrencyLook;
     }     
     public OpportunityLineItem line{get; set;}
     public String UnitPrice{get; set;}
     public String TotalPrice{get; set;} 
     public String Family {get; set;}
     public cProduct(OpportunityLineItem l, Decimal unit, Decimal total, String prodFamily){
   line = l;
   UnitPrice = CurrencyView(unit);
   TotalPrice = CurrencyView(total);  
   Family = prodFamily;    
     }
    }
    public List<cProduct> product {
     get{
      if(product==null)
       product=new List<cProduct>(); 
      return product;
     } 
     set;
    }


To access the wrapper class, I just used {!product.line.UnitPrice} for the standard Salesforce or {!product.UnitPrice} for the value in my custom wrapper class to get the formatting that I wanted.


Now I can use the opportunity line item fields as well as manipulate any other fields within opportunity line items.


Couldnt get <script>Currency({!prod.UnitPrice});<script> to work though.
 

Ron HessRon Hess
Great result!

thanks for posting this
a_Ba_B
Here a way you can do this ...


< apex:repeat value="{!opportunities}" var="op">
< tr>
< td>{!op.name}
< td id="{!op.id}" / >
< /tr>
<script>
var d;
d="{!op.amount}"/1000;
document.getElementById("{!op.id}").innerHTML=d;
</script>
< /apex:repeat>
< /table>

Message Edited by a_B on 01-07-2009 02:37 PM