andriy
andriy

Reputation: 4154

Primefaces change element attribute via Javascript

I'm working on project that uses Primefaces 3.5 (yeah, it is a quite old version). What I wanted to implement is a function in Javascript, that will change attribute of Primefaces element. Is it possible to do?

What I have is a panel defined as:

<p:panel id="mypanel" widgetVar="mypanelWidget" header="A title here" toggleable="true" toggleOrientation="vertical" collapsed="true">

And my idea is to change collapsed="true" to collapsed="false" with Javascript.

This necessity surged because of an ajax call that updates panel, and when it is updated, it appears as collapsed. Doing mypanelWidget.expand() on callback is not a good idea as my page becomes very animated.

Upvotes: 1

Views: 1545

Answers (1)

BalusC
BalusC

Reputation: 1108567

Two options:

  1. Simply use EL in collapsed attribute to check some model state instead of specifying a hardcoded true.

    <p:panel ... collapsed="#{not bean.ajaxMethodCalled}">
    

    Whereby you make sure that isAjaxMethodCalled() returns true if the ajax method of interest was called. It doesn't necessarily need to be a boolean property, it can be anything, including checking the HTTP request parameter map, as long as it evaluates to the desired boolean result.

  2. Don't update the panel itself during ajax call. Instead, update its contents.

    <p:panel ...>
        <h:panelGroup layout="block" id="panelContents">
            ...
            <p:ajax update="panelContents" />
            ...
        </h:panelGroup>
    </p:panel>
    

    This way the state of panel's HTML representation in the HTML DOM tree remains intact.

Upvotes: 2

Related Questions