【问题标题】:AJAX listener not being fired for <p:selectBooleanButton> inside <p:dataList><p:dataList> 内的 <p:selectBooleanButton> 未触发 AJAX 侦听器
【发布时间】:2013-02-24 15:41:06
【问题描述】:

我正在尝试使用&lt;p:dataList&gt; 为页面上显示的 alist f 项目实现“将项目添加到购物车”功能。添加按钮是p:selectBooleanButton

我在按钮上附加了一个 ajax 侦听器,以将当前项目添加到我的支持 bean 中的集合中。但是,没有调用侦听器方法,但按钮呈现更新。

附加valueChangeListener 可以,但我认为我不应该使用它?

但是,无论哪种情况,我的分页都会停止工作。点击任何分页按钮都不会改变页面内容。

有人可以提供相同的帮助或建议更好的方法吗?

以下是代码:

支持 Bean

@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

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

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

    【讨论】:

    • 但是那个表达式的最终值是真还是假?还是它实际上必须是一个布尔属性?也许我遗漏了一些东西,但确定按钮状态是否应该切换的唯一方法是购物车中是否存在该项目
    • 第一次阅读,是的。但是 JSF 尝试在 Apply 请求阶段设置值,在这里这是不可能的,因为这不是左值(你不能给它赋值,只是读取)。当你在JSF中绑定value属性时,即为两个定向绑定,读写。 selectBooleanButton 是输入组件,因此它不仅读取值,还会写入值。
    • Partlov 是绝对正确的。组件“值”的连接是双向连接,与“仅评估”属性相反,例如渲染。在这些属性中,您可以指定“bean.field ne null”等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多