我正在尝试为页面上显示的 alist f 项目实现“将项目添加到购物车”功能<p:dataList>
。添加按钮是一个p:selectBooleanButton。
我已将 ajax 侦听器附加到按钮以将当前项目添加到支持 bean 中的集合中。但是,没有调用侦听器方法,但按钮呈现更新。
附上valueChangeListener
作品,但我认为我不应该使用它?
但是,无论哪种情况,我的分页都会停止工作。单击任何分页按钮不会更改页面内容。
有人可以提供同样的帮助或建议更好的方法吗?
以下是代码:
支持豆
@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;
}
}
索引.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