3.0.M3 p:focus does not work

UI Components for JSF
Post Reply
Cool Mr Ice
Posts: 46
Joined: 17 Sep 2011, 11:53

19 Sep 2011, 08:25

The environment I am using is described in my signature.

Goal #1: when the page loads or after a successful "save" the focus needs to be in inputText input1
Goal #2: when any of the three inputText fields do not pass validation, the focus needs to be set on the first invalid inputText field

The above goals are what PrimeFaces claims its p:focus does and the examples in the showcase work (in the showcase). However the closest I have been able to get to p:focus working is the code as follows:

Code: Select all

            <h:form>
                <h:panelGrid id="inputPanel" columns="3">
                    <h:outputLabel value="Input 1"/>
                    <p:inputText id="input1" value="" required="true" requiredMessage="Input 1 is required"/>
                    <p:message for="input1"/>
                    
                    <h:outputLabel value="Input 2"/>
                    <p:inputText id="input2" value="" required="true" requiredMessage="Input 2 required"/>
                    <p:message for="input2"/>

                    <h:outputLabel value="Input 3"/>
                    <p:inputText id="input3" value="" required="true" requiredMessage="Input 3 required"/>
                    <p:message for="input3"/>
                    
                    <f:facet name="footer">
                        <p:focus/>
                        <p:commandButton value="Save" update="inputPanel"/>
                    </f:facet>
                </h:panelGrid>
            </h:form>
...and all that does is accomplish Goal #1. As for Goal #2, it always sets focus to input1 no matter which fields are in error.

I have tried moving <p:focus/> all over the place!!! Outside the PanelGrid, outside the form, etc. I've also tried setting an id (<p:focus id="focus1"/>) and updating it in the commandButton -- no luck.

P.S. I have read the 2.2 PrimeFaces User Guide, studied the PrimeFaces 2.2.1 and 3.0.M3 showcases, and searched this forum and others for answers. No doubt I'm doing something idiodic for this to not work, but I am not placing this post as a first resort to find a solution for my problem.
PrimeFaces 3.5
JSF 2.1
Mojarra 2.1.3
Glassfish 3.1.1
Netbeans 7.0.1
JDK 7
JEE 6

Cool Mr Ice
Posts: 46
Joined: 17 Sep 2011, 11:53

20 Sep 2011, 03:35

Well, at least someone validate or invalidate what I am saying. "Hey, it is working for me" or "Haven't you heard - it is broken for everyone, they will fix it in the 3.0 final release."

This is bizarre. I figured out of the litany of problems I am having with PrimeFaces, this one would be an easy one for someone to give a quick response. But, no... crickets are chirping.

Could someone at least point me to a Bugzilla for PrimeFaces? I think this qualifies as a bug.... right?
PrimeFaces 3.5
JSF 2.1
Mojarra 2.1.3
Glassfish 3.1.1
Netbeans 7.0.1
JDK 7
JEE 6

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

20 Sep 2011, 10:30

Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

20 Sep 2011, 20:22

Well, the position it is in now does not seem logical at all. Have you also tried putting it beween form and panelgrid?

Oh and complaining like you do often has the opposit effect.... (just a suggestion)

Cool Mr Ice
Posts: 46
Joined: 17 Sep 2011, 11:53

30 Sep 2011, 15:06

kukeltje wrote:Well, the position it is in now does not seem logical at all. Have you also tried putting it beween form and panelgrid?
Thank you for your time & response. You are right, that position does not seem logical. That is part of what drove me to post. It was only working if the focus was in the h:panelGrid, but if the p:focus is placed directly into the h:panelGrid the columns would be off whenever a h:facesMessage was displayed. The only other place that I could find to accomplish Goal #1 was inside the f:facet --yuck!!!

Yes, I had tried the position you are suggesting and it did not work. What is working for me now is as follows:

This code is on a form that displays a p:dialog (in another form) containing the p:focus in question...

