2 Replies Latest reply on Oct 8, 2010 2:53 AM by mathandos

    Convert integer field to correspondig drop down field

    mathandos
      Hi,

      Hoping there will be someone to assist me.

      I have a dropdown list, when saving, it stores the relevant foreign key. But when I try to edit the data instead of pulling the corresponding field, it pulls the first field in the drop down list.

      scenario:
      dropdown list A [1,2,3] respectively
      item1
      item2
      item3

      if item2 is selected it will store 2 in the db. when trying to edit data it selects item1 by default and not item2. I would like it to display item2.

      Any help will be much appreciated

      Thanking you in advance
        • 1. Re: Convert integer field to correspondig drop down field
          kragoth

          provide your JSF or whatever code for your drop down menu please.

          • 2. Re: Convert integer field to correspondig drop down field
            mathandos
            Hi Tim

            I hope i've provide enough code for your assistance, if not please do not hesitate to let me know if you need more info.

            (List.java)
            //to populate the dropdown with municipality description

            public class MunicipalityList extends EntityQuery<Municipality> {

                 private static final String EJBQL = "select municipality from Municipality municipality";

                 private static final String[] RESTRICTIONS = {"lower(municipality.municipalityDescription) like lower(concat(#{municipalityList.municipality.municipalityDescription},'%'))",};

                 private Municipality municipality = new Municipality();
                 private SelectItem[] items = null;

                 public MunicipalityList() {
                      setEjbql(EJBQL);
                      setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
                      setMaxResults(25);
                 }

                 public Municipality getMunicipality() {
                      return municipality;
                 }

                 public SelectItem[] getItems() {
                      if (items == null) {
                           items = new SelectItem[this.getResultList().size()];
                           for (int i = 0; i < this.getResultList().size(); i++) {
                                Municipality entity = this.getResultList().get(i);
                                            SelectItem item = new SelectItem(entity, entity.getMunicipalityDescription());
                                items[i] = item;
                           }
                      }
                      return items;
                 }
            }

            (.xhtml)

            <s:decorate id="municipalityField" template="layout/edit.xhtml">
                             <ui:define name="label">Municipality</ui:define>
                                <h:selectOneMenu value="#{regionHome.instance.municipality}">
                                     <f:selectItems value="#{municipalityList.items}" var="municipalityDescription"
                                          noSelectionLabel="Please Select..."/>
                                     <s:convertEntity />
                                     <a:support event="onblur" reRender="municipalityField" bypassUpdates="true" ajaxSingle="true"/>
                                </h:selectOneMenu>                      
                        </s:decorate>