开发者

How to Pass a paramter from commandLink to outputPanel to customize the outputPanel content

开发者 https://www.devze.com 2023-03-14 13:15 出处:网络
I am using the following code to generate an outputPanel on user\'s demand. I want to customize the outputPanel content acc to the user\'s response. Thus I need to pass a parameter from commandLink to

I am using the following code to generate an outputPanel on user's demand. I want to customize the outputPanel content acc to the user's response. Thus I need to pass a parameter from commandLink to ouputPanel. How can I do so ?

<h:form>     

<p:commandLink value="Load" onclick="lazyload()" />  

<p:remoteCommand name="lazyload" update="lazypanel">  
    <f:setPropertyActionListener value="#{true}" target="#{requestScope.shouldRender}"/>  
</p:remoteCommand>                          

<p:outputPanel id="lazypanel">  
    <h:outputText value="This part is lazily loaded" rendered="#requestScope.shouldRender}"/>  
</p:outputPanel>             

<开发者_开发问答/h:form>  


You can use the rendered property like you're doing now. The question is how you want to customize it. There's a fair number of ways depending on the situation. You can do a case by case set of values that are only rendered when their value is true.

i.e.

  <p:outputPanel rendered="#{bean.someBoolCheckCaseOne}" />
  <p:outputPanel rendered="#{bean.someBoolCheckCaseTwo}" />
  ...
  <p:outputPanel rendered="#{bean.someBoolCheckCaseThree}" />

or if you're getting a much wider range you can directly inject HTML into the panel with

  <p:outputPanel ...>
    <h:outputPanel escape="false" value="#{bean.htmlWithoutEscapes}" />
  </p:outputPanel>

As far as passing parameters

  <p:commandLink actionListener="#{bean.onClick}"...>
    <f:attribute name="someParam" value="#{someValue}" /> <!-- you can get this from the component attribute map -->
  </p:commandLink>

//Managed Bean
  public void onClick(ActionEvent evt)
  {
    Object param = evt.getComponent().getAttributes().get("someParam");
  }

Really I think it is pretty trivial to do. Obviously you need to define the input and output. I'd recommend using a bean over the requestScope simply because PrimeFaces 2.2.1 has a null pointer in the converter for text that was just fixed a few weeks back. I'm not sure why you're after the remote command. Its use is pretty specific and unless you have a need for that specificity (I would doubt you do) it is just adding some complexity and places for things to go wrong.

If you want to do it all in requestScope you can do that too... I just wouldn't recommend it.

I use it for things like a search string etc...

<h:inputText value="#{requestScope.searchString}"  style="width:95%;"/>
<p:commandButton value="Search" actionListener="#{inventoryBean.wardrobe.searchViaParameters}" update="inventoryWardrobe:searchForm">
  <f:attribute name="searchString" value="#{requestScope.searchString}"/>
</p:commandButton>

I hope that helps, its a powerful component.

0

精彩评论

暂无评论...
验证码 换一张
取 消