0

我正在设计一个网页,其中有一个 rich:DataTable 我用来在 Database 中添加新的客户信息。

因此,每次单击“添加新”按钮时,丰富的数据表中应该会出现一个新行,其中包含空白输入文本字段。

问题:即使我单击“添加新”按钮多少次,表格中也只剩下一行。

以下是我的网页代码:

<a4j:outputPanel id="rep" rendered="#{adminBean.currentItem == 'Participant' || adminBean.currentItem == 'Administrator'}">
    <rich:panel rendered="#{not empty adminBean.currentItem}" header="Add Customer" id="out">
        <h:panelGrid columns="2">
            <h:commandButton value="Add New" type="button" style="width: 70px" actionListener="#{adminBean.addCustAction}">
                <a4j:ajax  render="out"/>
            </h:commandButton>
            <a4j:commandButton value="Delete" style="margin-left: 10px; width: 70px"></a4j:commandButton>
        </h:panelGrid>

        <rich:dataScroller for="setcust" style="margin-top: 17px"></rich:dataScroller>
        <rich:dataTable id="setcust" value="#{adminBean.customerList}" var="custl" rows="10" style="width: 900px;margin-top: 5px">

            <rich:column id="col1">
                <f:facet name="header">
                    <h:outputText value="Customer ID" style="font-size: smaller; font-weight: bolder; "/>
                </f:facet>
                <h:inputText value="#{custl.Id}"></h:inputText>
            </rich:column>
            <rich:column id="col2">
                <f:facet name="header">
                    <h:outputText value="First Name" style="font-size: smaller; font-weight: bolder;"/>
                </f:facet>
                <h:inputText value="#{custl.FirstName}"></h:inputText>
            </rich:column>
            <rich:column id="col3">
                <f:facet name="header">
                    <h:outputText value="Last Name" style="font-size: smaller; font-weight: bolder;"/>
                </f:facet>
                <h:inputText value="#{custl.lastName}"></h:inputText>
            </rich:column>
            <rich:column id="col4">
                <f:facet name="header">
                    <h:outputText value="Phone" style="font-size: smaller; font-weight: bolder;"/>
                </f:facet>
                <h:inputText value="#{custl.phoneNo}"></h:inputText>
            </rich:column>
            <rich:column id="col5">
                <f:facet name="header">
                    <h:outputText value="Address" style="font-size: smaller; font-weight: bolder;"/>
                </f:facet>
                <h:inputText value="#{custl.address}"></h:inputText>
            </rich:column>
            <rich:column id="col6">
                <f:facet name="header">
                    <h:outputText value="City" style="font-size: smaller; font-weight: bolder;"/>
                </f:facet>
                <h:inputText value="#{custl.city}"></h:inputText>
            </rich:column>
            <rich:column id="col7">
                <f:facet name="header">
                    <h:outputText value="Email" style="font-size: smaller; font-weight: bolder;"/>
                </f:facet>
                <h:inputText value="#{custl.email}"></h:inputText>
            </rich:column>
        </rich:dataTable>
    </rich:panel>
</a4j:outputPanel>

以下是 AdminBean 的代码:

@ManagedBean
@ViewScoped
public class AdminBean {

    private String currentType;
    private String currentItem;
    private List<Customer> customerList = new ArrayList<Customer>();
    private List<Account> accountList;
    private List optionList;

    public List getOptionList() {
        optionList = new ArrayList<String>();
        if (currentType.equals("Add New User")) {
            optionList.add("Participant");
            optionList.add("Administrator");
        } else if (currentType.equalsIgnoreCase("Manage Balance")) {
            optionList.add("Withdrawl");
            optionList.add("Deposit");
        }
        return optionList;
    }

    public void setOptionList(List optionList) {
        this.optionList = optionList;
    }

    public List<Account> getAccountList() {
        return accountList;
    }

    public void setAccountList(List<Account> accountList) {
        this.accountList = accountList;
    }

    public List<Customer> getCustomerList() {
        System.out.println("got list..................");
        return customerList;
    }

    public void setCustomerList(List<Customer> customerList) {
        this.customerList = customerList;
    }

    public String getCurrentItem() {
        return currentItem;
    }

    public void setCurrentItem(String currentItem) {
        this.currentItem = currentItem;
    }

    public String getCurrentType() {
        return currentType;
    }

    public void setCurrentType(String currentType) {

        this.currentType = currentType;
    }

    public void addCustAction(){
        System.out.println("kshitij jain.......actionListener"+customerList.size());

        Customer cust = new Customer();
        customerList.add(cust);
    }
}
4

1 回答 1

0

您的代码中有一些问题:

将所有这些建议结合在一起,您的 JSF 代码应该是(我省略了不必要的代码来测试样式和渲染条件等结果)

<a4j:outputPanel id="rep" >
    <rich:panel id="out">
        <h:form id="frmCustomerData">
            <h:panelGrid columns="2">
                <a4j:commandButton value="Add New"
                    action="#{adminBean.addCustAction}"
                    render="setcust" />
                <a4j:commandButton value="Delete" />
            </h:panelGrid>
            <rich:dataScroller for="setcust" style="margin-top: 17px"></rich:dataScroller>
            <rich:dataTable id="setcust" value="adminBean.customerList" var="custl" rows="10" style="width: 900px;margin-top: 5px">
                <!-- datatable content... -->
            </rich:dataTable>
        </h:form>
    </rich:panel>
</a4j:outputPanel>

无需对托管 bean 进行任何更改。

于 2013-03-23T05:05:19.843 回答