You need to sign in to do that
Don't have an account?
James@KronosLab
How do you call an action from Javascript?
I'd like to call an action when a select is changed (using onchange). However, a plain old submit doesn't call any action. Looking at what a commandbutton generates, it's pretty hairy and specific, and I can't see a way to emulate it in user-written Javascript. Any help here?
James
James
Take a look at apex:actionSupport - it allows you wire up any dom event
to an action - all without code (well a/out you writing any js - that's the visualforce team's job)!
FYI at some point in rhe future I do expect to provide a supported
js API equivalent for those rare occasions where the component
approach is not a good fit...
Message Edited by dchasman on 10-18-2007 05:30 PM
<apex:outputpanel>
<apex:selectOneListbox value="{!currentEmployee}" size="1">
<apex:actionsupport event="onchange" action="{!changeEmployee}"/>
<apex:selectItems value="{!employeeList}" />
</apex:selectOneListbox>
</apex:outputpanel>
<apex:outputpanel>
<apex:actionsupport event="onchange" action="{!changeEmployee}"/>
<apex:selectOneListbox value="{!currentEmployee}" size="1">
<apex:selectItems value="{!employeeList}" />
</apex:selectOneListbox>
</apex:outputpanel>
<apex:outputpanel>
<apex:actionsupport event="onchange" action="{!changeEmployee}">
<apex:selectOneListbox value="{!currentEmployee}" size="1">
<apex:selectItems value="{!employeeList}" />
</apex:selectOneListbox>
</apex:actionsupport>
</apex:outputpanel>
I expect that you do not have an <apex:form></apex:form> that contains your input/select elements. Without that you should be getting a javascript error complaining about this._form has no properties or some such nonsense.
In Winter '08 we have added save time validation for this kind of thing (although while investigating your question I found that this specific component does not check to insure it has been nested in a form - bug opened for that now).
Nope, it's inside a form. Here's the complete snippet:
<apex:form><center>
<apex:outputpanel>
<apex:selectOneListbox value="{!currentEmployee}" size="1">
<apex:actionsupport event="onchange" action="{!changeEmployee}"/>
<apex:selectItems value="{!employeeList}" />
</apex:selectOneListbox>
</apex:outputpanel>
<apex:commandLink action="{!lastMonth}"><-------</apex:commandLink>
<apex:commandLink action="{!nextMonth}">-------></apex:commandLink>
</center>
However... actionsupport have the same behavior as setting an action on a commandbutton. The later causes the entire page to refresh, whereas actionsupport seems unable to do anything but a partial update, which makes my page break badly. I've worked around it for the moment by putting a commandbutton next to the select to fire the action, but what would be really useful is a javascript function you can call to cause an action call with full refresh.
James
<apex:page controller="Simple" showheader="false">
<apex:form>
<apex:outputText id="emp" value="The current employee: {!currentEmployee}"/><br/>
<apex:outputpanel>
<apex:selectOneListbox value="{!currentEmployee}" size="1">
<apex:actionSupport event="onchange" action="{!changeEmployee}" rerender="emp"/>
<apex:selectItems value="{!employeeList}" />
</apex:selectOneListbox>
</apex:outputpanel>
</apex:form>
</apex:page>
and the controller
public class Simple {
public SelectOption[] getEmployeeList() {
return new SelectOption[] { new SelectOption('A', 'A'), new SelectOption('B', 'B') };
}
public PageReference changeEmployee() {
return null;
}
public String getCurrentEmployee() {
return employee;
}
public void setCurrentEmployee(String employee) {
this.employee = employee;
}
public Simple() {
}
private String employee;
}
Can you please provide your entire page and controller? - I am just not following the thread of what you want to have happen versus your attempted workaround - thanks
Message Edited by dchasman on 10-19-2007 10:16 PM