Set up hotkey for collapse a panel - jsf

Our project use JSF 2.2 primeface 5.1.
I have a toggleable panel, the collapse attribute set to true.
<p:panel id="panel1" toggleable="true" collapsed="true">
...
</p:panel>
How can I set a p:hotkey in the jsf page to change the attribute collapsed to false ?

At a basic level, you can expand the pane with:
<p:hotkey bind="ctrl+s" handler="thePanelWidgetVar.expand();" />
If what you really want is to toggle the state of the panel, you could just call the collapse() and expand() on the panel directly. Obviously, you need to check the current state of the panel to determine what to call. You can collapse it all into the following script:
<script>
function togglePanelState (thePanel){
if(thePanel.cfg.collapsed){
thePanel.expand();
}else{
thePanel.collapse();
}
}
</script>
You could then use togglePanelState thus
<p:hotkey bind="ctrl+s" handler="togglePanelState(thePanelWidgetVar);" />
EDIT:
Thanks to Hatem Alimam for pointing out that a convenience method similar to my implementation of togglePanelState exists out of the box in PF 5.1, in the form of a toggle() function. Using that, you could then have instead:
<p:hotkey bind="ctrl+s" handler="PF('thePanelWidgetVar').toggle();" />

Related

Remove component from page source when visible is set to false

I have a situation when I want a primefaces' dialog to be displayed only under some conditions so what I made is to set visible attribute. Now I wonder, is it possible to hide component in rendered html when visible is set to false? Let's say someone wiser will work with my application and he will decide to display page's source code and there he will notice my hidden dialog and then he will remove my 'visible' attribute. What happens is that dialog will be displayed to him although it shouldn't be shown. Of course I can write my methods in such way that even if he display the dialog he will not be able to do anything wrong but I would prefer to remove my dialog from rendered html. Is it possible?
Use the rendered attribute and set it to false.
<h:panelGroup layout="block" id="enclosing-panel">
<p:dialog header="Basic Dialog" widgetVar="dlg1" minHeight="40" rendered="#{somecController.dialogRendered}">
<h:outputText value="Resistance to PrimeFaces is futile!" />
</p:dialog>
</h:panelGroup>
Whenever you need the dialog use ajax to refresh a div(<h:panelGroup/>) enclosing the dialog.
<p:commandButton value="show dialog" type="button" oncomplete="PF('dlg1').show();" actionListener="#{somecController.refreshPanel}" update="enclosing-panel" />

Primefaces : update dialog content and keep it open

I'm working with JSF and PrimeFaces, and I can't handle the following situation:
I have a dialog, and I placed a dataTable on it. In one of the cells of the table I would like to display given data in 3 different ways, and I'd like to switch between them. So far I managed to switch between these rendering types via commandLink, but my problem is that when I click on one of the 3 links, the dialog closes! Can I update the content of the dialog, and be able to keep it open the same time? (I'm updating which render type to use via myMethod)
my commandLink looks like this:
<p:commandLink id="id" update=":myForm:myDialog" ajax="false"
action="#{myBean.myMethod}" oncomplete="dialog.show()">
If i don't use the ajax=false attribute, the method is not called, and I also tried imediate=true, but that's not it either.
You need to define an p:outputPanel inside your dialog and update the outputpanel, not the dialog itself (that's why your dialog closes):
<p:dialog id="myDialog" ...>
<p:outputPanel id="myOutputPanel">
... your dialog content goes here
</p>
</p:dialog>
and change your commandlink
<p:commandLink id="id" update=":myForm:myDialog:myOutputPanel" ajax="true"
action="#{myBean.myMethod}" oncomplete="dialog.show()">
Regarding the oncomplete="dialog.show()" - I'm not entirely sure if you need that. A precise answer can be given if you provide more code regarding your table and code.
I had the same problem, and solution is to update a form instead of dialog. For example:
<p:dialog id="id_dialog" ...>
<h:form id="id_form">
... content
</h>
</p:dialog>
and commandLink:
<p:commandLink update=":id_form" process="#all" ...>
This worked for me!

PrimeFaces dialog isn't displayed properly after ajax call that update=#all

I have a single page application written in JSF - PrimeFaces.
There are a couple of dialogs that are displayed correctly at the begining.
But after clicking:
<p:menuitem value="Logout" update="#all" action="#{loginBean.logout}"/>
the dialogs don't open correctly.
Maybe is is possible for you to make an update elements list, for example like:
update=":a:b,:c,:d:e:f".
Many people complains when try to use "#all".
you can also use, its like to show dialog force fully
<p:dialog widgetVar="statusDialog" ....... />
and use onClick = "PF('statusDialog').show();"

PrimeFaces CommandButton: Dynamically enable/disable icon

PrimeFaces's CommandButton allows to specify an icon:
<p:commandButton value="Press me" icon="redBall" ... />
However, I need to enable/disable the icon depending on a JSF managed bean property.
I tried
<p:commandButton value="Press me" icon="#{bean.iconClass}" ... />
This works for choosing different icons, but does not allow to disable the icon altogether (i.e. get the same rendering like without the icon= attribute). I can return an empty string in getIconClass(), but PrimeFaces will still render the extra <span> for the icon inside the button, and CSS styling causes this span to be visible with a default icon.
Is there a way to tell PrimeFaces "I want no icon at all" (other than taking out the icon= attribute altogether)?
I can think of 2 ways without duplicating the button.
Supply the icon as <f:attribute> which is conditionally added by <c:if>.
<p:commandButton ...>
<c:if test="#{not empty bean.icon}"><f:attribute name="icon" value="#{bean.icon}" /></c:if>
</p:commandButton>
Set a style class which hides the icon altogether and supply it conditionally.
.hideicon .ui-icon { display: none; }
.hideicon .ui-button-text { padding-left: 1em; }
with
<p:commandButton ... icon="#{bean.icon}" styleClass="#{empty bean.icon ? 'hideicon' : ''}" />
A lame workaround would be to have 2 commandbuttons. One with icon definition and one without. And then render the correct one.

Displaying the panels dynamically in jsf + RichFaces +Facelets

I am creating a portal in jsf with a left menu(rich:panelMenu) and with a content area on the right side. I want to refresh only the content area with different forms on clicking the menu items in the left menu. The menu selection needs to retained. Which is the proper technique to handle this ?
Do you mean aside from specifying the ID of the content panel in the reRender attribute of your a4j commandButton/commandLink/support tag (or whatever you use as your menu) ?
UPDATE:
Well you could use an a4j include like this:
<rich:panel id="menu">
<a4j:commandLink id="link1" action="#{myBean.setContentViewIdLink1}" reRender="content">Link 1</a4j:commandLink>
</rich:panel>
<rich:panel id="content">
<a4j:include viewId="#{myBean.viewId}"/>
</rich:panel>
UPDATE#2:
The setContenViewIdLink1 might look something like this:
public void setContenViewIdLink1() {
this.contentView = "/page1.xhtml";
}

Resources