Search code examples
jqueryimpromptuimpromptu-interface

How to add and remove button dynamically from particular state in impromptu?


I am using impromptu for prompt and I want to add buttons dynamically on specific condition.

state0: {
    html: 'Do you want to apply changes?',
    buttons: { YES: 1, NO: 0 },
    focus: 1,
    submit: function(e, v, m, f) {
        if (v == 1) {
            e.preventDefault();
            $.prompt.goToState('state1');
            return false;
        }
        $.prompt.close();
    }
},

In this state I need 3 buttons for particular condition:
"NEWCONFIG", "YES", "NO"; and for another condition I need 2 buttons: "YES", "NO"


Solution

  • Define a button variable before initializing your impromptu as below

    var buttons={};
    if(condition1)
    {
       buttons = { YES: 1, NO: 0 };
    }
    else
    {
       buttons ={ NEWCONFIG:1, YES:1, NO:0};
    }
    

    then initialize impromptu

    state0: {
        html: 'Do you want to apply changes?',
        buttons: buttons,
        focus: 1,
        submit: function(e, v, m, f) {
            if (v == 1) {
                e.preventDefault();
                $.prompt.goToState('state1');
                return false;
            }
            $.prompt.close();
        }
    },
    

    That's it.. Let me know if any problem!!