3 Replies Latest reply on Sep 20, 2008 3:07 PM by Marcelo Leiras

    HtmlSuggestionBox doubt

    Marcelo Leiras Newbie

      Hi all,

      i'm trying to use the HtmlSuggestionBox programatically, but it is not working wright. The list appears correctly, the filtering is wright too, but when i select some item on the list, the selected item don't appears on the HtmlInputText.
      Please take a look at my code:

      <h:form id="formX">
      <rich:panel id="panelX" binding="#{telaLogin.panelX}"/>
      </h:form>
      


       public List autoCompleteFornecedor(Object event) {
       String txt = event.toString();
       List<Entity> list = new ArrayList<Entity>();
      
       Iterator<Entity> it = this.fornecedores.iterator();
       while (it.hasNext()) {
       Fornecedor f = (Fornecedor)it.next();
       if(f.getNome().toUpperCase().contains(txt.toUpperCase())) {
       list.add(f);
       }
       }
       return list;
       }
      


       private void createPnTeste() {
       this.panelX = new HtmlPanel();
      
       HtmlInputText fornecedor = new HtmlInputText();
       fornecedor.setId("itFornecedor");
      
       HtmlSuggestionBox sbFornecedor = new HtmlSuggestionBox();
       sbFornecedor.setId(fornecedor.getId()+"sb");
       sbFornecedor.setVar("grupo");
       sbFornecedor.setWidth("200");
       sbFornecedor.setHeight("200");
       sbFornecedor.setFor(fornecedor.getId());
      
       FacesContext context = FacesContext.getCurrentInstance();
       ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
       ELContext elCtx = context.getELContext();
      
       MethodExpression me = expressionFactory.createMethodExpression(elCtx, "#{telaLogin.autoCompleteFornecedor}", List.class, new Class[]{Object.class});
       sbFornecedor.setSuggestionAction(me);
      
       HtmlColumn column = new HtmlColumn();
       column.setId(sbFornecedor.getId()+"column");
      
       HtmlOutputText ot = new HtmlOutputText();
       ot.setId(column.getId()+"ot");
       ValueExpression ve = expressionFactory.createValueExpression(elCtx, "#{"+sbFornecedor.getVar()+".nome}", String.class);
       ot.setValueExpression("value", ve);
      
       column.getChildren().add(ot);
      
       sbFornecedor.getChildren().add(column);
      
       //pn.setStyle(pn.getStyle()+" height: 300px;");
       this.panelX.getChildren().add(fornecedor);
       this.panelX.getChildren().add(sbFornecedor);
       }
      


      What am i doing wrong? Thanks for help.

        • 1. Re: HtmlSuggestionBox doubt
          Marcelo Leiras Newbie

          I made the same thing on static way, and works perfectly.
          Why it is not working programatically? What am i missing?

          My static code that works fine:

          <h:form id="formY">
          <rich:panel id="panelY">
          <h:inputText id="itY" />
          <rich:suggestionbox id="sbY" for="itY" var="item" suggestionAction="#{telaLogin.autoCompleteFornecedor}" width="200" height="200">
          <h:column>
          <h:outputText value="#{item.nome}" />
          </h:column>
          </rich:suggestionbox>
          </rich:panel>
          </h:form>
          



          • 2. Re: HtmlSuggestionBox doubt
            Nick Belaevski Master

            Hi,

            I've created an issue about that: https://jira.jboss.org/jira/browse/RF-4506

            After I've added the code:

            ValueExpression ve = expressionFactory.createValueExpression(elCtx, "#{"+sbFornecedor.getVar()+"}", String.class);
             sbFornecedor.setValueExpression("fetchValue", ve);
            
            the issue has disappeared.

            • 3. Re: HtmlSuggestionBox doubt
              Marcelo Leiras Newbie

              Thanks a lot nbelaevski! It is working now after i added your code. Here is the entire code for future reference:

               private void createPnTeste() {
               this.panelX = new HtmlPanel();
              
               HtmlInputText fornecedor = new HtmlInputText();
               fornecedor.setId("itFornecedor");
               this.panelX.getChildren().add(fornecedor);
              
               HtmlSuggestionBox sbFornecedor = new HtmlSuggestionBox();
               sbFornecedor.setId(fornecedor.getId()+"sb");
               sbFornecedor.setVar("frn");
               sbFornecedor.setWidth("200");
               sbFornecedor.setHeight("200");
               sbFornecedor.setFor(fornecedor.getId());
              
               FacesContext context = FacesContext.getCurrentInstance();
               ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
               ELContext elCtx = context.getELContext();
              
               MethodExpression me = expressionFactory.createMethodExpression(elCtx, "#{telaLogin.autoCompleteFornecedor}", List.class, new Class[]{Object.class});
               sbFornecedor.setSuggestionAction(me);
              
               HtmlColumn column = new HtmlColumn();
               column.setId(sbFornecedor.getId()+"column");
              
               HtmlOutputText ot = new HtmlOutputText();
               ot.setId(column.getId()+"ot");
              
               ValueExpression ve = expressionFactory.createValueExpression(elCtx, "#{"+sbFornecedor.getVar()+".nome}", String.class);
               ot.setValueExpression("value", ve);
              
               ValueExpression ve2 = expressionFactory.createValueExpression(elCtx, "#{"+sbFornecedor.getVar()+".nome}", String.class);
               sbFornecedor.setValueExpression("fetchValue", ve2);
              
               column.getChildren().add(ot);
              
               sbFornecedor.getChildren().add(column);
               this.panelX.getChildren().add(sbFornecedor);
               }
              


              One more question. Is there a way to do it with a HtmlSelectOneMenu instead HtmlInputText?