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
kool_akool_a 

Passing data from a Html input Text to my Controller

Hi,

 

I have a controller that passes a list of objects to visualforce page. On this page I have an apex:repeater that iterates over these objects, each object is placed in an apex: PageBlockSection, then in this pageblocksection, I bind each object(List of inner objects) to an apex: pageBlockTable, then in each row of these tables(one table per section) I have html input elements of type text that house some fields from the inner objects. 

 

When a change is a made in the row of the table I want to pass back to my controller the Id of that object, the field that was changed and the value of the field. 

 

The save button is unique for every row and there is a javascript function that handles the onlick event.

In this javascript save function I pretty much have the object id, the field name and the value from the text box, I just don't know how to pass these to my controller.  Any suggestion is welcomed.

 

More Info:

I tried using apex: param but because of how dynamic this page is the value in the param becomes stale pretty quick. 

 

I am currently using jquery so I would want to avoid using any other javascript framework.

 

 

 

Thanks

 

K

Best Answer chosen by Admin (Salesforce Developers) 
kool_akool_a

I found a solution to this problem. I used a combination of <apex:inputHidden /> fields bound to some variables in my controller,  <apex:actionFunction />, and javascript.

 

So basically on the onClick of the save button click event, i get the objectid (embedded in Id the html form element), the name of field (also embedded in the id of the html form element), and the value.

Then in the javascript function i do my validation and then i save objectId, fieldName and the value in the <apex:inputHidden />, and then i call the <apex:actionFunction /> which calls a method in my controller which then gets the value from the variables that were bound to the <apex:inputHidden /> fields. I do my save to the object and then i refresh the page.

I could not use <apex: param /> because <apex: param /> will not work well in a grid, <apex: param /> will only get the value from the last row of the grid.

 

I can provide some code sample if anone wants to see

All Answers

Ron HessRon Hess
Apex Param is a good way to do this, the value in the param should be updated whenever the data is updated.
kool_akool_a

I found a solution to this problem. I used a combination of <apex:inputHidden /> fields bound to some variables in my controller,  <apex:actionFunction />, and javascript.

 

So basically on the onClick of the save button click event, i get the objectid (embedded in Id the html form element), the name of field (also embedded in the id of the html form element), and the value.

Then in the javascript function i do my validation and then i save objectId, fieldName and the value in the <apex:inputHidden />, and then i call the <apex:actionFunction /> which calls a method in my controller which then gets the value from the variables that were bound to the <apex:inputHidden /> fields. I do my save to the object and then i refresh the page.

I could not use <apex: param /> because <apex: param /> will not work well in a grid, <apex: param /> will only get the value from the last row of the grid.

 

I can provide some code sample if anone wants to see

This was selected as the best answer
ShikibuShikibu

Kool_a:

 

I've just worked out the same kind of scheme for getting data about selected rows from the inputCheckbox elements on rows in a pageBlockTable back into my controller, but it's still in rough form.

 

I would love to see your code. 

 

 

kool_akool_a

I have two scenarios, one when saving a specifc field for a specifc record and the other saving the whole record.
Mind you i used jquery here and there in my javascript functions. I have the javascript functions below. 
 
 
//Saving for a field this fires onclick of a save button beside the field. 

function Save(elementId)
{
//alert(elementId);
var readOnlyText = '#' + elementId.substr(0,elementId.length-1);
var editArea = readOnlyText + 'D';
var editText = readOnlyText + 'T';

var hdnobjectid = document.getElementById('{!$Component.hdnObjectId}');
var hdnfieldname = document.getElementById('{!$Component.hdnFieldName}');
var hdnvalue = document.getElementById('{!$Component.hdnValue}');

var objectid = elementId.substr(0,elementId.lastIndexOf('_'));
var name = elementId.substr(elementId.lastIndexOf('_')+1);
name = name.substr(0, name.length -1);
var value = $(editText).val();


//Update the hidden fields and then call the controller method to save to the database
hdnobjectid.value = objectid;
hdnfieldname.value = name;
hdnvalue.value = value;

// alert('Objectid=' + hdnobjectid .value + ' Fieldname=' + hdnfieldname.value+ ' value= ' + hdnvalue.value);
SaveFieldClient();

  //ui stuff (move to oncomplete of action function)

$(readOnlyText).text($(editText).val());
$(editArea).hide();
$(readOnlyText).show();
}

 

And this is the function for saving the whole record:

 

 

