2012-12-27 78 views
8

假設這個頁面的代碼:如何獲得ajax方法中的點擊項目?

<h:form prependId="false" id="form"> 

    <h:selectManyCheckbox id="checkBoxList" value="#{backedBean.lstIdSelectedItems}" layout="pageDirection"> 
     <f:selectItems value="#{backedBean.lstAvailableItems}" var="item" itemLabel="#{item.label}" itemValue="#{item.value}" /> 
     <f:ajax listener="#{backedBean.itemClicked}" /> 
    </h:selectManyCheckbox> 

</h:form> 

和會話的代碼託管bean:

public class BackedBean implements Serializable { 
    private List<SelectItem> lstAvailableItems; 
    private List<Long> lstIdSelectedItems; 

public BackedBean() { 
    lstAvailableItems = new ArrayList<SelectItem>(); 
    lstIdSelectedItems = new ArrayList<Long>(); 
} 

@PostConstruct 
private void postConstruct(){ 
    for (int i = 0; i < 10; i++) { 
     SelectItem item = new SelectItem(new Long(i), "CHKID " + i); 
     lstAvailableItems.add(item); 
    } 
} 

public void itemClicked(AjaxBehaviorEvent ae){ 
    HtmlSelectManyCheckbox uiCmp = (HtmlSelectManyCheckbox)ae.getSource(); 

    // (1) Here I would like to get the ID of the item that has been clicked. 

} 

在(1)我想獲得已被點擊的元素的ID由用戶。我可以在lstIdSelectedItems數組列表中看到用戶選擇的所有元素的ID,但是如何獲取用戶點擊的元素的ID?

我試圖在selectManyCheckbox中使用f:attribute標記,但是當在後端bean中調用ajax監聽器方法時,該屬性不在組件映射中。我已經使用過了,但不起作用:

<h:selectManyCheckbox id="checkBoxList" value="#{backedBean.lstIdSelectedItems}" layout="pageDirection"> 
    <f:selectItems value="#{backedBean.lstAvailableItems}" var="item" itemLabel="#{item.label}" itemValue="#{item.value}"> 
     <f:attribute name="clicked" value="#{item.value}" /> 
    </f:selectItems> 
    <f:ajax listener="#{backedBean.itemClicked}" /> 
</h:selectManyCheckbox> 

任何想法?

問候。

回答

11

因此,您對實際價值變化感興趣,而不僅僅是新價值。引入一個valueChangeListener,它將舊值與新值進行比較,並準備ajax偵聽器方法可攔截的一些屬性。

E.g.

<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; // +getter 
private List<Long> selectedItems; // +getter+setter 
private Long selectedItem; 
private boolean selectedItemRemoved; 

@PostConstruct 
public 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

當列表 「selectedItems」 你不加以制止,結束元素,代碼沒有呼叫的方法itemSelected。

+0

答案本簡介屬於OP的評論......請注意,這個問題在四年前被問到,並且有一個被接受(並且徹底)的答案。 – kwishnu

相關問題