在视图中更新项目

时间:2016-05-30 13:38:52

标签: jsf primefaces ajax-update

我正在尝试实施产品研究。

我的观点是这样的:

<h:form>
    <p:layoutUnit  position="center">
        <ui:include src="/Desktop/Users/Include/featureCheckbox.xhtml"/>     
    </p:layoutUnit>
    <p:layoutUnit position="south" style="border: 0px">
        <p:commandButton value="Search" 
                         actionListener="#{productsController.searchProdByFeatures()}"
                         oncomplete="listProd.show();"/>
    </p:layoutUnit>
</h:form>
<p:panel widgetVar="listProd" >
    <ui:include src="/Desktop/Users/Include/productsScroller.xhtml"/>
</p:panel>

这是productsScroller.xhtml

OT:我不知道为什么我必须使用它来查看更多数据,只需滚动即可...

<h:form prependId="false"> 
    <p:dataScroller value="#{productsController.items}" var="item" chunkSize="10">
        <f:facet name="header">
            Scroll Down to Load More Wheelchair
        </f:facet>

        <h:panelGrid columns="1" style="width:100%" >
            <p:outputPanel>
                <h:outputText value="#{item.name}" style="font-weight: bold"/>
            </p:outputPanel>
        </h:panelGrid>
        <f:facet name="loader">
            <p:commandButton type="button" value="View More" />
        </f:facet>
    </p:dataScroller>
</h:form>

这是productsController(@Named和@ViewScoped)

...
private Collection<Product> items;
private Collection<Product> selectedItems;


public Collection<Product> getSelectedItems() {
    return selectedItems;
}

public void setSelectedItems(Collection<Product> selectedItems) {
    this.selectedItems = selectedItems;
}

public Collection<Product> getItems() {
    if (items == null) {
        items = this.ejbFacade.findAll();
    }
    System.out.println(items.size());
    return items;
}

public void setItems(Collection<Product> items) {
    this.items = items;
}

public void searchProdByFeatures (){
    List<Product> items= (List<Product>) getItems();
    List<Product> newItems;
    Collection<Feature> featuresCollection = featureController.getSelectedItems();
    List<Feature> selectedFeatures = new ArrayList<>(featuresCollection);
    System.out.println(selectedFeatures.size());    //just for debug
    try {
        for (Feature selectedFeature : selectedFeatures) {
            String msg=(selectedFeature.getName()+" / ");
            System.out.println(selectedFeature.getName());
        }
        newItems=productFacade.findProdByFeatures(selectedFeatures, items);
        System.out.println("Searched product are "+newItems.size());    //just for debug        
        setItems(newItems);
        System.out.println("New items are "+getItems().size());   //just for debug     
    } catch (NotFoundException e) {
        setItems(null);
        JsfUtil.addErrorMessage(e.getMessage());
    }
}

方法 searchProdByFeatures()运行良好,但问题是dataScroller不会更新... 我知道视图中有问题......但是什么?

或者它最好从控制器更新视图?如何?

1 个答案:

答案 0 :(得分:2)

通过添加 update =“yourIddataScroller”

编辑代码以更新dataScroller
<p:commandButton value="Search" 
                         actionListener="#{productsController.searchProdByFeatures()}"
                         oncomplete="listProd.show();" update="yourIddataScroller"/>