6 Replies Latest reply on Oct 6, 2009 11:38 AM by Val Sw

    Issue with suggestionbox in ui repeat -urgent pls help

    Val Sw Expert

      I posted it in seam forum but there is no response, may be it is related to richfaces...

      I am trying to use suggestionbox in ui repeat but suggestionAction is never called.

      Please suggest, why suggestionAction="#{itemLinesBean.autocomplete}" is not called in ui repeat, I tested various scenarios but it doesn't work in ui repeat...

      Any suggestion will be of great help... I am stuck on this and can't proceed..

      XHTML

      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
       xmlns:s="http://jboss.com/products/seam/taglib"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:a="http://richfaces.org/a4j"
       xmlns:rich="http://richfaces.org/rich"
       xmlns:c="http://java.sun.com/jstl/core"
       template="layout/template.xhtml">
      
      <ui:define name="body">
      
      <h:messages globalOnly="true" styleClass="message" errorClass="errors" infoClass="info" id="globalMessages"/>
      
      <h:form id="proj" styleClass="edit">
      //THIS WORKS FINE
      <h:inputText value="#{itemLinesBean.cell}" id="text1" />
       <rich:suggestionbox id="suggestionBoxId1" for="text1" tokens=",[]"
       suggestionAction="#{itemLinesBean.autocomplete}" var="result"
       fetchValue="#{result.itemName}"
       nothingLabel="No items found" columnClasses="center"
       usingSuggestObjects="true">
       <h:column>
       <h:outputText value="#{result.itemName}" />
       </h:column>
       </rich:suggestionbox>
      <rich:panel id="reagentPanel">
      <table><thead>...</thead>
       <ui:repeat value="#{projHome.reg}" var="info" >
       <tbody>
       <td>
       <s:decorate id="type" template="layout/edit.xhtml">
       <h:inputText id="type" required="true" value="#{info.type}"/>
       </s:decorate>
       </td>
       <td>
       //NOT WORKING, suggestionAction EVENT IS NOT FIRED <h:inputText value="#{itemLinesBean.cell}" id="text" />
       <rich:suggestionbox id="suggestionBoxId" for="text" tokens=",[]"
       suggestionAction="#{itemLinesBean.autocomplete}" var="result"
       fetchValue="#{result.itemName}"
       nothingLabel="No cells found" columnClasses="center"
       usingSuggestObjects="true">
       <h:column>
       <h:outputText value="#{result.itemName}"/>
       </h:column>
       </rich:suggestionbox>
       </td>
       ....
       </tbody>
       </ui:repeat>
      </table>
      </rich:panel>
      
      <div class="actionButtons" align="center">
       <h:commandLink id="add" action="#{projHome.addreg}"
       value="Add More regs"/>
      </div>
      <div class="actionButtons">
       <h:commandLink id="save" action="#{projHome.persist}"..>Save</h:commandLink>
       ...
      </div>
      </h:form>
      </ui:define>
      </ui:composition>
      
      Corresponding bean
      @Name("itemsBean")
      public class ItemsBean {
       public List<Item> autocomplete(Object suggest) {
       System.out.println("ItemsBean.autocomplete() Entered.... CHECK.......");
       String pref = (String)suggest;
       ArrayList<Item> result = new ArrayList<Item>();
       Iterator<Item> iterator = getItems().iterator();
       while (iterator.hasNext()) {
       Item elem = ((Item) iterator.next());
       if ((elem.getItemName() != null && elem.getItemName().toLowerCase().indexOf(pref.toLowerCase()) == 0) || "".equals(pref))
       {
       result.add(elem);
       }
       }
       return result;
       }
      }


        • 1. Re: Issue with suggestionbox in ui repeat -urgent pls help
          Ilya Shaikovsky Master

          1) try a4hj:repeat

          2) if not helps please specify your RF version. (and try under the latest one at least just for check if usign older currently)

          • 2. Re: Issue with suggestionbox in ui repeat -urgent pls help
            Val Sw Expert

            Thanks for the reply, ui repeat to a4j:repeat change won't be good for us as our screen is very complex and will result in lot of issues.

            Environment is : jboss-4.2.3, jboss-seam-2.0.2.SP1 and richfaces 3.3.1.GA

            However I just tried adding ajaxSingle="false", now suggestionAction is firing the event but we are sure why adding ajaxSingle="false" fires the event.??

            We are at initial phase of testing and not sure if any issue might come up.

            <h:inputText value="#{itemLinesBean.cell}" id="text" />
             <rich:suggestionbox id="suggestionBoxId" for="text" tokens=",[]"
             suggestionAction="#{itemLinesBean.autocomplete}" var="result"
             fetchValue="#{result.itemName}"
             nothingLabel="No cells found" columnClasses="center"
             usingSuggestObjects="true" ajaxSingle="false">
             <h:column>
             <h:outputText value="#{result.itemName}"/>
             </h:column>
            </rich:suggestionbox>


            • 3. Re: Issue with suggestionbox in ui repeat -urgent pls help
              José Ventura Newbie

              It could be that other components on your page are complaining about validation errors, and you are not seeing them. This is a common issue...

              For example, suppose you have a required field in the same form as the suggestion box. When you start typing, the Ajax engine sends the whole form to the server. Since there is a required field that was left empty, errors are generated and the JSF phase processing ends, before the listener has a chance to execute.

              When you put ajaxSingle="true" in your tag, it tells the Ajax engine to only process the component firing the event. This way, that required field is never looked at, no validation errors occur, and the listener is called.

              With ajaxSingle="false", you submit the whole form (and must be aware of validations). With ajaxSingle="true", you process only one component. If you need something in between, use the "process" attribute: it lets you specify which components are processed. For example, you may want to process a button and a textfield, but not all the other fields. (I could be wrong here, if so, someone please correct me).

              In my project, we have several tags with ajaxSingle="true". Now, whenever we run into this sort of problem ("my action method/listener is not being called"), we check to see if it's a validation problem. You can also add a panel with validation messages, and make sure to reRender it on every ajax request; this way, you'll know when a validation problem is the culprit.

              Please correct me if I've said anything wrong :)

              • 4. Re: Issue with suggestionbox in ui repeat -urgent pls help
                Val Sw Expert

                Thanks for the inputs, I think you are right, our xhtml page is quiet complex with lot of validations... I will try to figure out this..

                It will be very nice of you to suggest on the following, I noticed the following, while implementing suggestionBox sample from richfaces.

                a) If I use ajaxSingle="true", "suggestionAction" event is not fired and nothing happens if I enter anything in "rich:suggestionbox"
                If I use ajaxSingle="false", event is fired and fetches the correct value from bean class and looks OK, do you foresee any issues..

                Pls help on this...
                b) As we are using ui repeat, some weird thing is happening

                Example : consider there are 2 rows and each row have suggestion box.

                1) User enters "A" and selects "Atlanta" from suggestion List : OK
                So, 1st. suggestionbox have : Atlanta

                2) Click "Add Regs" link, new row is added with new suggestion box and other fields.
                Enter "S" and select "Sacramento" from list then enter "T" and selects Trenton : OK
                So 2nd. suggestionbox have : Sacramento,Trenton

                3) Change the 1st. row suggestionBox value, using token (,) and enter "N" to select "Nashville" it is fine : OK
                So, 1st. suggestionbox have : Atlanta,Nashville

                4) Now ISSUE, try to change the 2nd. suggestionBox, by entering letter "C" it suggests the old list starting with "T" (Trenton, Topeka, etc.) from point 2 above.
                BUT, in the bean class it is prints correct list starting with "C".

                In CapitalsBean.autocomplete() capital Names are : Carson City
                In CapitalsBean.autocomplete() capital Names are : Concord
                In CapitalsBean.autocomplete() capital Names are : Columbus
                In CapitalsBean.autocomplete() capital Names are : Columbia
                In CapitalsBean.autocomplete() capital Names are : Charleston
                In CapitalsBean.autocomplete() capital Names are : Cheyenne


                I think issue is with clearing of "#{result.name}", I tried with no success... XHTML and Bean code is below. Bean is same as in rich faces sample.

                XHTML:

                <h:form id="proj" styleClass="edit">
                 <rich:panel id="regPanel">
                 <f:facet name="header">#{regHome.managed ? 'Edit' : 'Add/ Edit'} Reg</f:facet>
                 <table border="1" id="color" class="dr-table rich-table" width="100%">
                 <thead>
                 <tr class="dr-table-header rich-table-header">
                 <th>PName</th>
                 <th><h:outputText value="Action" /></th>
                 </tr>
                 </thead>
                
                 <ui:repeat value="#{projHome.Reg}" var="info" >
                 <tbody>
                 <td>
                 <s:decorate id="nameField" template="layout/edit.xhtml">
                 <h:inputText id="name" required="true" value="#{info.name}"/>
                 </s:decorate>
                 </td>
                 <td>
                 <h:inputText value="#{capitalsBean.capital}" id="text" />
                 <rich:suggestionbox id="suggestionBoxId" for="text" tokens=",[]"
                 rules="#{suggestionBox.rules}"
                 suggestionAction="#{capitalsBean.autocomplete}" var="result"
                 fetchValue="#{result.name}" rows="#{suggestionBox.rows}"
                 first="#{suggestionBox.intFirst}" minChars="#{suggestionBox.minchars}"
                 nothingLabel="No capitals found" columnClasses="center"
                 usingSuggestObjects="true"
                 ajaxSingle="false">
                 <h:column>
                 <h:outputText value="#{result.name}" />
                 </h:column>
                 </rich:suggestionbox> </td>
                 </tbody>
                 </ui:repeat>
                 </table>
                 </rich:panel>
                 <div class="actionButtons" align="center">
                 <h:commandLink id="add" action="#{projHome.addReg}" value="Add Regs"/>
                 </div>
                 </h:form>


                @Name("capitalsBean")
                public class CapitalsBean {
                private ArrayList<Capital> capitals = new ArrayList<Capital>();
                private ArrayList<String> capitalsNames = new ArrayList<String>();
                private List<SelectItem> capitalsOptions = new ArrayList<SelectItem>();
                private String capital = "";
                
                private String currentStateFilterValue;
                private String currentNameFilterValue;
                
                 public List<Capital> autocomplete(Object suggest) {
                 String pref = (String)suggest;
                 ArrayList<Capital> result = new ArrayList<Capital>();
                 Iterator<Capital> iterator = getCapitals().iterator();
                 while (iterator.hasNext()) {
                 Capital elem = ((Capital) iterator.next());
                 if ((elem.getName() != null && elem.getName().toLowerCase().indexOf(pref.toLowerCase()) == 0) || "".equals(pref))
                 {
                 System.out.println("In CapitalsBean.autocomplete() capital Names are : "+elem.getName());
                 result.add(elem);
                 }
                 }
                 return result;
                 }
                
                 public CapitalsBean() {
                 URL rulesUrl = getClass().getResource("capitals-rules.xml");
                
                 Digester digester = DigesterLoader.createDigester(rulesUrl);
                 digester.setUseContextClassLoader(true);
                
                 digester.push(this);
                 try {
                 digester.parse(getClass().getResourceAsStream("capitals.xml"));
                 } catch (IOException e) {
                 throw new FacesException(e);
                 } catch (SAXException e) {
                 throw new FacesException(e);
                 }
                 capitalsNames.clear();
                
                 for (Capital cap : capitals) {
                 capitalsNames.add(cap.getName());
                 }
                
                 capitalsOptions.clear();
                 for (Capital cap : capitals) {
                 capitalsOptions.add(new SelectItem(cap.getName(),cap.getState()));
                 }
                 }
                
                 public void resetFilter() {
                 setCurrentNameFilterValue("");
                 setCurrentStateFilterValue("");
                 }
                
                 public String addCapital(Capital capital) {
                 capitals.add(capital);
                 return null;
                 }
                
                 public ArrayList<Capital> getCapitals() {
                 return capitals;
                 }
                
                 public String getCapital() {
                 return capital;
                 }
                
                 public void setCapital(String capital) {
                 this.capital = capital;
                 }
                
                 public List<SelectItem> getCapitalsOptions() {
                 return capitalsOptions;
                 }
                
                 public ArrayList<String> getCapitalsNames() {
                 return capitalsNames;
                 }
                
                 public String getCurrentStateFilterValue() {
                 return currentStateFilterValue;
                 }
                
                 public void setCurrentStateFilterValue(String currentStateFilterValue) {
                 this.currentStateFilterValue = currentStateFilterValue;
                 }
                
                 public String getCurrentNameFilterValue() {
                 return currentNameFilterValue;
                 }
                
                 public void setCurrentNameFilterValue(String currentNameFilterValue) {
                 this.currentNameFilterValue = currentNameFilterValue;
                 }
                
                }


                • 5. Re: Issue with suggestionbox in ui repeat -urgent pls help
                  Val Sw Expert

                  I tried using a4j:repeat using rowKeyVar as below.
                  #{idx} prints correct index starting from 0,

                  project:#{idx}:text = project:0:text for 1st. row
                  project:#{idx}:text = project:1:text for 2nd. row
                  .... so on

                  But I get exception "Component for target project_0_text not found in SuggestionBox suggestionBoxId"

                  Please suggest how I can handle this...

                  <a4j:repeat value="#{projectHome.reagent}" var="info" rowKeyVar="idx">
                   <h:inputText value="#{capitalsBean.capital}" id="project_#{idx}_text" />
                   <rich:suggestionbox id="suggestionBoxId" for="project_#{idx}_text" tokens=",[]"
                   rules="#{suggestionBox.rules}"
                   suggestionAction="#{capitalsBean.autocomplete}" var="result"
                   fetchValue="#{result.name}"
                   first="#{suggestionBox.intFirst}"
                   minChars="#{suggestionBox.minchars}"
                   nothingLabel="No capitals found" columnClasses="center"
                   usingSuggestObjects="true"
                   ajaxSingle="false">
                   <h:column>
                   <h:outputText value="#{result.name}"/>
                   </h:column>
                   </rich:suggestionbox>
                  </a4j:repeat>


                  • 6. Re: Issue with suggestionbox in ui repeat -urgent pls help
                    Val Sw Expert

                    To update, it is working now after using a4j:repeat instead of ui:repaet