You need to sign in to do that
Don't have an account?
refer to the current element in a soql query
<apex:form > <apex:dataList value="{!roots}" var="r"> <apex:commandLink action="{!showChildren}" reRender="renderArea"> {!r.name} </apex:commandLink> </apex:dataList> </apex:form>
what i want to do now is this:
when an item is clicked, i want to return a list of child elements and display their name in the outputPanel named renderArea
this is what my action method showChildren looks like:
public List<Element__c> showChildren(){ return [select name,id from Element__c where parent__c = the ID of the record I clicked]; }
Two questions:
1. How do I refer to the selected element in a soql query? I have tried the this keyword with no success eg this.Element_Id__c.
2. I know one reason why this isnt working is because an action method must return a page reference. is there a way to create a custom action in apex that doesn't rely on page references or am i barking up the wrong tree here?
Make the following changes to your visualforce page and the controller and see if it works:
<apex:form >
<apex:dataList value="{!roots}" var="r">
<apex:commandLink action="{!showChildren}" reRender="renderArea">
{!r.name}
<apex:param value="{!r.id}" assignTo="{!recordId}"/>
</apex:commandLink>
</apex:dataList>
</apex:form>
public string recordId {get;set;}
public List<Element__c> getshowChildren(){
return [select name,id from Element__c where parent__c =:recordId];
}
If this works, the in another output panel you can display showChildren list and use rendered attribute in the outputpanel as
rendered="{!showChildren()!=NULL}"
Let me know if you face any difficulty...
So it doesn't like an action method referring to a getter, as shown below. The page throws an error Unknown method 'SignalParser.showChildren()
If I remove the get prefix from getshowChildren in the controller, the page will compile and render the original list of root elements as command links, as expected.
However when I click one of the command links and I get a visualforce error saying that the action method must be a page reference.
It seems the solution is to set recordId to a page reference, not a string. But when I try casting the string as a PageReference type, I get a compile error of Incompatible types since an instance of String is never an instance of System.PageReference. Did I do the casting correctly?
Here is the Sample Code that is workign fine form me:
Visualforce Page
<apex:page controller="acccontroller">
<apex:form >
<apex:dataList value="{!roots}" var="r">
<apex:commandLink action="{!children}" value="{!r.name}" rerender="out">
<apex:param name="paramval" value="{!r.Id}" assignTo="{!aId}" />
</apex:commandLink>
</apex:dataList>
<apex:outputPanel id="out">
<apex:dataList value="{!showChildren}" var="s">
{!s.Name}
</apex:dataList>
</apex:outputPanel>
</apex:form>
</apex:page>
Controller:
public class acccontroller {
public String aId {get;set;}
public List<Account> aList;
public PageReference children() {
system.debug(aId);
aList = [select Name from Account where ParentId=:aId];
return null;
}
public List<Account> getshowChildren(){
return aList;
}
public List<Account> getRoots(){
return [select Id, Name from Account];
}
}
Hope this helps you to change your code accordingly........
I was finally able to make it work by not referring to it as an action function. This code is very helpful - thanks.