Change Primefaces p:steps activeIndex with Javascript?

本小妞迷上赌 提交于 2019-12-24 07:59:54

问题


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?


回答1:


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

  • Pass parameter to p:remoteCommand from JavaScript


来源:https://stackoverflow.com/questions/38912243/change-primefaces-psteps-activeindex-with-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!