【问题标题】:Can't update components created dynamically无法更新动态创建的组件
【发布时间】:2014-01-27 17:08:28
【问题描述】:

使用 Primefaces,我将用户刚刚单击的组件的 ID 传递给 bean。我希望 bean 改变这个组件的样式。 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");
     }
}

修改没有出现在屏幕上。我错过了什么?

[编辑] 用户界面代码: (其他组件在初始化阶段动态创建在“tiles”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>

【问题讨论】:

  • 你能展示你的 UI 代码吗?如果您使用的是 ajax,您可以只使用 update="@this"。

标签: jsf jsf-2 primefaces


【解决方案1】:

@kocko 的回答是绝对正确的,但是由于您使用的是 Primefaces,您也​​可以通过编程方式进行:

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

【讨论】:

  • 正是我需要的。谢谢。
【解决方案2】:

您必须刷新组件,以便 styleClass 发生更改。

你可以,任一:

  • 刷新整个页面。

例如:

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

或者

  • 刷新特定组件:

例如:

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

【讨论】:

  • 谢谢!我添加了 UI 代码的细节。不知道应该把 ajax 标签放在哪里?
  • 我想我从这里得到了解决方案:stackoverflow.com/questions/13893025/… 我需要在远程命令中添加以下内容:process="@this" update=":tiles"
猜你喜欢
  • 2018-09-26
  • 2015-01-23
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 2020-06-23
  • 2019-09-27
  • 1970-01-01
  • 2021-08-01
相关资源
最近更新 更多