我怎样才能在AJAX方法点击项目?(How can I get the clicked item i

2019-07-17 15:06发布

假设这个页面的代码:

<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的用户点击?

我曾尝试使用F:属性标签selectManyCheckbox内,但属性是不是在当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>

有任何想法吗?

问候。

Answer 1:

你是这样的实际值的变化感兴趣,不仅在新的价值。 带来的valueChangeListener其中旧值与新的值进行比较,并准备一些性质其中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; // +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);
}


Answer 2:

当列表中的“selectedItems”你不加以制止,结束元素,代码没有调用该方法itemSelected。



文章来源: How can I get the clicked item in the ajax method?