简体   繁体   English

获取命名容器中组件的真实ID的更好方法

[英]A better way to get the real ID of an component in a naming container

I got the following scenario: I got several tabs (TabView is a naming container ) and in one of the I got ap:inputText which shows a dialog(which is located in other xhtml file) , now I want the dialog to update the p:inputText , the thing is that the id of the p:inputText is unknow (JSF adds some prefix to it) 我得到了以下场景:我有几个选项卡(TabView是一个命名容器),其中一个我得到了ap:inputText,它显示了一个对话框(位于其他xhtml文件中),现在我希望对话框更新p :inputText,问题是p:inputText的id是unknow(JSF为它添加了一些前缀)

<h:form id="hoursReportFrm">
    <p:inputText id="comment4Dialog" value="#{hoursReportBean.aComment}" 
onfocus="dlg1.show();"/>

I can't use this update="hoursReportFrm:comment4Dialog" in the dialog 我无法在对话框中使用此update =“hoursReportFrm:comment4Dialog”

ATM i looked at the example of this site JSF: working with component IDs (id vs clientId) (its from 2009) ATM我看了这个网站JSF的例子:使用组件ID(id vs clientId) (从2009年开始)

and added binding to to inputtext , like this binding="#{lookup.components.comment4Dialog}" and in the p:commandButton of the dialog I changed to update="#{lookup.clientIds.comment4Dialog}" 并添加了对inputtext的绑定,比如这个binding="#{lookup.components.comment4Dialog}"并在对话框的p:commandButton中我改为update="#{lookup.clientIds.comment4Dialog}"

and It works just fine, but I'm looking for a better way , without the need to bind every component that I would like to access later... 它工作正常,但我正在寻找一种更好的方法,而不需要绑定我以后想要访问的每个组件...

Thanks ahead, 谢谢你,

To be quite honest, I think the binding is probably the easiest route, however when I've been in that situation I've found composite components often offer a better solution than bindings. 说实话,我认为绑定可能是最简单的路径,但是当我遇到这种情况时,我发现复合组件通常提供比绑定更好的解决方案。 Depending on the situation (and again, its totally case by case) you can use a composite component to get around this problem. 根据具体情况(以及完全不同的情况),您可以使用复合组件来解决此问题。 This allows you to reuse parts of a page creatively so that your specific updates don't take a lot of work and you can reuse the code. 这允许您创造性地重用页面的某些部分,以便您的特定更新不需要大量工作,您可以重用代码。

An example of this might be: 一个例子可能是:

//comp:myDialog
...
<composite:interface>
  <composite:attribute name="update" />
  <!-- Other attributes -->
</composite:interface>

<composite:implementation>
  ...
  <!-- Implementation -->
  <p:commandButton update="#{cc.attrs.update}"/>
  ...
</composite:implementation>

And here might be the component in use: 这里可能是正在使用的组件:

//for the sake of argument 'comp' as your library
<h:form id="someForm">
  <p:inputText value="#{bean.changeMe}" id="changeMe"/>
</h:form>

<h:form id="dialog">
  <!-- dialog here -->
  <comp:myDialog update="someForm:changeMe" />
</h:form>

By separating this view into a piece of reusable code you might be able to get around the burden of specifying the full path because it is now much easier to reuse. 通过将此视图分成一段可重用的代码,您可以解决指定完整路径的负担,因为它现在更容易重用。 However, I think it is a toss up of a binding or the composite component depending on the specific case. 但是,根据具体情况,我认为这是绑定或复合组件的折腾。 For reuse, make a new object (component) and reuse it. 要重用,请创建一个新对象(组件)并重用它。 If you're dealing with a highly dynamic environment: bind it. 如果您正在处理高度动态的环境:绑定它。

I'm not an expert by any means, but generally speaking the above has gotten me out of a lot of tough situations. 我无论如何都不是专家,但总的来说,上面让我摆脱了很多困难局面。

EDIT: on a re-read you should also make sure that the tab-view isn't lazily loaded and take a look at the rendering to make sure the path is correct (inspect the ID). 编辑:在重新阅读时,您还应该确保选项卡视图没有延迟加载并查看渲染以确保路径正确(检查ID)。 I've had problems (in older versions of Primefaces) where sometimes the id was nested inside ap:outputPanel or in rare cases the subview id. 我遇到了问题(在Primefaces的旧版本中),有时id嵌套在ap:outputPanel内,或者在极少数情况下嵌套在subview id中。 It might be a very simple fix by specifying the full path ':subview:form:component' though that shouldn't be the case. 通过指定完整路径':subview:form:component'可能是一个非常简单的修复,但情况并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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