You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Madhav Bhargava <un...@gmail.com> on 2007/06/09 22:53:22 UTC

I just cannot make InputSuggestAjax component work

Hi All,

Environment:
Weblogic 8.1
JDK 1.4.2.x
Myfaces 1.1.5
Tomahawk 1.1.5
Tomahawk sandbox 1.1.5

I have taken the lead from the example at http://www.irian.at which is again
present at http://wiki.apache.org/myfaces/InputSuggestAjax

Instead of an Address bean i have a KeyValuePair bean. This bean has 2
properties - key (String), value (Object). I want to display the values in
the drop down that would come up when the user starts typing something and
then the selected bean will then be passed to the model. Using the key field
of the selected bean i will then compose a query for the database.

JSP code:
<s:inputSuggestAjax suggestedItemsMethod="#{ajaxsuggest.suggest}"
    itemLabelMethod="#{ajaxsuggest.getLabel}" value="#{
ajaxsuggest.selectedBean}">
    <f:converter converterId="inputSuggestAjaxConverter"/>
</s:inputSuggestAjax>

Methods in Controller/Managed bean for inputSuggestAjax component:

    public List suggest(String prefix) {
        List matchingList = new ArrayList();
        Iterator iter = items.iterator();
        while(iter.hasNext()) {
            KeyValuePair bean = (KeyValuePair)iter.next();
            if (bean.getValue().toString().startsWith(prefix)) {
                matchingList.add(bean);
            }
        }
        return matchingList;
    }

    public String getLabel(Object selectedBean) {
        if (null != selectedBean && selectedBean instanceof KeyValuePair) {
            Iterator iter = items.iterator();
            while(iter.hasNext()) {
                KeyValuePair bean = (KeyValuePair)iter.next();
                if
(bean.getKey().equals(((KeyValuePair)selectedBean).getKey()))
{
                    return (String)bean.getValue();
                }
            }
        }
        return null;
    }

Just to test i am populating the items List in the controller itself:
    public InputSuggestAjaxController() {
        items = new ArrayList();
        items.add(new KeyValuePair("1", "India"));
        items.add(new KeyValuePair("2", "US"));
        items.add(new KeyValuePair("3", "Russia"));
        items.add(new KeyValuePair("4", "China"));
        items.add(new KeyValuePair("5", "Indonasia"));
        items.add(new KeyValuePair("6", "Iran"));
        items.add(new KeyValuePair("7", "Uganda"));
    }

Custom Converter for this component:
    public Object getAsObject(FacesContext context, UIComponent component,
String value)
            throws ConverterException {

        if (null != value) {
            KeyValuePair bean = new KeyValuePair();
            bean.setKey(value);
            return bean;
        }

        return null;
    }

    public String getAsString(FacesContext context, UIComponent component,
Object value)
            throws ConverterException {
        if (null != value) {
            if (value instanceof KeyValuePair) {
                return (String)((KeyValuePair)value).getValue();
            }else if (value instanceof String) {
                return value.toString();
            }
        }
        return null;
    }

public class KeyValuePair implements Serializable {
    private String key = null;
    private Object value = null;
    //getter and setter methods...
}

The problem is that when the user starts typing nothing is suggested even
though the control goes to suggest method and a list is created properly.
getLabel method gets called and even the method in the converter gets
called. But somehow it does not show up.

Browser that i am using is IE 6

Any help in this regard will be greatly appreciated.

Regards,
Madhav

-- 
When I tell the truth, it is not for the sake of convincing those who do not
know it, but for the sake of defending those that do

Re: I just cannot make InputSuggestAjax component work

Posted by Gerald Müllan <bi...@gmail.com>.
Hi,

can you have a look at the response code after typing in? There must
be something like [[asd][asd],[bcd][bcd]]...

Or is it empty?

If you have firebug installed just turn it on and you can see easily
the http traffic.

Any js-errors on the page?

cheers,

Gerald

On 6/9/07, Madhav Bhargava <un...@gmail.com> wrote:
> Hi All,
>
> Environment:
> Weblogic 8.1
> JDK 1.4.2.x
> Myfaces 1.1.5
> Tomahawk 1.1.5
> Tomahawk sandbox 1.1.5
>
> I have taken the lead from the example at http://www.irian.at which is again
> present at http://wiki.apache.org/myfaces/InputSuggestAjax
>
> Instead of an Address bean i have a KeyValuePair bean. This bean has 2
> properties - key (String), value (Object). I want to display the values in
> the drop down that would come up when the user starts typing something and
> then the selected bean will then be passed to the model. Using the key field
> of the selected bean i will then compose a query for the database.
>
> JSP code:
> <s:inputSuggestAjax suggestedItemsMethod="#{ajaxsuggest.suggest}"
>     itemLabelMethod="#{ajaxsuggest.getLabel}"
> value="#{ajaxsuggest.selectedBean }">
>     <f:converter converterId="inputSuggestAjaxConverter"/>
> </s:inputSuggestAjax>
>
> Methods in Controller/Managed bean for inputSuggestAjax component:
>
>     public List suggest(String prefix) {
>         List matchingList = new ArrayList();
>         Iterator iter = items.iterator();
>         while(iter.hasNext()) {
>             KeyValuePair bean = (KeyValuePair)iter.next();
>             if
> (bean.getValue().toString().startsWith(prefix)) {
>                 matchingList.add(bean);
>             }
>         }
>         return matchingList;
>     }
>
>     public String getLabel(Object selectedBean) {
>         if (null != selectedBean && selectedBean instanceof KeyValuePair) {
>             Iterator iter = items.iterator();
>             while(iter.hasNext()) {
>                 KeyValuePair bean = (KeyValuePair)iter.next();
>                 if
> (bean.getKey().equals(((KeyValuePair)selectedBean).getKey()))
> {
>                     return (String)bean.getValue();
>                 }
>             }
>         }
>         return null;
>     }
>
> Just to test i am populating the items List in the controller itself:
>     public InputSuggestAjaxController() {
>         items = new ArrayList();
>         items.add(new KeyValuePair("1", "India"));
>         items.add(new KeyValuePair("2", "US"));
>         items.add(new KeyValuePair("3", "Russia"));
>         items.add(new KeyValuePair("4", "China"));
>          items.add(new KeyValuePair("5", "Indonasia"));
>         items.add(new KeyValuePair("6", "Iran"));
>         items.add(new KeyValuePair("7", "Uganda"));
>     }
>
> Custom Converter for this component:
>     public Object getAsObject(FacesContext context, UIComponent component,
> String value)
>             throws ConverterException {
>
>         if (null != value) {
>             KeyValuePair bean = new KeyValuePair();
>             bean.setKey(value);
>             return bean;
>         }
>
>         return null;
>     }
>
>     public String getAsString(FacesContext context, UIComponent component,
> Object value)
>             throws ConverterException {
>         if (null != value) {
>             if (value instanceof KeyValuePair) {
>                  return
> (String)((KeyValuePair)value).getValue();
>             }else if (value instanceof String) {
>                 return value.toString();
>             }
>         }
>         return null;
>     }
>
> public class KeyValuePair implements Serializable {
>     private String key = null;
>     private Object value = null;
>     //getter and setter methods...
> }
>
> The problem is that when the user starts typing nothing is suggested even
> though the control goes to suggest method and a list is created properly.
> getLabel method gets called and even the method in the converter gets
> called. But somehow it does not show up.
>
> Browser that i am using is IE 6
>
> Any help in this regard will be greatly appreciated.
>
> Regards,
> Madhav
>
> --
> When I tell the truth, it is not for the sake of convincing those who do not
> know it, but for the sake of defending those that do


-- 
http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces