Question

I'm using: http://www.jquery-steps.com/Examples#async in my project. It's a nice Jquery-plugin for adding wizards.

My question is about dynamic steps. The content of the next step should depend on the answer of the previous step. How can I send additional data with the AJAX call to my backend. My backend will server the next step based on that value.

I searched the documentation and source code, but couldn't find an answer.

Was it helpful?

Solution 2

Currently there are two ways to realize it. One with more and the other one with less effort. But less effort means also less control – in other words jQuery Steps handles showing and hiding the loading message and the async call itself of course. Anyhow, the first solution (less effort) requires that you add a default async step on initialization like you’re used to.

<div id="wizard">
    <h1>Choose</h1>
    <div>
        <select id="choose">
            <option value="0" selected="selected">default</option>
            <option value="1">extraordinary</option>
        </select>
    </div>
    <h1>Result 1</h1>
    <div data-mode="async" data-url="/rest/service/0"></div>
</div>

Then add a small portion of code to the onStepChanging event like melc mentioned. This code should analyze the data of the previous step and remove if necessary the default async step and add an new one at the same position but with a different URL.

<script>
    $(function()
    {
        var wizard = $("#wizard").steps({
            onStepChanging: function(event, currentIndex, newIndex)
            {
                if (currentIndex === 0)
                {
                    if ($("#choose > option:selected").val() === "1")
                    {
                        wizard.steps("remove", 1);
                        // In this case you could also use add instead of insert
                        wizard.steps("insert", 1, {
                            title: "Result 2",
                            contentMode: "async",
                            contentUrl: "/rest/service/1"
                        });
                    }
                }
                return true;
            }
        });
    });
</script>

The other solution is already described by melc.

OTHER TIPS

In the documentation it mentions an event that is fired before changing a step, https://github.com/rstaib/jquery-steps/wiki/Settings#events

So what you need to do is add a callback function on this event and retrieve the data from server based on what has been selected on the current step. Once you get the data refresh the content of the next step.

Care must be taken, since the call to your server is async and the onStepChanging event is called before transitioning to the next step. In order to make it work right both for you and your users (non blocking), you need to display a loading spinner on the next page until you get the response from your ajax call to the server and then replace the spinner by populate the data of the step.

The last step is setting the content of the 'new' empty step. What's the most effective way of setting the context of this step? The ID's of the DIV's are generated dynamically, so I can't select them. PS Can I only give points to 1 answer

Had the same problem with user2779014. Had to use complicated selectors to get the wizard content for the correct step

$.ajax({ url: './Advanced Example Content Loading with AJAX Alternative With More Control.php',
         data: { GenerateContentName: $("#GenerateContentID > option:selected").val() },
         type: 'POST',
         success: function(output) {
                      //Gets the options object (object passed to the steps() function)
                      var options = wizard.data("options");
                      var bodyTag = options["bodyTag"];
                      wizard.children(".content").children(bodyTag).eq(newIndex).html(output);
                  }
});

Full Code: http://plnkr.co/edit/OyHkcZEBv8Fon3vJv7PZ

Note the full code does NOT work without downloading it, and hosting it over a web server, as it uses PHP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top