//onlick for a button on the row for a specific record
function SaveContent(objectId)
{
var hdnid = document.getElementById('{!$Component.hdnId}');
var hdnname = document.getElementById('{!$Component.hdnName}');
var hdnurlpath = document.getElementById('{!$Component.hdnUrlPath}');
var hdnstatus = document.getElementById('{!$Component.hdnStatus}');
var hdnupdatedby = document.getElementById('{!$Component.hdnUpdatedBy}');
var hdnreportsection = document.getElementById('{!$Component.hdnReportSection}');

var txtname = document.getElementById(objectId + '_Name');
var txturlpath = document.getElementById(objectId + '_UrlPath');
var ddlstatus = $('.'+ objectId +'_status');

var txtupdatedbyid = $('.'+ objectId +'_updatedby').attr('id');

//get value from loopup field

var hdnupdatedbyval = document.getElementById(txtupdatedbyid + '_lkid');


var txtreportsection = $('.'+ objectId +'_AssociatedReportSection').attr('id');
var hdnreportsectionval = document.getElementById(txtreportsection + '_lkid');

//alert(hdnupdatedbyval.value + ' ' + ddlstatus.val());

//Update the hidden fields and then call the controller method to save to the database
if(jQuery.trim(txtname.value) != '')
{
hdnid.value = objectId;
hdnname.value = txtname.value;
hdnurlpath.value = txturlpath.value;
hdnstatus.value = ddlstatus.val();
hdnupdatedby.value = hdnupdatedbyval.value;
hdnreportsection.value = hdnreportsectionval.value;

//alert('Objectid=' + hdnid.value + ' Name=' + hdnname.value+ ' urlpath= ' + hdnurlpath.value+ ' status= ' + hdnstatus.value + 'status updated by = ' + hdnupdatedby.value);
SaveReportContentClient();
}
else
{
alert('Content Name is required, please try again');
}
}

 

Then I have the action functions:

<apex:actionFunction action="{!SaveField}" name="SaveFieldClient"/>

<apex:actionFunction action="{!SaveReportContent}" name="SaveReportContentClient" />

 

The hidden fields:

<apex:inputHidden id="hdnObjectId" value="{!FieldToSave.ObjectId}" /> <apex:inputhidden id="hdnFieldName" value="{!FieldToSave.FieldName}" />
<apex:inputhidden id="hdnValue" value="{!FieldToSave.Value}"/>

<apex:inputHidden id="hdnId" value="{!RcToSave.Info.Id}" />
<apex:inputhidden id="hdnName" value="{!RcToSave.Info.Name}" />
<apex:inputhidden id="hdnUrlPath" value="{!RcToSave.Info.URL_Path__c}"/> <apex:inputhidden id="hdnStatus" value="{!RcToSave.Info.Status__c}"/><apex:inputhidden id="hdnUpdatedBy" value="{!RcToSave.Info.Status_Updated_By__c}"/>
<apex:inputhidden id="hdnReportSection" value="{!RcToSave.Info.Associated_Report_Template_Section__c}"/>

 

The html button for saving the record (this is inside a repeat vf component):

<input type="button" id="{!c.Info.Id}_S" class="btn" value="Save" onClick="SaveContent('{!c.Info.Id}')" style="display:none" />

 


 Kunle

 
 
 
Message Edited by kool_a on 09-28-2009 10:11 AM
ShikibuShikibu

I eventually understood that VisualForce is able to access apex class members. This enables a wrapper class solution to this problem. Here's a nice write-up on wrapper classes for visualforce by TehNrd.

 

For the problem I was working on, I wanted radio button behavior, not independently selectable rows. My wrapper class looks like this:

 

public class WrappedAsset
{
public Asset thisAsset {get; set;}
public Case theCase {get; set;}

public Boolean selected {
get {
return theCase.AssetId == thisAsset.Id;
}
set {
// this is actually accomplished by setting theCase.AssetId
}
}

public WrappedAsset(Asset a, Case c)
{
thisAsset = a;
theCase = c;
}

 

 and the checkboxes on the VF page look like this:

 

<apex:column headerValue="Select">
<apex:inputCheckBox value="{!asset.selected}">
<apex:actionSupport event="onclick"
action="{!selectAssetAction}"
status="updating_assets_status"
rerender="assets, the_asset"
immediate="false">
<apex:param name="assetId" value="{!asset.thisAsset.Id}"/>
</apex:actionSupport>
</apex:inputCheckBox>
</apex:column>

 

public void selectAssetAction() {
Id clickedAssetId = Utility.getUrlParameter('assetId');
if (clickedAssetId == theCase.AssetId) {
theCase.AssetId = null;
}
else {
theCase.AssetId = clickedAssetId;
}
}

 


 

 

 

Message Edited by Shikibu on 10-14-2009 01:51 PM
saharisahari

Hi

 

I have a similar requirement. I am using jquery script to calcualte a value. now I need to pass this value to the controller.

 

Please help me with some example code.

 

this jquery scripts is setting the calculated value to a html imput field but its not setting the same value to apex inputtext field.

Does any one have an idea as to how to set this calculated value in an apex: inputtext field?

 

Need urgent help on this.!! Any help is greatly appreciated.

 

 

ShikibuShikibu