2013-12-14 21 views
0

我使用<rich:datatable>來顯示來自List<Map<String, String>>(實際上,該列表只有一個Map<String, String>)的數據。 datatable在一行中顯示數據,並顯示該行旁邊的編輯和刪除按鈕。刷新一些內容以使它們在單擊刪除按鈕後消失

當我單擊delete button時,我清除了List和數據庫中的寄存器。我想單擊delete button時,不要顯示datatable中的內容並禁用editdeletebuttons。這裏的數據表代碼:

<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> 

也有一個List<String>顯示datatable下面的一些鏈接。我希望這些鏈接和上面的文本不會再出現,點擊刪除按鈕後。

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

我用2個變量稱爲deleteBtDisablededitBtDisableddisabled property從按鈕的值。在刪除()(下面的代碼)我將這些變量設置爲true,我清除List<Map<String, String>>,變量名ListList鏈接顯示和linkslabel。 這裏的remove()

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

此外,delete buttonrender屬性設置爲datatable's ID。

有了所有這些代碼,當我單擊刪除按鈕時,只有來自數據表的內容消失,但按鈕繼續啓用,並且鏈接列表在那裏繼續。

我該如何解決這個問題?

回答

1

我不是Richfaces用戶。但是:

1)JSF標準CommandButton的Action方法應返回結果導航字符串或空(在你的情況)。例:公共字符串的removeItem(){...}

2)其推薦使用List作爲數據表的價值,而不是地圖,我不知道是什麼類型有你的財產finalResults

3)如果你使用JSF 2.x的,那麼你可以傳遞參數給你actionMethods爲EL ValueExpression:action="#{searchAuthDetailController.removeSelectedAuthor(item.id)}",我建議使用<f:ajax ....>更新,那麼你就可以渲染/只更新頁面

我希望這些幫助的某些部分。

如果你願意,我可以給你一個工作的例子(沒有richfaces)。

UPDATE:例

豆:注意:無需在臉上-config來註冊你的Bean,而不是我在這裏使用的註釋做

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

} 

用戶對象:

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

表格xhtml頁面:

<!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> 
+0

謝謝!你能給我一個例子和一個數據表(不需要richfaces一個)。我不知道如何使用它.. – Luciane

+0

我更新了我的答案。我在Tomcat 7.x上使用JSF 2.2,Mojarra 2.2對其進行了測試。 –

+0

謝謝你的例子!這是我使用的確切配置。查看f:ajax的執行和渲染屬性。我使用標籤的名稱? - 您是否使用@form,因爲h:form標籤? – Luciane