如何通过手动单击添加新按钮在rich:datatable中添加多行?

时间:2013-03-23 04:21:05

标签: java jsf jsf-2 richfaces

我正在设计一个网页,其中有一个丰富的:DataTable,我用它来在数据库中添加新的客户信息。

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

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

以下是我的网页代码:

<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);
    }
}

1 个答案:

答案 0 :(得分: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进行任何更改。