0

Hi i am very new to primefaces. I Tried the following functionality.

I add a button to primefaces datatable as a column.

Based on the button I add new row to same data table.

For this i use the following code.

in vendorcontacts.xhtml

    <p:dataTable id="dt" var="pmr" value="#{globalMaster.vendorContactsList}"
                                    style="width:900px;">
                                    <p:column headerText="Organisation">
                                        <h:outputText value="#{pmr.contactSno}" />
                                    </p:column>

                                    <p:column headerText="Program">
                                        <h:outputText value="#{pmr.location}" />
                                    </p:column>

                                    <p:column headerText="Action" >
                                       <p:commandButton value="Add New" action="#{globalMaster.getVendorContactsList}"  />
                                    </p:column>
                                </p:dataTable>

And my Bean code is:

public List<VendorContactDetails> getVendorContactsList(){
    try{
        VendorContactDetails veneContactDetails=new VendorContactDetails();
        vendorConList.add(veneContactDetails);

    }catch(Exception e){
        e.printStackTrace();
    }return vendorConList;
}

And now the problem is;

Intially my view page displays single row.

When i click the add new button my view page displays 3 rows instead of second row.

I find something in debug mode my list method calls no.of times(i.e no.of properties in my modal class i.e VendorContactDetails)

I change my bean scope session to view scope .but no use.

Is there any mistake in my code.Please guide me

1 Answer 1

1

first of all, view scope is correct, but;

getVendorContactsList() should just initialize the list if needed, and return it.

public List<VendorContactDetails> getVendorContactsList(){
    if (vendorConList == null)
    {
        vendorConList = new ArrayList<VendorContactDetails>();
    }
    return vendorConList;
}

then, there should be a separate method to add an item to the list.

public void addVendorContactToList(){
    getVendorContactsList().add(new VendorContactDetails());
}

and at last, the button adding a new item should not be in a table column. it's not an operation related to an existing item/row.

<p:commandButton value="Add New" process="@this" update="dt"
    action="#{globalMaster.addVendorContactToList}"/>

if you are planning to update or delete an item in list, then those buttons will be in table columns.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.