Visualforce Repeaters - salesforce

Hi I am getting a headache with the visualforce repeater control:
<apex:repeat value="{!productDetails}" var="o">
<apex:pageBlockSectionItem >
<apex:outputText style="label" value="Name:"></apex:outputText>
<apex:outputLabel value="{!o.name}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="{!o.ProductCode}"/>
<apex:outputText value="{!o.Family}" />
</apex:pageblockSection>
<apex:pageBlockSection>
<apex:outputText style="label" value="Quantity:"> </apex:outputText>
<apex:inputText value="{!theList}"/>
</apex:pageblockSection>
</apex:repeat>
What I am trying to do, is for each record in the product details list, generate a text box. This text box is bound to another field (theList.quantity). But I am finding that when I change the value in the last text box it sets the quantity in all the text boxes(obviously as they are bound to the same field).
So my question is whats the best way to have each textbox that is generated in the repeater have its own parameter value?
Or am I using the repeater in the wrong way?
EDIT(clarification):
For each product detail record(What I am iterating through) I want to have a textbox where a user can enter the quantity of products. The value of this textbox is unrelated to the product detail records.
My question is how can I generate unique parameters for each iteration of the textbox? Hopefully that makes sense.
Cheers

You need to use the var in the <apex:repeat> for each element in the productDetails list. For example:
<apex:repeat value="{!productDetails}" var="productDetail">
<apex:pageBlockSection>
<apex:outputText style="label" value="Quantity:"> </apex:outputText>
<apex:inputText value="{!productDetail.quantity}"/>
</apex:pageblockSection>
</apex:repeat>
That will set the the quantity property of each productDetail.
If you're really iterating over a parent of the productDetail in this example, then you'll need to change your controller to create a parent for each and then iterate over that. I'll write the example code as if you're iterating over a list of potential orders.
In your controller, you'll need to create an order for each of the products. I'm not sure if the parent is an SObject or a custom class, but I'll write the example as if it was a custom class.
public class Order {
public Order(ProductDetail productDetail) {
this.productDetail = productDetail;
}
public ProductDetail productDetail;
public Integer quantity;
}
// I assume you've already implemented this getter or are setting it some other way.
public ProductDetail[] productDetails {
get {
if (productDetails == null) {
productDetails = ...;
}
return productDetails;
}
set;
}
public Order[] orders {
get {
if (orders == null) {
orders = new Order[]{};
for (ProductDetail productDetail: productDetails) {
orders.add(new Order(productDetail);
}
}
return orders;
}
set;
}
Now, in your VisualForce page you can iterate over the Order list and have the user set the quantity.
<apex:repeat value="{!orders}" var="order">
...
<apex:outputtext style="label" value="Name:"></apex:outputText>
<apex:outputLabel value="{!order.productDetail.name}" />
...
<apex:inputText value="{!order.quantity}"/>
....
</apex:repeat>
Back in your controller, only save the orders that have a quantity of more than zero (or whatever other criteria you have in mind).

The easiest way to do this is to add a custom field to the object you're repeating through. However, I understand there are cases where that's not desirable.
You probably could use a custom Apex object (not custom Salesforce object) for this. You can do this by adding a nested class within your Controller or Extension.
For example:
public class MyClass
{
public class MyProductDetail
{
public string Name {get;set;}
public string Family {get;set;}
public string ProductCode {get;set;}
public integer Quantity {get;set;} // theList
}
public List<MyProductDetail> MyProductDetails {get;set;}
}
You would need to loop through all of your Product detail records (returned from SOQL), and add them to the MyProductDetails list. Once you have that, though, you can use a Visualforce Repeater to display each of them them and save the inputted quantity data for each record.
<apex:repeat value="{!MyProductDetails}" var="o">
<apex:pageBlockSectionItem >
<apex:outputText style="label" value="Name:"></apex:outputText>
<apex:outputLabel value="{!o.Name}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="{!o.ProductCode}"/>
<apex:outputText value="{!o.Family}" />
</apex:pageblockSection>
<apex:pageBlockSection>
<apex:outputText style="label" value="Quantity:"> </apex:outputText>
<apex:inputText value="{!o.Quantity}"/>
</apex:pageblockSection>
</apex:repeat>
Hope that helps!

Related

Selecting picklist option on Visual force page upon load

I have a visualforce page that has a picklist called Topic and sometimes I will need to select one of the picklist options upon page load (meaning the Topic will be passed on from another page and will need to be selected upon loading the page for the first time). I'm not sure how to do this? I'm posting part of the Visualforce page that handles topic selection and the Controller code that below. Any help would be appreciated.Thanks.
Visualforce page:
<!---------------------------------- Select Topic ----------------------------------------------------->
<apex:pageblockSection title="Select the Topic" >
<apex:selectList value="{!topic}" size="1">
<apex:outputlabel for="Topic" value="Pick a Topic :" ></apex:outputlabel>
<apex:selectOptions id="topic" value="{!Topics}"/>
<apex:actionSupport action="{!populateParameters}" reRender="parametersSection,querySection" event="onchange"/>
</apex:selectList>
</apex:pageblockSection>
<!---------------------------------- End of Select Topic ---------------------------->
<!---------------------------------- Parameters for Topic ----------------------------------------------------->
<apex:pageblockSection id="parametersSection" title="Input Parameters">
<apex:repeat value="{!topicpParamWrapperList}" var="params">
<apex:outputPanel >
<apex:outputlabel value="{!params.parameter.Name}" ></apex:outputlabel>
<apex:inputfield value="{!params.parameter.inputValue__c}" rendered="{!params.renderAsText}">
<apex:actionsupport action="{!placeValuesInQuery}" reRender="querySection,splunUrlLink" event="onchange"/>
</apex:inputfield>
<apex:inputfield value="{!params.parameter.DateTimeValueHolder__c}" rendered="{!params.renderAsDate}">
<apex:actionsupport action="{!placeValuesInQuery}" reRender="querySection,splunUrlLink" event="onchange"/>
</apex:inputfield>
</apex:outputPanel>
</apex:repeat>
</apex:pageblockSection>
<!---------------------------------- End of Parameters for Topic ----------------------------------------------------->
Apex Controller
public List < topicpParamWrapper > topicpParamWrapperList {
get;
set;
} {
topicpParamWrapperList = new List < topicpParamWrapper >();
}
public void populateParameters()
{
if(!topicpParamWrapperList.isEmpty())
{
topicpParamWrapperList.clear();
}
if(topic!='' && topic!=Null)
{
for(Query_Parameter__c qParam :[select id, Parameters__r.Variable_Name__c, Parameters__r.Type__c,Parameters__r.Name from Query_Parameter__c where Topics__c=:topic])
{
Parameters__c param = new Parameters__c();
param.Name =qParam.Parameters__r.Name ;
param.type__c = qParam.Parameters__r.type__c;
param.Variable_Name__c=qParam.Parameters__r.Variable_Name__c;
topicpParamWrapperList.add(new topicpParamWrapper(param));
}
getQueryToRun();
}
}
public void getqueryToRun(){
if(mapTopics.containsKey(topic))
{
this.queryToRun =mapTopics.get(topic).query__c;
this.queryMain=mapTopics.get(topic).query__c;
}
}
public List < topicpParamWrapper > paramList {
get;
set;
} {
paramList = new List <topicpParamWrapper>();
}
All you really have to do is to set the topic to some initial value in the constructor (the special function that has name identical to class' name). You set it to some value and then it'll be rendered properly in visualforce (assuming same value is one of the selectable options!).
You have omitted the constructor or <apex:page> tag so we don't know how you're navigating to that page. But probably easiest for you would be to pass the topic in the URL. So if you access the page like that:
/apex/MyPage?topic=Something, something
then in the constructor you could do this:
topic = ApexPages.currentPage().getParameters().get('topic');
(the name of the URL parameter doesn't have to be same as the variable name but it makes sense to have them at least similar)
You can read more about getParameters()
If there is risk that your topic will contain &, spaces etc you probably should URLENCODE it when building the link.

radio button value not visible in vfpage

im tring to display records in question__c object along with fields in object as radio button. but im not able to get radio button value. getting only radio button without name.
im new to salesforce.if this is very basic question pls excuse me.
<apex:page standardController="question__c" extensions="GetQuestionList" >
<apex:form >
<apex:repeat value="{!que}" var="a">
<apex:pageBlock >
<apex:pageBlockSection columns="1">
{!a.Quiz_question__c} {!a.id} <br/>
<apex:selectRadio value="{!selectedAns}" >
<apex:selectOption itemValue="{!opt3}" itemLabel="{!a.option_3__c}"/>
<apex:selectOption itemValue="{!opt4}" itemLabel="{!a.option_4__c}"/>
</apex:selectRadio>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:repeat>
</apex:form>
</apex:page>
____mycontroller is
public class GetQuestionList {
question__c q1;
public List<question__c> que{set;}
public List<question__c> getque(){
List<question__C> que= new List<question__c>();
for(question__C q:[select Quiz_question__c,id from question__c]){
que.add(q);
system.debug(que);
}
system.debug(que);
return que;
}
public String selectedAns{get;set;}
public String opt2{get;set;}
public String opt4{get;set;}
public String opt3{get;set;}
public GetQuestionList(ApexPages.StandardController controller) {
}
}
The reason you have no label is because the value field on the <apex:selectRadio> is a String variable, rather than a field. If you want to add a label, there is a label attribute on that visualforce component you can use.
If you want the label to be dynamically shown based on an actual field's label in the data model, you need to set the value field to that field. An example of that would be if you had an Answer__c field on the Question__c object.
<apex:selectRadio value="{!a.Question__r.Answer__c}">
<apex:selectOption itemValue="{!opt3}" itemLabel="{!a.option_3__c}"/>
<apex:selectOption itemValue="{!opt4}" itemLabel="{!a.option_4__c}"/>
</apex:selectRadio>
That value field would use the label assigned to that field in the object model by default.

Invalid field Email for SObject AggregateResult in VisualForce

I want to display the AggregateResult on my Visualforce page but it is generating the following error " Invalid field Email for SObject AggregateResult"
Below is my code:
public with sharing class searchDuplicate {
public AggregateResult[] con{get;set;}
public searchDuplicate()
{
find();
}
public void find(){
con = [select Email from Contact group by Email having count(Email) > 1];
System.debug(con);
}
}
Below is my visualforce page code:
<apex:page controller="searchDuplicate">
<apex:pageBlock title="Searching for Duplicate Contacts Record">
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:dataTable value="{!con}" var="c" border="2" cellspacing="5" cellpadding="5">
<apex:column headerValue="Email" value="{!c['Email']}" />
</apex:dataTable>
</apex:pageBlock>
</apex:page>
kindly Make a correction if possible
public with sharing class searchDuplicate {
public list <con> conList{get;set;}
public class con{
public string Email {get;set;}
public con( string email){
this.Email = email;
}
}
public searchDuplicate()
{
find();
}
public void find(){
conList = new list <con>();
for( AggregateResult ar : [select Email from Contact group by Email having count(Email) > 1];){ conList.add(new con(string.valueOf(ar.get('Email')))) }
}
}
Aggregate results (and their fields/columns) come as generic Objects, not sObjects. Therefore there's no obj.get('somefieldname') you can call on them.
Your best option might be to make a helper class that has String email; Integer count; fields and loop through the query results populating a list of objects of this helper class.
You could also use Map<String, Integer> but that'd come to VF as not sorted alphabetically.
See https://salesforce.stackexchange.com/questions/7412/count-a-grouped-by-soql if you need a code sample.
Although the other answers are correct in solving your problem, what you have is actually a bug in SalesForce.
The way to resolve this without creating a custom object is to separate the into two - The "headerValue" and "value" both being set cause this error.
You want to change it to this:
<apex:dataTable value="{!con}" var="c" border="2" cellspacing="5" cellpadding="5">
<apex:column headerValue="Email" >
<apex:outputText>{!c['Email']}</apex:outputText>
</apex:column>
</apex:dataTable>
Thanks,
Michael

How can I link labels in a SelectCheckBoxes component in Visualforce?

I have an <apex:SelectCheckBoxes> component in my Visualforce page, which gets its select options from an Apex method. I want to have a label with a link appearing next to each checkbox. How can I achieve this? Please help.
One way to accomplish this is by using apex:inputCheckbox controls individually, rather than relying on selectCheckboxes to do all the rendering for you. It's hard to get more specific in a suggestion because the question can be answered in many ways.
If, for example, you need these checkboxes to appear in a list alongside SObject instances, create a wrapper class. If they are essentially a-la-carte, you can create a class that contains a Boolean and create a list of instances of this class. Then create a dataTable, pageBlockTable, etc., and in one of the columns you include the checkbox component. Or simply use apex:repeat if you don't want any of the other table formatting.
Here's the repeat example from the VF guide:
<!-- Page: -->
<apex:page controller="repeatCon" id="thePage">
<apex:repeat value="{!strings}" var="string" id="theRepeat">
<apex:outputText value="{!string}" id="theValue"/><br/>
</apex:repeat>
</apex:page>
/*** Controller: ***/
public class repeatCon
{
public String[] getStrings()
{
return new String[]{'ONE','TWO','THREE'};
}
}
Replacing outputText with inputCheckbox and a String array with Boolean. Then simply start with an apex:outputLink and follow it with the checkbox.
EDIT -
Here's an example of using a class to get the job you want done.
Apex:
public class Example
{
public List<CheckboxClass> theCheckboxes {get; private set;} // Reference THIS array
public Example()
{
theCheckboxes = new List<CheckboxClass>();
theCheckboxes.add(new Checkbox(true));
theCheckboxes.add(new Checkbox(false));
theCheckboxes.add(new Checkbox(true));
theCheckboxes.add(new Checkbox(false));
}
public class CheckboxClass
{
public Boolean theCheckbox {get; private set;}
public CheckboxClass(Boolean b)
{
this.theCheckbox = b;
}
}
}
Visualforce:
<apex:form>
<apex:pageBlock>
<apex:pageBlockTable value="{!theCheckboxes}" var="item">
<apex:column headerValue="The Checkboxes">
<apex:inputCheckbox value="{!item.theCheckbox}">
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
I haven't tested this, but this is the idea I believe you're looking for.

inputField databinding issue with VisualForce

I'm having strange results with a Visualforce page (yes, Salesforce.com is icky, I know). My issue is that I am trying to use the inputField to bind data on a custom sObject, but in my custom controller, it is not recognizing the user input data.
Here is the code snippet from the page:
<apex:pageBlockSection title="Enter New Fee" rendered="{!isRenderedFees}" >
<apex:inputField value="{!workingFee.Fee_Type__c}" required="True"/>
<apex:inputField value="{!workingFee.Fee__c}" required="True"/>
<apex:pageBlockSectionItem >
<apex:CommandButton value="Save Fee" action="{!saveFee}" immediate="true" />
<apex:CommandButton value="Cancel" action="{!cancelFee}" immediate="true" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
and here is the code from the controller:
public Fee__c workingFee {get; set;}
....
public PageReference saveFee(){
this.workingFee.Trade_Group__c = tradeGroup.id;
try{
System.debug('~~~~#~~#~~workingFee: '+workingFee.Fee_Type__c +'='+workingFee.Fee__c);
upsert workingFee;
}catch (System.Dmlexception e){
ApexPages.addMessages(e);
return null;
}
System.debug('~~~~#~~#~~workingFee: '+workingFee.Fee_Type__c +'='+workingFee.Fee__c);
//savedFees.add(workingFee.clone());
//re-render the page
this.isRenderedFees = False;
return null;
}
I've made sure the workingFee property is not null. Whenever I hit the "Save Fee" button after entering the values, it reloads the page and gives me the message "Error: Required fields are missing: [Fee__c]" (note, Fee__c here is a currency field -- it's not that it expects this to be an sObject, is it?)
The debug statement in the saveFee() method shows that workingFee's important fields are null, when I would expect them to have been assigned the values input by the user.
I have had a whole heap of issues binding controls to a property exposed with the simple { get; set; } notation... The rest of your code will see the properties, but for some bizarre reason, your View won't (always) bind...
Try writing explicit get/set methods, like
private workingFee;
public Fee__c getWorkingFee() {
return workingFee;
}
public void setWorkingFee(Fee__c value) {
workingFee = value;
}
There is no logical reason why this should work any different to
public Fee__c workingFee { get; set; }
but in my experience, it sometimes does...
what did you say about it being icky? ;)
Immediate=false/true has bitten me quite a few times too. Good catch Ian.

Resources