简体   繁体   中英

Cannot seem to remove an object item from an arraylist

I am not able to remove an item from a arraylist. What i tried is pasted below. Cannot seem to figure out what is wrong in below code. The xhtml is

<p:dataTable id="emaildt" styleClass="hide-column-names"
    style="width:70%" rowIndexVar="rowIndex"
    value="#{myBean.emailAddressList}" var="email">
    <p:column style="width:15%">
        <p:inputText id="email" value="#{email.email}"
        validatorMessage="Invalid email" onblur="TrimString(this)">
            <f:validateRegex
            pattern="^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$" />
            <p:ajax event="blur"  
                global="false" listener="#{myBean.checkEmailExist(email)}" />
        </p:inputText>
    </p:column>
    <p:column style="width:13%">
        <p:spacer width="10" rendered="#{myBean.emailAddressList.size()>'1'}"/>
        <p:commandLink  id="delete" immediate="true"
            actionListener="#{myBean.removeEmailFromList}"
            update="emaildt" process="emaildt"
            rendered="#{myBean.emailAddressList.size()>'1' &amp;&amp; false}">
                <h:graphicImage value="../images/button_remove.gif" />
                <f:param name="rowToRemove" value="#{rowIndex}" />
        </p:commandLink>

        <p:spacer width="5" rendered="#{rowIndex == myBean.emailAddressList.size()-1}"/>

        <p:commandLink id="addemailbtn" immediate="true"
            actionListener="#{myBean.addEmail}" 
            style="float:right;" update="emaildt" process="@this emaildt" 
            rendered="#{rowIndex == myBean.emailAddressList.size()-1}">

            <h:graphicImage value="../images/addbutton.jpg" />
        </p:commandLink>
    </p:column>
</p:dataTable>

Create a model class EmailAddressModel with string email and its getters and setters.

MyBean code is

List<emailAddressList> emailAddressList; //make its getters and setters
@PostConstruct
public void fillPage() {

    emailAddressList = new ArrayList<EmailAddressModel>();
    emailAddressList.add(new EmailAddressModel());

}

public void addEmail()  {
    try 
    {

        EmailAddressModel emailAddress = new EmailAddressModel();
        emailAddressList.add(emailAddress);
    }
    catch(IndexOutOfBoundsException I) {
        log.warn("FL Warning", I);

    }
}

public void removeEmailFromList() {
    String rowIndex = null;
    try {
        rowIndex = FacesContext.getCurrentInstance().getExternalContext()
                .getRequestParameterMap().get("rowToRemove");
        int index = Integer.parseInt(rowIndex);
        emailAddressList.remove(index);

    } catch (Exception e) {
        log.warn("FL Warning", e);
    }
}

Issue is -> after we add and enter the values to first and second remove, click on delete of first row, then the second row gets deleted. On debugging, the first row is gets removed in backend arraylist but in front end, the second row gets deleted.

Hope that the above information is helpful.

Check the value of what's passed to rowIndex, as Boris the Spider mentioned, Java is zero indexed.

It's likely you're returning a rowIndex of 1, when the first item is indexed at 0.

To test, In your try-catch block you could use:

 int index = Integer.parseInt(rowIndex) - 1;

Got the solution added atrribute resetValues="true" to p:commandLink . Thank you all helping me..:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM