Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Resolved! about linking two autocompleters using topics

Giovanni Azua

2007-12-04

Replies:

Hi Jeromy,

Many thanks for your response!

Jeromy Evans wrote:
> I don't use dojo's autocompleter myself, but after a quick look at the
> javascript source for the widget (ComboBox) I can see there's a public
> method setSelectedValue(value) which may help, although it's not clear
> to me which component of the ComboBox is updated....The js source is
> pretty horrible.
By doing detective work on the scattered documentation finally found a
working solution!
might not be the most beautiful one but works for me. It is not the most
beautiful one
because the target Ajax Action returns only one value so I had to
hardcode the key
to be e.g. "value" and by knowing this key in the action and in the
javascript then I use
the autocompleter setSelectedKey("value") and then it displays it
properly. I also
hide the fact that the text field is an autocompleter by using the
showDownArrow="false".

For the records please find all the relevant sources below.

Many thanks!

Best regards,
Giovanni

Setup:

Struts 2.1.1 built from sources recently approx 25.11.2007
JSON plugin 0.19

Example request/response:
Request:
AjaxGetExchangeRates.action?historicBook0Key=AZN%20GB.GBP.LON.1.00.stock&historicBook1Key=AZN%20GB.GBP.LON.1.00.stock
Response:
{"eurosPerListedUnit0":{"1.393":"value"},"eurosPerListedUnit1":{"1.393":"value"}}

**************************** struts.xml *******************************

<package name="ajax" extends="json-default">
 <action name="Ajax*"
class="com.sag.optimizer.ui.web.action.ajax.Ajax{1}Action">
   <result type="json" />
 </action>
</package>

**************************** AjaxGetExchangeRatesAction ***************

public
class AjaxGetExchangeRatesAction
extends ActionSupport
{
 
//------------------------------------------------------------------------
  // public
 
//------------------------------------------------------------------------
  /**
  * @return the eurosPerListedUnit0
  */
  public final Map<String, String>
  getEurosPerListedUnit0()
  {
    Map<String, String> myMap = new HashMap<String, String>();
   
myMap.put(String.valueOf(calculateExchangeRate(theHistoricBook0Key)),
       "value");    
    return myMap;
  }

 
//------------------------------------------------------------------------
  /**
  * @param aHistoricBook0Key the historicBook0Key to set
  */
  public final void
  setHistoricBook0Key(String aHistoricBook0Key)
  {
    theHistoricBook0Key = aHistoricBook0Key;
  }

 
//------------------------------------------------------------------------
  /**
  * @return the eurosPerListedUnit1
  */
  public final Map<String, String>
  getEurosPerListedUnit1()
  {
    Map<String, String> myMap = new HashMap<String, String>();
   
myMap.put(String.valueOf(calculateExchangeRate(theHistoricBook1Key)),
       "value");    
    return myMap;
  }

 
//------------------------------------------------------------------------
  /**
  * @param aHistoricBook1Key the historicBook1Key to set
  */
  public final void
  setHistoricBook1Key(String aHistoricBook1Key)
  {
    theHistoricBook1Key = aHistoricBook1Key;
  }

 
//------------------------------------------------------------------------
  // private
 
//------------------------------------------------------------------------
  private double
  calculateExchangeRate(String aHistoricBookKey)
  {    
    ...
  }  
   
 
//------------------------------------------------------------------------
  // members
 
//------------------------------------------------------------------------
  private String theHistoricBook0Key;
  private String theHistoricBook1Key;
 
  private static final long serialVersionUID = 1L;
}

**************************** mypage.jsp ******************************

<%@(protected)"%>
<%@(protected)"%>
<%@(protected)" %>

<script type="text/javascript">
dojo.event.topic.subscribe("/afterExchangeRateReloaded",
function(data, request, widget)
{
  //data : text returned from request
  //request: XMLHttpRequest object
  //widget: widget that published the topic
  widget.setSelectedKey("value");
});
</script>

<s:url var="exchangeRates" action="AjaxGetExchangeRates.action" />

<s:form id="formSimulation" action="%{targetAction}" method="post"
theme="%{currentTheme}">
  ...
  <sx:autocompleter tooltip="Provide the first Historic Book"
valueNotifyTopics="/changedHistoricBook0" searchType="substring"
label="Historic Book 1" cssStyle="width: 250px;" dropdownHeight="180"
name="historicBook0" list="historicBooks" listKey="id" listValue="name"
/>    
  <sx:autocompleter tooltip="Provide the second Historic Book"
valueNotifyTopics="/changedHistoricBook1" searchType="substring"
label="Historic Book 2" cssStyle="width: 250px;" dropdownHeight="180"
name="historicBook1" list="historicBooks" listKey="id" listValue="name" />
  ...
  <sx:autocompleter id="eurosPerListedUnit0" tooltip="Provide the
Currency Rate 1" formId="formSimulation" showDownArrow="false"
    cssStyle="width: 250px;" href="%{#exchangeRates}"
listenTopics="/changedHistoricBook0"
    label="Currency Rate 1" name="eurosPerListedUnit0"
autoComplete="false" afterNotifyTopics="/afterExchangeRateReloaded" />
  <sx:autocompleter id="eurosPerListedUnit1" tooltip="Provide the
Currency Rate 2" formId="formSimulation" showDownArrow="false"
    cssStyle="width: 250px;" href="%{#exchangeRates}"
listenTopics="/changedHistoricBook1"    
    label="Currency Rate 2" name="eurosPerListedUnit1"
autoComplete="false" afterNotifyTopics="/afterExchangeRateReloaded"
/>    
  <s:submit value="%{buttonLabel}" align="center"/>
</s:form>




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.