Code: Select all

                                        <p:menuitem value="New"
                                                    icon="ui-icon ui-icon-plus"
                                                    actionListener="#{serviceTypeController.prepareCreateServiceType}"
                                                    onclick="serviceTypeCreateDialogWVar.show();"
                                                    oncomplete="resetServiceTypeCreate();"
                                                    process="@this"
                                                    update="serviceTypeCreatePanel"
                                                    immediate="true"/>
Notice the oncomplete in the p:menuitem above. It calls javascript that sets the focus on the text field inside the dialog. I have tried adding the id of p:focus to the update list of the p:menuitem - no luck. Why won't p:focus do this on its own like in the showcase?

Code: Select all

function resetServiceTypeCreate() {
    serviceTypeNameCreateWVar.value='';
    document.getElementById('serviceTypeNameCreate').focus();
}

Okay the code below works beautifully. Once the javascript gives the p:inputText its focus, the user can tab and reverse tab between the p:inputText and the two p:commandButtons and nothing weird happens. If there is a validation error, the first field in error gets focus (i did throw several other p:inputText on the screen to test this). Once a successful process/update happens on the panel however, the p:inputText does not receive focus as it should. See the javascript below.

Code: Select all

            <h:form id="serviceTypeCreateForm" prependId="false">
                <p:dialog widgetVar="serviceTypeCreateDialogWVar"
                          header="Create New Service Type"
                          modal="true"
                          closable="false"
                          resizable="false"
                          showEffect="fold"
                          hideEffect="explode">
                    <p:panel id="serviceTypeCreatePanel">
                        <p:focus id="serviceTypeCreateFocus"/>
                        <h:panelGrid columns="3">
                            <h:outputText value="#{bundle.CreateServiceTypeLabel_servicetypename}"/>
                            <p:inputText id="serviceTypeNameCreate"
                                         widgetVar="serviceTypeNameCreateWVar"
                                         tabindex="1"
                                         value="#{serviceTypeController.serviceType.servicetypename}"
                                         required="true"
                                         requiredMessage="#{bundle.CreateServiceTypeRequiredMessage_servicetypename}"/>
                            <p:message for="serviceTypeNameCreate"/>
                        </h:panelGrid>
                        <p:separator/>
                        <p:commandButton tabindex="2"
                                         image="ui-icon ui-icon-check"
                                         title="#{bundle.CreateServiceTypeSaveLink}"
                                         actionListener="#{serviceTypeController.createServiceType}"
                                         process="@this serviceTypeCreatePanel"
                                         update="serviceTypeCreatePanel serviceTypeCreateFocus"
                                         oncomplete="setNoErrorsFocus(args, 'serviceTypeNameCreate');"/>
                        <p:commandButton tabindex="3"
                                         image="ui-icon ui-icon-close"
                                         title="Close"
                                         update="serviceTypeCreatePanel serviceTypes"
                                         oncomplete="serviceTypeCreateDialogWVar.hide(); refreshDataTable(serviceTypesWVar); "
                                         immediate="true"/>
                    </p:panel>
                </p:dialog>
            </h:form>
Once the first p:commandButton above is processed successfully, this will reset the focus to the first p:inputText.

Code: Select all

function setNoErrorsFocus(args, focusElementId) {
    if(args.notValid || args.validationFailed)
        return;

    document.getElementById(focusElementId).focus();
}
Note - once I got it working to this point, I switched on "Good Vibrations" and danced like Marky Mark for an hour give or take.

Now, a couple of things. In the showcase, there is no id required for the p:focus. The p:commandButton updates the outer p:panel and that is enough to reset the focus to the first open text field (or first in error). Also, as mentioned before, I have to use javascript to initially set the focus and reset the focus to the appropriate p:inputText after a successful ajax process/update. For whatever reason, I can strip everything out to the bare bones like I did in my original post and it will not work as advertised in the showcase.

