Search code examples
jqueryjquery-uijquery-ui-dialog

Add Additional Buttons


I have a scenario where i have a standard set of buttons to be available within the jQuery Dialog box but based on other conditions, I need to add additional buttons.

Good news is that I wont need to Add/Remove buttons, just Add. These buttons will effectively trigger a PostBack, forcing a refresh of the page content.

$(divSearch).dialog('option', 'buttons',
    {   // @buttons
        "Add": function (evt) {
            $(btnAdd).click();

            // Close
            $(this).dialog("close");
        },
        "Cancel": function (evt) {
            $(this).dialog("close");
        }
    }
);

$(divSearch).dialog('option', 'buttons',
    {   // @buttons
        "Search": function (evt) {
            $(btnSubmit).click();

            // Close
            $(this).dialog("close");
        },
        "Cancel": function (evt) {
            $(this).dialog("close");
        }
    }
);

Question

How do I have a common Dialog Init and then come back later and add the unique set of buttons for the scenarios?


Solution

  • Solution

    Ref: https://stackoverflow.com/a/19175417/659246

    // Setup standard settings for Dialog
    InitDialog(divSearch, 315
        , { // @buttons
            "Cancel": function (evt) {
                $(this).dialog("close");
            }
        }
        , function (evt) {  // @open
            $(this).parent().appendTo("form");
        });
    
    // In a Function...Far, Far, Away from the Document.Ready
    var btns = $(divSearch).dialog('option', 'buttons');
    $(divSearch).dialog('option', 'buttons'
        , $.extend({}, {
            "Search": function (evt) {
                $(btnSubmit).click();
    
                // Close
                $(this).dialog("close");
            }
        }, btns)
    );
    

    I used $.extend to get my desired result.

    Explanation

    capture the existing Buttons:

    var btns = $(divSearch).dialog('option', 'buttons');
    

    Pending on your need, I need to add a button in front of the standard set:

    $(divSearch).dialog('option', 'buttons'
        , $.extend({}, {
            "Search": function (evt) {
                $(btnSubmit).click();
    
                // Close
                $(this).dialog("close");
            }
        }, btns)
    );
    

    If you don't care on the order, then simply do this:

    $(divSearch).dialog('option', 'buttons'
        , $.extend(btns, {
            "Search": function (evt) {
                $(btnSubmit).click();
    
                // Close
                $(this).dialog("close");
            }
        })
    );
    

    If you need to Add, Subtract, or Change the button(s) object, then you can do it through the btns variable.