1

I really need some help here. So I am using one Jquery Steps PlugIn. Now in this PlugIn when we go to the last tab it has a button name as "Finish" when clicked calls "onFinishing" Method.

onFinishing: function () {
            var loadedPrefix = MyNamespace.loadedPrefix;
            var inputBoxPrefix = $('#SitePrefix').val();
            $.ajax({
                type: 'POST',
                url: '/Settings/IsPrefixExists',
                data: { newPrefix: inputBoxPrefix, prefixLoadedFromDB: loadedPrefix },
                success: function (data) {
                      // do some stuff here
                    }
                },
                error: function () {
                    DisplayError('Failed to load the data.');
                }
            }); 
        }

Now above works perfectly fine. But my manager wants me to have two button there. One as "Save" and another as "Submit". And clicking on each of them will perform a different action. But this plugin has only one "Finish" button. And it is getting generated via PlugIn's Javascript code. Can I somehow use JQuery/Javascript to have one more button there, and write some code against it.

JS FIDDLE SAMPLE: JS FIDDLE SAMPLE

Image 1: enter image description here

I want something like below Image 2: enter image description here

SAMPLE Example: JS FIDDLE

Unbreakable
  • 7,776
  • 24
  • 90
  • 171

2 Answers2

6

Using the onStepChanged event found in the Steps plugin documentation...

You could do this:

$( window ).load(function() {
  $("#example-basic").steps({
    headerTag: "h3",
    bodyTag: "section",
    transitionEffect: "slideLeft",
    autoFocus: true,
    onFinishing: function () {
      alert("Hello");
    },
    onStepChanged:function (event, currentIndex, newIndex) {
      console.log("Step changed to: " + currentIndex);

      if(currentIndex == 2){
        console.log("ok");
        var saveA = $("<a>").attr("href","#").addClass("saveBtn").text("Save");
        var saveBtn = $("<li>").attr("aria-disabled",false).append(saveA);

        $(document).find(".actions ul").prepend(saveBtn)
      }else{
        $(".actions").find(".saveBtn").remove();
      }
      return true;
    },

  });


  // on Save button click
  $(".actions").on("click",".saveBtn",function(){
    alert("Saving!");
  });
});

Updated JSFiddle

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • Thank you so so much. Can you kindly tell me the use of timeout here. Your code works fine but kind of complex for beginner like me. So, you added a anchor tag and then you added that anchor tag to the Button and then added it to the UL Element. Till here I understood. What is the significance of timeout? – Unbreakable Jul 05 '17 at 02:09
  • Also, the check for currentIndex ==1, I could not understand. – Unbreakable Jul 05 '17 at 02:11
  • No use at all... I didn't know if the `.actions` was dynamic... So I've done it by default. – Louys Patrice Bessette Jul 05 '17 at 02:12
  • I understood the currentIndex ==1 part. Nevermind. When we are at index 1 and switching to index == 2 then we need to have the button ready. Thank you Sire! – Unbreakable Jul 05 '17 at 02:14
  • The index is returned on change... And that is the **current**. So 1 is the second since this index is zero-based. – Louys Patrice Bessette Jul 05 '17 at 02:14
  • Got it. Thanks a ton! – Unbreakable Jul 05 '17 at 02:15
  • You could compare to the `section` count minus 2 to get the one before the last... And stop caring about it even if the steps amount changes.. ;) try `var sectionAmount = $("section").length;` ;) And say "Hi!" to your manager for me. – Louys Patrice Bessette Jul 05 '17 at 02:16
  • Hey!! I changed the triggering event for `onStepChanged`... Because it was making the "save" button appear on step 1 when clicking "Previous"... So now `currentIndex` is the index after the change event. – Louys Patrice Bessette Jul 05 '17 at 02:42
  • I will try to incorporate it in my project. If I get any doubt I will ask you. Thanks again. :) – Unbreakable Jul 05 '17 at 13:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148440/discussion-between-unbreakable-and-louys-patrice-bessette). – Unbreakable Jul 05 '17 at 18:54
0

Regardless of using the steps plugin, you can add a save button on the last step and bind a click handler like you would with any normal button:

<input id='btnSave' name='btnSave' value='Save' type='button'>

and in your javascript:

$('#btnSave').click(function() {
    $.ajax({
            type: 'POST',
            url: '/Settings/SavePrefix',
            data: { newPrefix: inputBoxPrefix, prefixLoadedFromDB: loadedPrefix },
            success: function (data) {
                  // do some stuff here
                }
            },
            error: function () {
                DisplayError('Failed to save the data.');
            }
        }); 
});

You can also inject the button as needed.

Using the default plugin inside of a tag with and id of #steps-uid-8 you can get the finish element with $('#steps-uid-8 a').last(); It is nested inside of a <li> which is inside of a <ul> so you can get a reference to the ul $('#steps-uid-8 a').last(),parent().parent()

Using that reference you can add a hyperlink so your "button" has the same style as the plugin, like this:

$('#steps-uid-8 a').last().parent().parent().prepend("<li><a id='btnSave' name='btnSave'>Save</a></li>");

You can also add a function in your settings to only show the Save button on the last step if you need to

var numberOfSteps=3,// change as needed

var settings = {
    onStepChanged: function (event, currentIndex, priorIndex) 
    { 
    if(currentIndex==numberOfSteps) $('#btnSave').show();
    else $('#btnSave').hide();
    }
}

Using your fiddle, the onload Javascript was simply changed.

<script>
      function saveMe() {
        alert('Saving');
      }
      $(window).load(function() {
        var lastStep = 2;
        $("#example-basic").steps({
          headerTag: "h3",
          bodyTag: "section",
          transitionEffect: "slideLeft",
          onStepChanged: function(event, currentIndex, priorIndex) {
            //console.log(currentIndex);
            if (currentIndex == lastStep) $('#btnSave').show();
            else $('#btnSave').hide();
          },
          autoFocus: true,
          onFinishing: function() {
            alert("Hello");
          },
        }).find('a')
        .last().parent().parent()
        .prepend("<li><a style='display:none' href='#save' id='btnSave' name='btnSave' onclick='saveMe()'>Save</a></li>");
      });
    </script>

Here is a working copy: https://jsfiddle.net/rwh64ja8/

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • I will try and get back to you. I would have given a jSFIddle, but I am not sure how to add this plugin in JSFIddle. – Unbreakable Jul 05 '17 at 00:27
  • where do I need to place this code. This plug in autmatically creates the "Finish" button using its javascript code. Where shall I place the code you gave? Please guide me. – Unbreakable Jul 05 '17 at 00:31
  • where this line will go `` – Unbreakable Jul 05 '17 at 00:32
  • thank you so much. I will try this and will get back to you. Also, I wil try to add the code in the StackOverflow fiddle so that I can ask my doubts properly. – Unbreakable Jul 05 '17 at 00:52
  • Hello Sir, I have created the Jsfiddle. Can you kindly guide me here. https://jsfiddle.net/unbreakable/o2gxgz9r/9543/ – Unbreakable Jul 05 '17 at 01:32
  • I did put my effort by adding a div element with that id and using the code snippet you have provided, but it did not have any effect. I could not see the button. – Unbreakable Jul 05 '17 at 01:47