1

当用户选择选项列表目标列表中的项目时,我想重新渲染组件。

我怎么做?


更新(2013 年 12 月 17 日)

我在这里描述了这个问题的完整解决方案 - http://leonotepad.blogspot.com.br/2013/12/primefaces-capturing-target-list-item.html

谢谢@Hatem,如果没有您的回答,我无法解决这个问题。

基本上是这样的:

我有一个带有 ap:pickList 的 @ViewScoped 托管 bean,由 ap:selectMany 组件使用 ap:ajax 更新事件过滤。

像这样

<p:outputLabel for="siteFilter" value="Site Filter:" style="width:100px;"/>
<p:selectOneMenu converter="#{siteTypeConverter}" id="siteFilter" value="#{myMB.selectedSiteType}" style="width:200px;">              
   <f:selectItem itemLabel="Select Site Type" itemValue="#{null}" />
   <f:selectItems value="#{myMB.siteTypeFilterList}" var="st" itemLabel="#{st.name}" itemValue="#{st}" />
   <p:ajax update="sites" listener="#{myMB.handleSiteTypeFilterChange}" oncomplete="rebindClicks()"/>
</p:selectOneMenu>

<p:outputLabel for="sites" value="Sites:" style="width:100px;"/>
<p:pickList
   widgetVar="xyzabc"
   id="sites"
   value="#{myMB.sites}"
   var="site"                 
   converter="#{siteConverter}"
   itemLabel="#{site.siteType.name}.#{site.name}"
   itemValue="#{site}" /> 

这里的想法是在用户单击某些 pickList 目标列表项时触发一个新事件。

因此,正如@Hatem 所建议的,我们必须将点击事件绑定到每个目标元素。像这样

(...)
   <p:remoteCommand name="updateCommand" action="#{myMB.updateAccounts}" update="accounts"/>
</h:form>
<h:outputScript library="js" name="pickListHack.js" />   
(...)

请注意,我必须在表单之后添加它。

pickListHack.js 位于 WebContent/resources/js/pickListHack.js

function rebindClicks() {
    xyzabc.jq.on('click','.ui-picklist-target li',function(){
        var item = $(this).attr('data-item-value');
        alert(item);
        updateCommand([{name:'param', value:item}]);
   });
};

$(document).ready(function() {
    rebindClicks();
});

这里的技巧似乎是,在更新事件之后,绑定丢失了,所以我们必须在更新后重新绑定它们。这就是为什么 p:ajax 有那个 oncomplete 事件。

最后,当用户点击目标列表中的元素时,我们调用 updateCommand,通过 p:remoteCommand 声明。

请注意,所选项目作为名为“param”的参数传递。我们像这样在托管 bean 中获取这个参数

public void updateAccounts(){
   String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("param");
   System.out.println(">"+value);
   this.accounts = value;
}

在这种情况下,值只是 ID,因为这就是我的 siteConverter 对对象所做的。

@ManagedBean
@RequestScoped
public class SiteConverter implements Converter,Serializable{
    private static final long serialVersionUID = -194442504109002565L;

    @EJB
    private MyEJB myEJB;

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
            throws ConverterException {

        if (arg2 != null){
            Site s = myEJB.getSiteById(Long.parseLong(arg2));
            return s;
        }else{
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
            throws ConverterException {

        if (arg2 != null){
            Site s = (Site)arg2;
            return String.valueOf(s.getId());
        }else{
            return null;
        }
    }
}
4

1 回答 1

1

这种方式应该可以。

远程命令

<p:remoteCommand name="updateCommand" update="componentId" />  

p:pickList

<p:pickList id="pickList" widgetVar="pickListVar" value="#{pickListBean.cities}" 
 var="city" itemLabel="#{city}" itemValue="#{city}" />

JS

$(document).ready(function() {
   PF('pickListVar').jq.on('click','.ui-picklist-target li',function(){
       updateCommand();//remoteCommand to update
       //and you can get the item value and lable
       $(this).attr('data-item-value');
       $(this).attr('data-item-lable');
   });
});

编辑:

而且,如果您的 pickList 被另一个组件定期更新,那么您毕竟会丢失 click 事件。

在这种情况下,您可能希望绑定对pickList 的父容器组件的单击。

例如:

 $('#formId').on('click','.ui-picklist-target li',function(){
       updateCommand();//remoteCommand to update
       //and you can get the item value and lable
       $(this).attr('data-item-value');
       $(this).attr('data-item-lable');
   });

但同样,如果任何其他组件更新了整个表单,您将失去事件绑定..

于 2013-12-16T21:29:02.997 回答