在JSF中动态添加组件? [英] Dynamically add the component in JSF?
问题描述
在我的应用程序中,组件必须动态更改。我有一个数据表,因为我有两列,首先是菜单中的< h:selectoneMenu>
我有两个数据(数据是1和2) )如果选择1,则应显示< h:inputText>
,如果选择2,则< h:selectoneMenu>
应该出现。需要帮助才能做到这一点?
In my application the component has to change dynamically. I am having a Datatable in that i am having two column, first is a <h:selectoneMenu>
in the menu i am having two data(the data are 1 and 2) if 1 is selected then a <h:inputText>
should appear and if 2 is selected <h:selectoneMenu>
should appear. Need help to do this?
我的JSF
<h:selectOneMenu id="menu" value="#{sample.data}" rendered="true" valueChangeListener="#{sample.change}">
<f:selectItem itemLabel="Data" itemValue=""/>
<f:selectItems value="#{sample.list1}"/>
<a4j:support event="onchange" reRender="text" />
</h:selectOneMenu>
<h:inputText id="text" value="#{sample.input}" rendered="#{sample.status}" />
My Manged Bean Class
public class Sample {
private Boolean status; //Getter & Setter
private List<SelectItem> list1; //Setter
private String input; //Getter & Setter
private String data; //Getter & Setter
public void change(ValueChangeEvent event){
System.out.println((String)event.getNewValue());
if(((String)event.getNewValue()).equals("value1")){
status=true;
}
else if(((String)event.getNewValue()).equals("value2")){
status=false;
}
}
public Boolean getStatus(){
if(status==null){
status=true;
}
return status;
}
public List<SelectItem> getList1() {
if(list1==null) {
list1 = new ArrayList<SelectItem>();
list1.add(new SelectItem("value1", "label1"));
list1.add(new SelectItem("value2", "label2"));
}
return list1;
}
}
推荐答案
我的建议是:不要动态添加/删除组件。以另一种方式解决您的问题:
My advice would be: do not add/remove component dynamically. Solve your problem another way:
- 切换可见性组件
- 重新绑定属于某个组件的数据
- Toggle visibility of components
- Rebind the data belonging to a component
动态添加/删除组件始终是一个麻烦和机会的来源你可以用另一种方式做得更简单。
Adding/removing component dynamically is always a source of trouble and chances are that you can do it another way much simpler.
在你的情况下,使用渲染
属性来玩可见性应该够了。
In your case, playing with the visibility using rendered
attribute should be enough.
这篇关于在JSF中动态添加组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!