function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kamran-RaoKamran-Rao 

Populate Value Attribute in apex:inputField Through Javascript

<script> function getValue () { var myString = 'Test Value'; return myString; } </script> <apex:pageBlockTable value="{!myData}" var="aMyData" id="MyData"> <apex:column headerValue="My Custom Value"> <!--getValue() is my supposed javascript function that will return me the value -> <apex:outputField value="{!$getValue ();}"/> </apex:column> </apex:pageBlockTable>

 

I want to get some value placed in the 'value' attribute of my apex:outputFiled/apex:inputFiled through a javascript. Is it possible?
Following is the code for what i want to do (this code will not work, just to explain what I want to do)

 

Regards,

Rao

 

 

Message Edited by Kamran-Rao on 06-30-2009 04:59 AM
bbrantly1bbrantly1

The best way to use a custom list from an object is to your do the manipulating in the controller.  Take a look at this quick example.

 

Approved_Commission__c is a Custom Object I have created

SomeObjs is a class

 

Controller:

 

 

public class TestForBlog {

public List<SomeObjs> getValues()
{
List<SomeObjs> CustomList = new List<SomeObjs>();

List<Approved_Commission__c> queryResult = [SELECT Id,Total__c FROM Approved_Commission__c];
if(queryResult.size()>=1)
{
for(integer i=0;i<queryResult.size();i++)
{
Approved_Commission__c obj = queryResult[i];
SomeObjs o = new SomeObjs();
o.Total = obj.Total__c;
o.CustomValue = (obj.Total__c * 10);
CustomList.add(o);
}
}
return CustomList;
}

public class SomeObjs
{
public Decimal Total {get;set;}
public Decimal CustomValue {get;set;}
public SomeObjs() { } //Constructor You could pass your values here
}
}

 

Visualforce Page:

 

 

<apex:page controller="TestForBlog">
<apex:form >

<apex:pageBlock >
<apex:pageBlockTable value="{!Values}" var="a">
<apex:column headerValue="Total">
<apex:outputText value="{!a.Total}"/>
</apex:column>
<apex:column headerValue="My Custom Value">
<apex:outputText value="{!a.CustomValue}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>

</apex:form>
</apex:page>

 

Your Output would be as follows:



Total

My Custom Value



5684.0

56840.0



8386.8

83868.0


Message Edited by bbrantly1 on 06-30-2009 09:25 AM
bbrantly1bbrantly1

You can of course access visualforce outputs with javascript using the below method:

 

Visualforce Page:

 

<apex:page controller="TestForBlog">
<apex:form>
<script>
function getValue ()
{
var myValue = document.getElementById('{!$Component.IdHERE}').innerHTML;
alert("Your Value Is: " + myValue);
}
</script>
<a href="#" onclick="getValue()">Click Here for Value</a>
<br/>
<apex:outputText id="IdHERE" value="{!AValue}"></apex:outputText>
</apex:form>
</apex:page>

 You have to give the apex outputText an Id and reference it in your javascript.

 

 

Kamran-RaoKamran-Rao

Brantly,

 

Thank you very much for your code and advice. Let me clear you my actual scenario, If you enable history tracking for a custom object and its fields say 'MyObject', system automatically creates a history table named 'MyObject__History' for that object, where all changes are automatically

tracked. Now I want to display this data in my custom <apex:pageBlockTable> and want to change some fields value on some condition. When I find that condition true and try to change the value it gives me exception saying that field is not writable since that is system generated object.

 

I thought a workaround of it by letting it go as it comes in apex class and change the same thing through javaScript as soon as the pageBlock or pageBlockTable is rendered or refreshed. In fact I want all values in a specific column of page block to be checked if they end with '__C' then trim their last three characters.

 

Rao

bbrantly1bbrantly1

Kamran-Rao,

 

Could you please send me a small portion of the Custom Object Layout (Fields) and then maybe a sample of a normal output in a table format if you just list the contents of the object and then a modified layout for which you're trying to archive?

Kamran-RaoKamran-Rao

My apologies for being late in replying... have been out of town. I just want to do my formatting on a single column of mey apex:pageBockTable. I am providing you the output column and desired output. I have total 5 columns in my output table (rest do'nt need any formatting).

 

Field Changed (current Format) |Field Changed (Desired Format)
=================       |==============
OAS_System__c                       |OAS System
Pixel_Usage__c                         |Pixel Usage
Tag_Type__c                            |Tag Type
Agency__c                                |Agency
Advertiser__c                            |Advertiser

 

I have tried a number of solutions, including creating another object and putting same fields in that. Then in apex code trimming '__c' and putting it in corresponding field in new object and binding the new object with the table. But this solution is having its own issues. Changing the column value at rendering time though javascript seems the best option to me.

 

Rao

ShikibuShikibu

Kamran,
 
I think perhaps your javascript does not work because the js runs at the wrong time. I have a VF page that uses js to update a field value. This is an input form that sets the CompanyName whenever the First or Last name is changed. I've stripped the code down to its bare essentials:

 

 

<apex:page controller="ServiceTagController"> <script> function setAccountName() { accountName.value = 'foobar'; } </script> <apex:form> <apex:pageBlock title="Customer Information" mode="edit"> <apex:inputField value="{!theContact.firstName}" id="FirstName" onchange="javascript:setAccountName();"/> <script>var firstName = document.getElementById("{!$Component.FirstName}");</script> <apex:inputField value="{!theContact.lastName}" id="LastName" onchange="javascript:setAccountName();"/> <script>var lastName = document.getElementById("{!$Component.LastName}");</script> <apex:inputField value="{!theAccount.Name}" id="AccountName" styleClass="createNewAccount"/> <script>var accountName = document.getElementById("{!$Component.AccountName}");</script> </apex:pageBlock> </apex:form> </apex:page>

 

 

 

 

bcgbcg

Hi there,

 

I want the same thing i.e. want to populate the output field through javascript and i am using your code,but the problem is in Avalue method you used in it, will you please post the code for this method or if possible the code for your controller.

 

 

Thanks.

bcgbcg

Hi there,

 

I have the same requirement but the problem is the method "Avalue" you are calling is not clear .

will you please post the code for that method.Or the code for your controller.

 

Thanks in advance.

bcgbcg

Hi there,

 

Will you please post the code for your controller where you are defining "Avalue()"

 

 

Thanks.