4

I'm working with JSF and PrimeFaces, but I need to get the value of the id of a component. Because I'm building dinamycally panels with diferent id, to show a the panel I need to compare if is the current panel, then show it.

For example if I've the next panel

<p:outputPanel id="#{bean.getID}" autoUpdate="true"
   renderer=#{@this.id == bean.currentPanel}
>
</p:outputPanel>

And Bean

public class Bean(){
  private int numberPanels =0;
  private int currentPanel = 0;

  public int getID(){
     //...a process that return different ID
  }
  // getter's and setters

}

Obviously, @this.id doesn't work. So, how to get the value of ID of componente ne JSF with PrimeFaces?

Cristian
  • 1,480
  • 5
  • 32
  • 65
  • First, you should use `#{bean.ID}` instead of `#{bean.getID}`. Second, you should not have business logic in your getters methods, see [here](http://stackoverflow.com/q/2090033/1065197) (if you still insist to place business logic in the getter, don't complain for performance problems later =\\). To the question, I guess you can dynamically bind the ``. I haven't tested this but may do what you want/need (but you should change `bean.currentPanel` to `String`). – Luiggi Mendoza Sep 24 '13 at 17:42
  • Related: http://stackoverflow.com/q/8168302/1065197 – Luiggi Mendoza Sep 24 '13 at 17:44
  • 1
    @LuiggiMendoza There are at least 3 typos in the text besides the getter! – skuntsel Sep 24 '13 at 17:46

1 Answers1

9

There is an implicit #{component} object in the EL scope that evaluates to the current UI component. Given this information, you'll get the following attribute:

rendered="#{component.id eq bean.currentPanel}"
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • Could you post any documentation to this `#{component}`as well? – Luiggi Mendoza Sep 24 '13 at 17:46
  • 3
    @Luiggi: it's listed in "getValue" table entry of chapter 5.6.2.1 of JSF 2 spec (page 5-25) and -of course- in the known [Communication in JSF 2](http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ImplicitELObjects) article. – BalusC Sep 24 '13 at 17:57
  • @LuiggiMendoza It is listed in section 5.6.2.1 of the JSF 2.0 specification "Implicit Object ELResolver for Facelets and Programmatic Access". – skuntsel Sep 24 '13 at 18:13
  • @BalusC I most probably knew of that object from your famous post. But finding it in the Specification was also worth doing. Took some time, though, as I'm on my mobile. Sorry for the overlapping comment! – skuntsel Sep 24 '13 at 18:18
  • I found a trouble, when I call `rendered={component.id eq bean.currentPanel}` the `component.id` it is as if you have not assigned, because always I print the values and some cases are equals , so how to assigned the value after?. – Cristian Sep 30 '13 at 10:34