簡體   English   中英

向Datable渲染的JSF PrimeFaces / Ajax無法將項添加到列表中

[英]JSF PrimeFaces / Ajax rendering to Datable fails to add items to the list

我試圖在'panelGrid'中使用一個表單來使用<f:ajax>dataTable呈現值。 但是,使用<h:commandButton>提交時,發送的值不會顯示在dataTable 我沒有在瀏覽器中獲得堆棧或任何控制台錯誤。

這是我的xhtml(簡體):

產品目錄:

    <p:tabView id="tabView" style="width: 65%" >
            <p:tab id="books" title="Books">
                <p:dataGrid var="book" value="#{saleBean.productList}" columns="3"
                            rows="9" paginator="true" paginatorTemplate="{CurrentPageReport} 
                            {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} 
                            {LastPageLink} {RowsPerPageDropdown}"
                            rowsPerPageTemplate="3,6,9">
                    <h:form id="product" >
                    <p:panel header="#{book.name}" style="text-align: center">


                            <h:panelGrid columns="1" style="width: 65%">

                                <!-- Add Book Images here -->
                                <h:outputText value="#{book.price}"/>

                                <h:outputLabel value="Dct (in dollars): " />
                                <h:inputText id="discount" value="#{saleBean.item.unit_discount}" style="width: 50%" />

                                <p:commandButton value="Add to Cart" update=":dataTableForm:sc" 
                                                 action="#{saleBean.doAddItem}"/>


                            </h:panelGrid>


                    </p:panel>
                    </h:form>          
                </p:dataGrid>
            </p:tab>

            <p:tab id="arts" title="Art / Various">
                <!-- Art Products to be added here -->
            </p:tab>

            <p:tab id="other" title="Other">
                <!-- Various Products to be Added here -->
            </p:tab>

    </p:tabView>


    <hr/>        
    <h1>Shopping Cart</h1>
    <hr/>

    <h:form id="dataTableForm">
    <p:dataTable id="sc" value="#{saleBean.sd}" var="item" style="width: 60%">

        <p:column>
            <f:facet name="header">
                <h:outputText value="Product"/>
            </f:facet>
            <h:outputText value="#{item.product_fk}"/>
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="Quantity"/>
            </f:facet>
            <h:outputText value="#{item.quantity}"/>
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="Unit Price"/>
            </f:facet>
            <h:outputText value="#{item.unit_Cost}"/>
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="Unit Discount"/>
            </f:facet>
            <h:outputText value="#{item.unit_discount}"/>
        </p:column>

    </p:dataTable>
    </h:form>

</h:body>

這個bean(簡體):

   public String doAddItem()
    {
        item.setUnit_Cost(product.getPrice());        
        item.setProduct_fk(product);
        item.setQuantity(1);
        sd.add(item); 

        return "productCatalog";
    }

根據PrimeFaces的文檔:

http://www.primefaces.org/docs/vdl/3.4/primefaces-p/commandButton.html

你的actionListener="#{saleBean.doAddItem}"必須使用actionListener="#{saleBean.doAddItem}"對應的函數:

public void doAddItem(ActionListener event)
{
    // do your stuff
}

在這里使用動作就是一個例子:

action="#{saleBean.doAddItem}"

public String doAddItem(void)
{

    item.setUnit_Cost(product.getPrice());        
    item.setProduct_fk(product);
    item.setQuantity(1);
    sd.add(item); 

    return "productCatalog";
}

盡可能使用pf組件,例如p:datatable。 用ap:commandButton替換按鈕並使用process和update而不是f:ajax。

也許最好還是在表單中包含p:datatable。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM