1

I'm considering using the p:steps component

<p:steps activeIndex="1" styleClass="custom" readonly="false">
    <p:menuitem value="Personal" url="#"/>
    <p:menuitem value="Seat Selection" url="#"/>
    <p:menuitem value="Payment" url="#"/>
    <p:menuitem value="Confirmation" url="#"/>
</p:steps>

how can I change activeIndex with Javascript?

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Gyonder
  • 3,674
  • 7
  • 32
  • 49

1 Answers1

2

Normally you should be able to do so using the JavaScript API and using the widgetVar attribute. However, when I use widgetVar="stepsVar", the widget is unknown (JavaScript console):

> PF('stepsVar')
Widget for var 'stepsVar' not available!

So, you need some kind of workaround I'm afraid. For example, keep the index in a managed bean and update it using a remoteCommand.

XHTML

<p:remoteCommand name="setStepIndex"
                 action="#{yourBean.setStepIndexByRemoteCommand}"
                 update="steps"/>
<p:steps id="steps"
         activeIndex="#{yourBean.stepIndex}">
    <p:menuitem value="Personal" />
    <p:menuitem value="Seat Selection" />
    <p:menuitem value="Payment" />
    <p:menuitem value="Confirmation" />
</p:steps>
<button onclick="setStepIndex([{name:'index', value:2}]);return false">Test</button>

Bean

private int stepIndex;

public int getStepIndex()
{
    return stepIndex;
}

public void setStepIndexByRemoteCommand()
{
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, String> map = context.getExternalContext().getRequestParameterMap();
    String indexString = map.get("index");
    stepIndex = Integer.valueOf(indexString);
}

See also

Community
  • 1
  • 1
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • I'd also found that widgetVar was undefined. Anyway,thanks I'll try your approach... – Gyonder Aug 12 '16 at 09:06
  • The steps component is not a 'widget' in the javascript sense. This can be seen in https://github.com/primefaces/primefaces/blob/master/src/main/java/org/primefaces/component/steps/StepsRenderer.java. So it does not have a javascript api and is in that sense a 'readonly' component. – Kukeltje Aug 12 '16 at 09:34
  • Thanks. I was about to report a bug. The `widgetVar` attribute is really misleading. – Jasper de Vries Aug 12 '16 at 09:36
  • Another bug I ran into is binding the steps component. In that case duplicate IDs are generated. – Jasper de Vries Aug 12 '16 at 09:39
  • Please do, it is misleading. I mentioned this before with the PF guys that their components do not throw an error if an unsupported attribute is used. And the 'interactive' version in the showcase does not do anything for me, so it seems like something is wrong (hmmm it does... just navigate to the current page) – Kukeltje Aug 12 '16 at 09:41
  • Done. https://github.com/primefaces/primefaces/issues/1690 and https://github.com/primefaces/primefaces/issues/1691 – Jasper de Vries Aug 12 '16 at 09:48
  • @Kukeltje they closed it as invalid – Jasper de Vries Aug 14 '16 at 11:55
  • you addition is good. Then please add 'documentation' and 'tagLib' to the title. – Kukeltje Aug 14 '16 at 12:18
  • @Kukeltje Do you recon they read comments on closed issues? I guess they don't. Same thing seemed to have happened here https://github.com/primefaces/primefaces/issues/1133 – Jasper de Vries Aug 16 '16 at 13:04
  • Thomas Andraschko at least does (if I remember correctly). Otherwise lets just spam them and create a new issue (but start it with a reference on why the other one was (invalidly, or to quickly) closed – Kukeltje Aug 16 '16 at 13:15