代码之家  ›  专栏  ›  技术社区  ›  Martlark

richfaces suggestionbox向backing bean传递附加值

  •  5
  • Martlark  · 技术社区  · 16 年前

    使用RichFaces时 suggestionBox 如何将多个ID或值从包含文本输入的页传递到 建议箱 背豆。IE:显示选定州内建议的城市列表?这是我的 autoComplete 方法。

    public List< Suburb > autocomplete(Object suggest)
    {
        String pref = (String) suggest;
        ArrayList< Suburb > result = new ArrayList< Suburb >();
    
        Iterator< Suburb > iterator = getSuburbs().iterator();
        while( iterator.hasNext() )
        {
            Suburb elem = ((Suburb) iterator.next());
            if( (elem.getName() != null && elem.getName().toLowerCase().indexOf( pref.toLowerCase() ) == 0) || "".equals( pref ) )
            {
                result.add( elem );
            }
        }
        return result;
    }
    

    如您所见,有一个值是从页面传递的, Object 建议,这是 h:inputText (在脸上 m:textFormRow )

    <m:textFormRow id="suburb" label="#{msgs.suburbPrompt}" 
        property="#{bean[dto].addressDTO.suburb}"
        required="true" maxlength="100" size="30" />
    
    <rich:suggestionbox height="200" width="200" usingSuggestObjects="true"
        suggestionAction="#{suburbsMBean.autocomplete}" var="suburb" for="suburb"
        fetchValue="#{suburb.name}" id="suggestion">
        <h:column>
            <h:outputText value="#{suburb.name}" />
        </h:column>
    </rich:suggestionbox>
    

    在页面的前面,您可以选择一个状态,我想用它来缩减建议框显示的郊区列表。

    4 回复  |  直到 9 年前
        1
  •  3
  •   Jonik    15 年前

    (免责声明:我知道这个问题是很久以前提的,但也许这会帮助有类似问题的人……)

    看看这篇关于类似问题的博文: RichFaces - SuggestionBox and hidden field

    关键是使用 <f:setPropertyActionListener value="#{...}" target="#{...}"> 包在里面 <a4j:support event="onselect" ajaxSingle="true"> . 当 onselect 为提示框触发。

    通过这种方法,我创建了一个SuggestionBox,它显示(和自动完成)客户的 姓名 但一经选择 客户对象 (具有几个属性;由id标识)用于bean。

        2
  •  1
  •   sth    16 年前

    是否使用 <f:parameter 标签在 <rich:suggestionbox 工作?

        3
  •  0
  •   Mark    16 年前

    你看过RichFaces建议箱吗 demo 但是呢?示例下有链接可查看源代码。

    编辑:

    听起来,在用户在suggestionbox中输入之前,您需要bean中的state值。我将使用richfaces ajax支持将state的值传递给bean,这样当调用autocomplete方法时,用户就可以在页面上选择state来填充郊区列表。

        4
  •  0
  •   PMorganCA    9 年前

    你可以使用 <f:parameter 标签在 rich:suggestionbox . 我的任务是根据list元素的某个属性筛选一个列表,有时可以忽略该属性。比如,有时候我只想要一份柑橘类水果的清单,有时候我想要一份完整的可用水果清单。

    在页面中:

    <rich:suggestionbox usingSuggestObjects="true"
            suggestionAction="#{listBuilder.autocompleteFilterFruit('')}" var="ind"
            for="fruitInput" fetchValue="#{fruit.name}" id="suggestion" >
        <f:param name="constrainInd" value="#{basket.isConstrainedToCitrus}" />
    
        ...
    
    </rich:suggestionbox>
    

    我只有一节课( Basket )知道列表是否需要特殊筛选,以及另一个类( ListBuilder )这就构成了名单。

    :

    public Boolean getIsConstrainedToCitrus ()
    {
        return new Boolean ( logic that answers "is this basket for citrus only" );
    }
    

    在ListBuilder中:

    public List<Fruit> autocompleteFilterFruit (Object arg)
    {
        List<Fruit> rtnList = new ArrayList<Fruit> ();
    
        String suggestion = (String) arg;
    
        // get the filter control that the page retrieved from the Basket
        //
        Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext ().getRequestParameterMap();
        boolean isConstrainedToCitrus = "true".equals (params.get ("constrainInd"));
    
        // allFruit is a pre-initialized list of all the available fruit. use it to populate the return list according 
        // to the filter rules and matches to the auto-complete suggestions
        for (Fruit item : allFruit)
        {
            if ((!isConstrainedToCitrus || item.isCitrus())  &&  item.name.startsWith(suggestion))
            {
                rtnList.add (item);
            }
        }
        return rtnList;
    }