2014-01-27 29 views
0

使用Primefaces我將bean傳遞給用戶剛剛單擊的組件的id。我想讓這個bean改變這個組件的風格。豆的相關部分:無法更新動態創建的組件

public void passIdClicked() { 

    FacesContext context = FacesContext.getCurrentInstance(); 
    Map map = context.getExternalContext().getRequestParameterMap(); 
    String idClicked = (String) map.get("idClicked"); 

    UIComponent tile = FindComponent.doFind(context, idClicked); 

    Iterator<UIComponent> it = tile.getChildren().iterator(); 

    UIComponent comp = it.next(); 

    if (comp.getId().equals(idClicked)) { 
     HtmlOutputText labelDone = (HtmlOutputText) comp; 
     labelDone.setValue("heyyyyyyyyy"); 
     labelDone.setStyleClass("myStyleClass"); 
    } 
} 

修改沒有出現在屏幕上。我錯過了什麼?

[編輯] UI代碼: (。附加組分得到在「磚」 panelGroup中動態地在初始化階段所創建的passIdClicked方法修改這些部件中的一個 - 的一個用戶點擊)

<h:head> 
</h:head> 
<h:body> 
    <h:form id="form"> 
     <h:commandButton actionListener="#{bean.createNewTile()}" title="new" value="new"/> 
     <p:remoteCommand name="sendNameClicked" actionListener="#{bean.passIdClicked}"/> 
    </h:form> 

    <h:panelGroup layout="block" id="tiles"> 
    </h:panelGroup> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('.tile').click(function() { 
       sendNameClicked([{ 
         name: 'idClicked', 
         value: $(this).attr('id') 
        }]); 
      }); 
     }); 
    </script> 
</h:body> 
+0

你能告訴你的UI代碼?如果你使用ajax,你可以使用update =「@ this」。 – Yamada

+0

添加了UI代碼! – seinecle

回答

1

@kocko答案是絕對正確的,但因爲你正在使用Primefaces你也可以做到這一點編程:

RequestContext.getCurrentInstance().update(comp.getClientId()); 
+0

正是我所需要的。謝謝。 – seinecle

1

您將不得不刷新組件,以便發生styleClass更改。

可以,要麼

  • 刷新整個頁面。

例如:

<f:ajax render="@all" /> 

或者

  • 刷新特定部件:

例如:

<p:someComponent id="componentId"> 
    .... 
</p:someComponent> 
... 
<f:ajax render="componentId" /> 
+0

謝謝!我添加了UI代碼的細節。不知道我應該把ajax標籤放在哪裏? – seinecle

+0

我想我從這裏得到了解決方案:http://stackoverflow.com/questions/13893025/refresh-jsf-component-after-custom-javascript-ajax-call我需要將以下內容添加到remoteCommand:process =「 @this「update =」:tiles「 – seinecle