我有一个remmoteCommand更新List,在项加载后重复渲染selectBooleanCheckbox。但是,未在复选框中正确设置值,所有这些值都未选中,旁边有一些具有selected属性(布尔值)的项设置为true。
如果我只是在加载列表后在支持bean中设置一个Item,它只适用于该Item。可能是什么问题?
的index.xhtml
<h:form>
<h:panelGroup id="itemsHolder">
<h:panelGrid rendered="#{backingBean.items != null}">
<h:selectBooleanCheckbox value="#{backingBean.item.selected}" disabled="#{backingBean.rendered}" /> #{backingBean.item.name}
<ui:repeat value="#{backingBean.items.toArray()}" var="option" >
<h:selectBooleanCheckbox value="#{option.selected}" disabled="#{backingBean.rendered}" />
#{option.name}
<br />
</ui:repeat>
</h:panelGrid>
</h:panelGroup>
<p:remoteCommand rendered="#{backingBean.rendered}"
name="initItems"
actionListener="#{backingBean.initItems()}"
process="@this"
update="itemsHolder"/>
</h:form>
<ui:fragment rendered="#{backingBean.rendered}">
<script>
$(document).ready(function () {
setTimeout(function () {
initItems();
}, 3000);
});
</script>
</ui:fragment>
BackingBean.java
@Component
@ManagedBean
@Scope("view")
public class BackingBean {
private boolean rendered = true;
private Set<Item> items;
private Item item;
public void initItems() {
items = new LinkedHashSet<>();
items.add(new Item("item 1", true, "1"));
items.add(new Item("item 2", false, "2"));
items.add(new Item("item 3", true, "3"));
item = new Item("lonely item", true, "4");
}
//Getters and setters
}
更新 我在SandBox中创建了一个真实代码的副本,我仍然无法重现,我想我将不得不从实际代码中添加更多依赖项。
答案 0 :(得分:0)
我已经重新创建了你的案例,它可以正常使用以下简单的托管bean
@ManagedBean(name = "bean")
@ViewScoped
public class YourBean implements Serializable {
List<YourItem> items=new ArrayList<>();
YourItem anItem=new YourItem();
@PostConstruct
public void init() {
System.out.println("Bean is created");
//initialize items
anItem.setSelected(false);
//add 5 items to list
for (int i=0;i<5;i++){
YourItem item=new YourItem();
item.setSelected(false);
items.add(item);
}
}
public void onUpdateItems(){
System.out.println("Items will be updated...");
anItem.setSelected(true);
//modify 1st and 3rd item
items.get(0).setSelected(true);
items.get(2).setSelected(true);
//remove 4th item
items.remove(3);
}
public List<YourItem> getItems() {
return items;
}
public void setItems(List<YourItem> items) {
this.items = items;
}
public YourItem getAnItem() {
return anItem;
}
public void setAnItem(YourItem anItem) {
this.anItem = anItem;
}
}
p:command
应按以下方式实施
<h:form id="form">
<h:panelGroup id="itemHolder">
<h:panelGrid rendered="#{bean.items != null}">
<h:selectBooleanCheckbox value="#{bean.anItem.selected}" />
<ui:repeat value="#{bean.items}" var="item">
<h:selectBooleanCheckbox value="#{item.selected}" />
#{item.selected}
<br />
</ui:repeat>
</h:panelGrid>
</h:panelGroup>
<p:remoteCommand name="startUpdate"
actionListener="#{bean.onUpdateItems()}"
update=":form:itemHolder"/>
<p:commandButton value="Update items" onclick="startUpdate();"/>
</h:form>
将您的代码与此示例进行比较。您的托管bean方法很可能存在更新items
列表或更新h:panelGroup
的方式的问题。