값이 다른 값으로 변경되면 SelectOneMenu에서 목록을 변경하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1346088

  •  20-09-2019
  •  | 
  •  

문제

나는 그것을 바꾸고 싶다 SelectItem[] 두 번째 배열 SelectOneMenu, 값이 첫 번째 값에서 변경된 경우. 그게 가능합니까?

도움이 되었습니까?

해결책

나는 이것을 알아 냈지만 JSF뿐만 아니라 Richfaces의 Ajax 기능을 사용했습니다. 내 첫 SelectOneMenu에 태그를 추가하면 작동합니다. :)

<a4j:support event="onchange" action="#{bean.onChange}"
             reRender="otherSelectOneMenuID"/>

어쨌든 응답에 감사드립니다!

다른 팁

값 변경 리스너를 첫 번째 SelectOneMenu에 바인딩하면 가능해야합니다.

ValueChangeevent에서 새 값을 얻고 그에 따라 목록을 업데이트하십시오. JSF 페이지는 업데이트 된 목록을 표시해야합니다.

그것이 말이되기를 바랍니다!

글쎄, 나는 A4J를 사용했고 그것은 효과가 있었다.

<code>
//JSF
<h:outputLabel value="First selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.selectedItem}">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df01" itemLabel="Item01" itemValue="1" />
<f:selectItem id="df02" itemLabel="Item02" itemValue="2" />
<f:selectItem id="df03" itemLabel="Item03" itemValue="3" />
<a4j:support event="onchange" reRender="secondSelectOneMenu"/> //secondSelectOneMenu is the id of the dropdown you want to change
</h:selectOneMenu>


<h:outputLabel value="Second selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.attributeToStoreSelectedValue}" id="secondSelectOneMenu">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df00" itemLabel="Select" itemValue="0" /> //Default value
<f:selectItems value="#{yourBackingBean.returnByChoice}" />
</h:selectOneMenu>


//Converter

public class DefaultConverter implements Converter {
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
    return value;
}

public String getAsString(FacesContext ctx, UIComponent component, Object value) {
    String label = "";
    if (value != null) {
        label = value.toString();
    }
    return label;
}
}

//Backing Bean Sample
public List<SelectItem> returnByChoice() { //it must return a list of SelectItems so it can be displayed on the jsf page
   String id = (String) getSelectedItem(); //this is the value chosen from the first dropDownMenu wich selectedItem is the attribute onthe binding of the first dropDownMenu.
   ArrayList<SelectItem> arrItems = new ArrayList<SelectItem>();
   if (id != null) {

            List<YourClass> yourObjectList = yourDao.findAllItemsFromType(new Integer(id));

         Iterator<YourClass> iterator = yourObjectList.iterator();
         String tempName = "";
         String tempId = "";
         YourClass tempYourObject = null;

        while (iterator.hasNext()) {
           tempYourObject = iterator.next();
           tempId = String.valueOf(tempYourObject.getId());
           tempName = tempYourObject.getName();
           arrItems.add(new SelectItem(tempId, tempName));
        }
    }
    return arrProfiles;
}
</code>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top