2

PrimeFaces.current().focus method works with inputTexts but with a commandButton I see no results, alternative I can use executeScript but the idea is to use focus for this kind of requeriment:

This works:

PrimeFaces.current().executeScript("document.getElementById('frmAperturaCajaMasiva:btnAceptarAperturaCajaMasiva').focus();");

This works on inputTexts but not working on button:

PrimeFaces.current().focus("frmAperturaCajaMasiva:btnAceptarAperturaCajaMasiva");

Any idea why? its the same thing but different in both commands

This is the code of the button on the xhtml nothing fancy :)

<p:commandButton id="btnAceptarAperturaCajaMasiva"
                    value="#{etiquetasMsg.cerrar_caja}" styleClass="cds-icon-button"
                    icon="cds-icon aprobar"
                    disabled="#{aperturaMasivaMB.blBtnProcesar}"
                    title="#{tooltipsMsg.cierrecaja_masiva_cerrar}"
                    onclick="if(!confirmarSeleccionTabla(PF('dtbFrmCajaWv'),null)){ return false; }"
                    actionListener="#{aperturaMasivaMB.validarCierreCajaMasivo}"
                    rendered="#{adminRestriccionMB.validarRestriccion('BTN_CERRAR_CAJAMASIVO')}" />
BugsForBreakfast
  • 712
  • 10
  • 30

1 Answers1

2

Yes I know why. The focus method in PrimeFaces specifically excludes buttons it was intended to focus input fields. Here is the source code.

focus: function(id, context) {
    var selector = ':not(:submit):not(:button):input:visible:enabled[name]';

Notice the "not(:submit):not(:button)" in the Jquery selector.

Source code: https://github.com/primefaces/primefaces/blob/master/primefaces/src/main/resources/META-INF/resources/primefaces/core/core.js#L699-L728

Emil Sierżęga
  • 1,785
  • 2
  • 31
  • 38
Melloware
  • 10,435
  • 2
  • 32
  • 62
  • Hey dude where did you find this info?? – BugsForBreakfast Jul 19 '19 at 13:02
  • I updated the answer with a link to the PrimeFaces Source code where its happening. – Melloware Jul 19 '19 at 13:06
  • Hmm INTERESTING why would they do that! thanks mate I guess I will continue using PrimeFaces.current().executeScript("document.getElementById('frmAperturaCajaMasiva:btnAceptarAperturaCajaMasiva').focus();"); for focusing buttons :) – BugsForBreakfast Jul 19 '19 at 13:09
  • Hey @Melloware just wanted to let you know something curious about focusing buttons in PF 7.0, if I use PrimeFaces.current().executeScript("document.getElementById('frmAperturaCajaMasiva:btnAceptarAperturaCajaMasiva').focus();"); for focusing a button, it won't work alone, it must be precede by PrimeFaces.current().focus("frmAperturaCajaMasiva:btnAceptarAperturaCajaMasiva"); and then it will focus the button, any idea why? its like he need the primefaces focus call and also the execute script focus call to focus on a button, for inputtext it will work with only one of those calls. – BugsForBreakfast Aug 01 '19 at 16:33
  • In 3.5 I only needed PrimeFaces.current().executeScript("document.getElementById('frmAperturaCajaMasiva:btnAceptarAperturaCajaMasiva').focus();"); for focusing button, without precede of PrimeFaces.current().focus("frmAperturaCajaMasiva:btnAceptarAperturaCajaMasiva"); – BugsForBreakfast Aug 01 '19 at 16:34
  • Why not just call 1 JS line `PrimeFaces.focusElement($('#frmAperturaCajaMasiva\\:btnAceptarAperturaCajaMasiva'));` – Melloware Aug 02 '19 at 11:09
  • I didn't saw that method focusElement, gonna try it, why do you use those \\? – BugsForBreakfast Aug 02 '19 at 13:13
  • Hmm it won't work mate, only if I call focus function and then also executeScript for focus it will finally focus the button, for inputtext I just use .focus and it works, the thing is that for buttons I should be only using executeScript but for some reason it needs focus method too – BugsForBreakfast Aug 02 '19 at 13:47
  • That doesn't make sense. – Melloware Aug 02 '19 at 20:18
  • So what do I do? – BugsForBreakfast Aug 02 '19 at 21:05
  • Keep debugging. There has to be a reason why it works and doesn't work. – Melloware Aug 03 '19 at 10:53