Refreshing some content to make them disappear after clicking a delete button

1.1k views Asked by At

I'm using a <rich:datatable> to display data from a List<Map<String, String>> (in fact, that list has only one Map<String, String>). The datatable displays the data in a row and it shows an edit and a delete button next to the row.

When I click the delete button I clear the register from that List and from the database. I'd like, when I click the delete button, not to display the content from the datatable and disable the edit and delete buttons. Here's the datatable code:

 <rich:dataTable value="#{searchAuthDetailController.finalResults}" 
        id="table" var="result"> 
            <c:forEach items="#{searchAuthDetailController.variableNames}" var="vname">
                <rich:column>
                         <f:facet name="header">#{vname}</f:facet>
                          #{result[vname]}
                     </rich:column> 
            </c:forEach> 
            <rich:column>
            <h:commandButton action="EditAuthor"  value="Edit" 
            disabled="#{searchAuthDetailController.editBtDisabled}">
                        <f:setPropertyActionListener target="#{editAuthController.selectedAuthor}"  
                        value="#{searchAuthDetailController.selectedAuthor}"/> 
            </h:commandButton>
            </rich:column>
            <rich:column>
            <a4j:commandButton value="Delete" disabled="#{searchAuthDetailController.deleteBtDisabled}"
             action="#{searchAuthDetailController.removeSelectedAuthor()}" render="table"/>
            </rich:column>
       </rich:dataTable>  

Also there's a List<String> displaying some links below the datatable. I'd like those links and the text above not to appear anymore, after I click the delete button.

<h:outputText value="#{searchAuthDetailController.linksLabel}" />
      <br />
       <a4j:repeat value="#{searchAuthDetailController.sameasListToShow}" var="uri" >
            <a href="#{uri}">#{uri}</a>
             <br />
       </a4j:repeat>

I'm using 2 variables called deleteBtDisabled and editBtDisabled as the value from the disabled property from the buttons. At remove() (code below) I set those variables to true and I clear the List<Map<String, String>>, the variablenames List, the List of links displayed and linkslabel. Here's remove():

public void removeSelectedAuthor() {
        this.authMapper.remove(bdModel, this.selectedAuthor);
        this.variableNames.clear();
        this.editBtDisabled = true;
        this.deleteBtDisabled = true;
        this.sameasListToShow.clear();
        this.linksLabel = "";
    }

Also, the delete button has its render property set to the datatable's id.

With all those code, when I click the delete button, Only the content from the datatable disappear, but the buttons continue enabled and the list of links continue there.

How can I solve that problem?

1

There are 1 answers

5
Rami.Q On BEST ANSWER

i am not a Richfaces User. But:

1) an Action method of the JSF standard CommandButton should return the outcome Navigation String or Null ( in your case). ex.: public String removeItem() {...}

2) its recommended to Use List as value of datatables rather than maps, i don't know what type has your property finalResults

3) if you use JSF 2.x then you can pass parameters to your actionMethods as EL ValueExpression: action="#{searchAuthDetailController.removeSelectedAuthor(item.id)}", i recommend using <f:ajax ....> for updating, then you can render/update only some parts of the page

i hope these help.

if you wish i can give you a working example (no richfaces ).

UPDATE: EXAMPLE

Bean: NOTE: no need to register your Bean in faces-config, instead i use here the Annotations to do that

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="bbean")
@SessionScoped
public class MyBean {

    private boolean dataTableRendered;
    private boolean linksRendered;
    private List<User> users;
    private List<String> links;

    public MyBean() {

    }

    @PostConstruct
    public void init(){
        try{
            users = new ArrayList<User>();
            links = new ArrayList<String>();
            dataTableRendered = new Boolean(true);
            linksRendered = new Boolean(true);
            for(int i=0; i<5; i++){
                users.add(new User(i,"User: XYZ-"+i));
            }

            for(int i=0; i<3; i++){
                links.add("http://www.abc-"+i+".tld");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }   

    public final void removeUser(final User selectedUser){
        if( selectedUser == null ){
            return;
        }
        for(User u: this.users){
            if( u.getId() == selectedUser.getId() ){
                users.remove(u);
                break;
            }
        }
        this.setLinksRendered(false);       
    }   

    public boolean isDataTableRendered() {
        return dataTableRendered;
    }
    public void setDataTableRendered(boolean dataTableRendered) {
        this.dataTableRendered = dataTableRendered;
    }
    public boolean isLinksRendered() {
        return linksRendered;
    }
    public void setLinksRendered(boolean linksRendered) {
        this.linksRendered = linksRendered;
    }
    public List<User> getUsers() {
        return users;
    }
    public void setUsers(List<User> users) {
        this.users = users;
    }
    public List<String> getLinks() {
        return links;
    }
    public void setLinks(List<String> links) {
        this.links = links;
    }

}

User Object:

import java.io.Serializable;
public class User implements Serializable{
    private static final long serialVersionUID = -8349963947101031989L;
    private int id;
    private String name;
    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Form xhtml Page:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
<h:head>
    <meta charset="UTF-8" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Test</title>    
</h:head>
<body>
<f:view> 
<h:form id="dataForm">
<h:dataTable id="dataTable" var="user" value="#{bbean.users}"
            rendered="#{bbean.dataTableRendered}"
            >
            <h:column>
                <h:outputText value="#{user.name}"></h:outputText>              
            </h:column>
            <h:column>
                <h:commandButton value="Remove">
                    <f:ajax execute="@form" render="@form" listener="#{bbean.removeUser(user)}" />
                </h:commandButton>
            </h:column>
</h:dataTable>
<h:panelGroup layout="block" id="linksTable" rendered="#{bbean.linksRendered}">
<ui:repeat var="link" value="#{bbean.links}">
<h:outputText value="#{link}, "></h:outputText>
</ui:repeat>
</h:panelGroup>
</h:form>
</f:view>
</body>
</html>