kukeltje wrote:Oh and complaining like you do often has the opposit effect.... (just a suggestion)
Umm... I would venture to say that 99.9% of all posts on this forum is someone complaining that something is not working for them. If you perceived something I wrote as problematic, feel free to point it out. Otherwise, I feel that I have made factual statements from my point of view. Anyone is free to try to convince me otherwise. I did say...
Cool Mr Ice wrote: ... No doubt I'm doing something idiodic for this to not work...
That implies I am willing to take 100% of the responsibility for <p:focus> not working for me.

Other than that, I'm just here to learn as much as I can about PrimeFaces and help wherever I can be of service. My heart goes out to anyone who is hurt by anything I have said. My mind does not.
PrimeFaces 3.5
JSF 2.1
Mojarra 2.1.3
Glassfish 3.1.1
Netbeans 7.0.1
JDK 7
JEE 6

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

16 Jan 2014, 07:50

Cool Mr. Ice wrote:Note - once I got it working to this point, I switched on "Good Vibrations" and danced like Marky Mark for an hour give or take.
Did you video-tape that and upload to youtube? :)

You and I have spoke on a few occasions in the past, but wow, I like your last/latest post on this topic. I just stumbled upon an issue in my app that is related to p:focus, and this topic of yours is motivating me to use javascript (and remotecommand/etc...) to resolve an issue (exception, server error 500) that is caused by or related to p:focus id="columnWithFocus" on two different pages, same ID, but different forms, and the 2nd page is rendered via ajax=false.

To duplicate issue

1. open ORDERS ADD page, which contains the following (and no issues with p:focus on this page)

Code: Select all

<h:form id="orderAddForm" >

	<p:focus id="columnWithFocus" for="#{pf_orderCostDetailsController.columnWithFocus}"/>
2. optional: edit (or press <Tab> key on keyboard to tab to) any of the first few p:inputText on the page

3. click Quick Quote on menubar

Code: Select all

<p:menuitem value="Quick Quote"  ajax="false" immediate="true"
            action="#{pf_ordersController.prepareQuickQuote()}"
            onclick="displayLoadingImage(true)" oncomplete="displayLoadingImage(false)"/>
Quick Quote page has the following:

Code: Select all

<h:form id="quickQuoteForm" >

	<p:focus id="columnWithFocus" for="#{pf_orderCostDetailsController.columnWithFocus}"/>
but below is the result 'every' time the steps above are repeated (and component ID mentioned in the exception below will always be the last component that had focus on ORDERS ADD page)

Code: Select all

javax.faces.FacesException: Cannot find component with expression "orderAddForm:groupSizeOnMobile" referenced from "quickQuoteForm:columnWithFocus".
so, for now, my workaround is the following:

Code: Select all

<p:menuitem value="Quick Quote"  ajax="false" immediate="true"
            rendered="#{!pageNavigationController.isPageOrdersAdd()}"
            action="#{pf_ordersController.prepareQuickQuote()}"
            onclick="displayLoadingImage(true)" oncomplete="displayLoadingImage(false)"/>
which does 'not' render the menu option when ORDERS ADD page is the current view/page in the browser.

Today, my versions are as follows:

Code: Select all

PrimeFaces Elite 4.0.6, Extensions 1.2.1, Push (Atmosphere 2.1)
TomEE 1.6.1 (Tomcat 7.0.50), MyFaces Core 2.2.0
JUEL 2.2.6 | OmniFaces 1.6.3 | EclipseLink-JPA/Derby | Chrome
The same also occurs with TomEE 1.6.0, Tomcat 7.0.47, and MyFaces Core 2.1.13.
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

User avatar
andyba
Expert Member
Posts: 2473
Joined: 31 Mar 2011, 16:27
Location: Steinfeld, near Bremen/Osnabrück, DE
Contact:

16 Jan 2014, 14:08

This has to be worth a new Thread Howard.
PF 4.x (Elite versions), PF 5, Pf 5.1, PF 6.0
Glassfish 4.1, Mojarra 2.x, Java 8, Payara 4.1.1.
If you haven't read the forum rules read them now

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 45 guests