jquery-steps | send data to server on ajax content load
Asked Answered
K

3

6

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.

Karyokinesis answered 19/10, 2013 at 10:24 Comment(0)
D
6

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.

Despondent answered 19/10, 2013 at 13:1 Comment(1)
Thank you both. I prefer having more control, so I have implemented an solution like Melc suggested. On my onStepChanging event I call an function to make an Ajax call and sends the current index/title to the backend. The backends responds with the corresponding content. In the mean time, the next (empty) step is shown to the user with an loader animation. 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?Karyokinesis
F
10

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.

Farming answered 19/10, 2013 at 10:31 Comment(2)
Thank you! How can I give points/a vote for your answer as well?Karyokinesis
@Karyokinesis You can upvote by clicking the up arrow (dark triangle) on the side. Thanks.Farming
D
6

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.

Despondent answered 19/10, 2013 at 13:1 Comment(1)
Thank you both. I prefer having more control, so I have implemented an solution like Melc suggested. On my onStepChanging event I call an function to make an Ajax call and sends the current index/title to the backend. The backends responds with the corresponding content. In the mean time, the next (empty) step is shown to the user with an loader animation. 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?Karyokinesis
D
0

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.

Ditty answered 31/5, 2015 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.