1

I have two combobox(category and subcategory), i want to render subcategory combobox that is depending on the selection id of category combobox. But, subcategory combobox doesn't render? Here is my code,

<h:panelGrid>
    <h:outputText value="Cateogyr : "/>
    <rich:comboBox defaultLabel="Enter some value" >
      <a4j:support event="onchange" reRender="subCombo" ajaxSingle="true"/>
      <f:selectItems value="#{bookManager.categoryList}" />
    </rich:comboBox>
    </h:panelGrid>

    <h:panelGrid>
    <h:outputText value="Sub Category : "/>
    <rich:comboBox defaultLabel="Enter some value" id="subCombo">
       <f:selectItems value="#{bookManager.subCategoryList}" />
    </rich:comboBox>  
    </h:panelGrid>

I printed out in backing bean where the subcategory combobox is rendered, but it does not appear. Please, if u have any idea, let me know.

Myo Thu Zar Kyaw
  • 137
  • 1
  • 3
  • 11

1 Answers1

1

It looks like you're working with JSF 1.2 and RichFaces. By the posted code, it looks like you need to do some fixes in your actual code:

  • You're missing the <h:form> that wraps the data to be sent to the server. Your code should be like this:

    <h:form>
        <h:panelGrid>
            <h:outputText value="Category : "/>
            <rich:comboBox defaultLabel="Enter some value" >
              <a4j:support event="onchange" reRender="subCombo" ajaxSingle="true"/>
              <f:selectItems value="#{bookManager.categoryList}" />
            </rich:comboBox>
        <!-- rest of JSF/HTML code... -->
    </h:form>
    
  • In order to work properly, your bookManager managed bean needs to have Session Scope or Request scope and the @KeepAlive annotation in order to work without problems. For performance hits, I would recommend using Request scopes. Your class should look like this

    @KeepAlive
    public class BookManager {
        //class code...
    }
    

Seeing your code, it looks like you're retrieving the bookManager.subCategoryList on the getter of your bean (from database or cached resource). Make sure your managed bean getters and setters are pretty straight forward (like public int getNumber() { return this.number; }) and do not contain any business logic code in it because JSF could call the getter methods more than once. For further info about this, see:

Now, how to solve this problem of retrieving the subcategory list when changing a value in the <rich:combobox>? Use the action component attribute of <a4j:support> invoking a method that retrieves and prepare the data in the bookManager.categoryList.

JSF Code

<a4j:support event="onchange" reRender="subCombo" ajaxSingle="true"
    action="#{bookManager.obtainSubcategoryList}" limitToList="true" />

Java Code

@KeepAlive
public class BookManager {
    public void obtainSubcategoryList() {
        //retrieve the data in this action.
        this.subCategoryList = ...;
    }
    //class code...
}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • By the way, seeing your question edit, I assume you forgot to post the bind to the value of the ``, in other words, the ``. – Luiggi Mendoza Oct 29 '12 at 06:15
  • Than u for your comments, but it doesn't rendered. :( – Myo Thu Zar Kyaw Oct 29 '12 at 07:10
  • @MyoThuZarKyaw what problem now you have? – Luiggi Mendoza Oct 29 '12 at 07:12
  • I've already do all of you said. But, obtainSubcategoryList() does not work after selecting the categoryList. It didn't rerender. – Myo Thu Zar Kyaw Oct 29 '12 at 07:19
  • Have you tested that the `obtainSubcategoryList` is being invoked? Is there any JSF message about this issue (you can write an `` section for this)? – Luiggi Mendoza Oct 29 '12 at 07:20
  • Yes, I have put messagees tag. The combo box doesn't still rendered. :( Nothing happen. – Myo Thu Zar Kyaw Oct 29 '12 at 07:33
  • @MyoThuZarKyaw did you check the `obtainSubcategoryList` is executed (maybe put a break point or a `System.out.println("entered obtainSubcategoryList method");` to make sure). Also, did you bind the value of the `` to a value of your managed bean? As a last resource, try replacing `` instead of `` (for both comboboxes in your page). – Luiggi Mendoza Oct 29 '12 at 07:37
  • Yey.. Now, it works. I replace with instead of . But I have still want to know a little. I gave list item as List array to combo box. Coz i want itemvalue and itemlabel. In it is ok. But in , it only show id. – Myo Thu Zar Kyaw Oct 29 '12 at 07:46
  • @MyoThuZarKyaw read here: [`` wiki](http://stackoverflow.com/tags/selectonemenu/info) – Luiggi Mendoza Oct 29 '12 at 13:52