因此,您不仅对新值感兴趣,而且对实际值更改感兴趣。引入一个
valueChangeListener将旧值与新值进行比较的a
,并准备一些ajax侦听器方法可以拦截的属性。
例如
<h:selectManyCheckbox value="#{bean.selectedItems}" valueChangeListener="#{bean.selectedItemsChanged}" converter="javax.faces.Long"> <f:selectItems value="#{bean.availableItems}" /> <f:ajax listener="#{bean.itemSelected}" /></h:selectManyCheckbox>
与
private Map<String, Long> availableItems; // +getterprivate List<Long> selectedItems; // +getter+setterprivate Long selectedItem;private boolean selectedItemRemoved;@PostConstructpublic void init() { availableItems = new linkedHashMap<String, Long>(); for (long i = 0; i < 10; i++) { availableItems.put("CHKID " + i, i); }}public void selectedItemsChanged(ValueChangeEvent event) { List<Long> oldValue = (List<Long>) event.getOldValue(); List<Long> newValue = (List<Long>) event.getNewValue(); if (oldValue == null) { oldValue = Collections.emptyList(); } if (oldValue.size() > newValue.size()) { oldValue = new ArrayList<Long>(oldValue); oldValue.removeAll(newValue); selectedItem = oldValue.iterator().next(); selectedItemRemoved = true; } else { newValue = new ArrayList<Long>(newValue); newValue.removeAll(oldValue); selectedItem = newValue.iterator().next(); selectedItemRemoved = false; }}public void itemSelected(AjaxBehaviorEvent event) { System.out.println("Selected item: " + selectedItem); System.out.println("Selected item removed? " + selectedItemRemoved);}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)