没有为< p:selectBooleanButton>触发AJAX侦听器在< p:dataList>内部 [英] AJAX listener not being fired for <p:selectBooleanButton> inside <p:dataList>

查看:77
本文介绍了没有为< p:selectBooleanButton>触发AJAX侦听器在< p:dataList>内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用<p:dataList>为页面上显示的f个商品实现将商品添加到购物车"功能.添加按钮是 p:selectBooleanButton .

I am trying implement an "Add item to Cart" functionality for alist f items displayed on page using <p:dataList>. The add button is a p:selectBooleanButton.

我在按钮上附加了ajax侦听器,以将当前项目添加到我的backing bean中的集合中.但是,不会调用listener方法,而是会更新按钮渲染.

I have attached an ajax listener to the button to add current item to a collection in my backing bean. However, the listener method is not being invoked but the button rendering updates.

安装valueChangeListener可行,但我认为我不应该使用它?

Attaching a valueChangeListener works but I assume I should not be using it?

但是,无论哪种情况,我的分页都无法正常进行.单击任何一个分页按钮都不会更改页面内容.

有人可以帮忙还是建议更好的方法?

Could someone please assist with the same or suggest a better approach?

以下是代码:

后备豆

@ManagedBean(name = "movies")
@ViewScoped
public class MovieListBean extends BaseBean implements Serializable
{
   private static final long serialVersionUID = -1887386711644123475L;

   private LazyDataModel<Movie> lazyModel;

   private Map<Integer, Rental> cart;

   @PostConstruct
   public void initialize() {
      cart = new HashMap<>(0);
   }

   public LazyDataModel<Movie> getLazyModel() 
   {
      if (lazyModel == null) 
      {
         lazyModel = new LazyDataModel<Movie>() {

            private static final long serialVersionUID = -858683609299266223L;

            @Override
            public List<Movie> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) 
            {
               List<Movie> movieList = serviceLocator.getMovieService().getMovieCatalogInRange(first, (first + pageSize));
               this.setRowCount(serviceLocator.getMovieService().getCatalogSize());
               return movieList;
            }
         };
      }
      return lazyModel;
   }

   public void updateCart(Object obj)
   {
      System.out.println("Selected Movie ID" + obj);
      if (lazyModel != null)
      {
         System.out.println("Selected row in datalist - " + lazyModel.getRowIndex());
         System.out.println("Object instance associated with selected row - " + lazyModel.getRowData());
      }
      // Process object instance and add to cart
   }

   public Map<Integer, Rental> getCart() {
      return cart;
   }

   public void setCart(Map<Integer, Rental> cart) {
      this.cart = cart;
   }

}

index.xhtml

<ui:composition template="/WEB-INF/templates/template.xhtml">
   <ui:define name="content">
      <h:outputStylesheet name="movies.css" library="styles" />

      <h:form prependId="false" id="form">
         <p:dataList value="#{movies.lazyModel}" var="movie" id="movies" paginator="true" rows="10"
            paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
            type="none" paginatorAlwaysVisible="false" lazy="true">

            <p:panel id="movieInfo">
               <h:outputText value="#{movie.movieName}" styleClass="titles" />
               <div id="infoContainer">
                  <div>
                     <h:graphicImage library="images" name="#{movie.imageUri}" />
                  </div>
                  <div>
                     <div>
                        <h:outputText value="#{movie.plotLine}" />
                     </div>
                     <div class="rating">
                        <span>Rating : </span>
                        <p:rating value="#{movie.rating}" stars="10" readonly="true" />
                     </div>
                     <div id="rent">
                        <p:selectBooleanButton offLabel="Add to Cart" onLabel="Remove from Cart" id="btnRent"
                        value="#{not empty movies.cart[movie.movieID]}">
                           <p:ajax update="btnRent" listener="#{movies.updateCart (movie.movieID)}" event="click"/>
                        </p:selectBooleanButton>
                     </div>
                  </div>
               </div>
            </p:panel>

         </p:dataList>
      </h:form>

   </ui:define>
</ui:composition>

我正在使用JSF 2.1(Mojarra)+ Primefaces 3.5

I am using JSF 2.1 (Mojarra) + Primefaces 3.5

推荐答案

您的p:selectBooleanButton的值为#{not empty movies.cart[movie.movieID]}.这是不可能的.它应该是一个布尔左值,带有getter和setter的某些属性.您如何看待JSF与AJAX一起发送时会设置字段?由于此按钮位于数据表中,因此您可以将它们的值组织为布尔数组.

Value of your p:selectBooleanButton is #{not empty movies.cart[movie.movieID]}. This is not possible. It should be a Boolean lvalue, some property with getter and setter. How you think JSF will set field when it is sent with AJAX? As this buttons are in datatable you can organize their values as array of Boolean.

这篇关于没有为&lt; p:selectBooleanButton&gt;触发AJAX侦听器在&lt; p:dataList&gt;内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