Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I dynamically add buttons to a Jquery UI Dialog box?

Tags:

jquery-ui

I am trying to add a new button to a jquery UI Dialog box based upon some input.

My code looks like this:

function editTour(ID, myDate) {
$.post("/Admin/EditTour", { TourID: ID },
        function (data) {
            $('#EditTour').html(data);
            $('#EditTour').dialog({
                modal: true,
                width: 400,
                resizable: false,
                title: formatDate(myDate),
                buttons: {
                    "Save": function () {
                      //some junk logic removed
                     },
                    "Cancel": function () {
                        $(this).dialog("close");
                    }
                }
            });

        });   //end post
}

What I want to do in this function is add a "Delete" button if the ID passed in is 0.

I know I can just create a cut and paste copy of the editTour function to manually add in the "Delete" button... but I was hoping for something cleaner.

like image 224
John Stone Avatar asked May 16 '11 17:05

John Stone


People also ask

What is jquery ui dialog?

The jQuery UI dialog method is used to create a basic dialog window which is positioned into the viewport and protected from page content. It has a title bar and a content area, and can be moved, resized and closed with the 'x' icon by default. Syntax: You can use the dialog ()method in two forms: $(selector, context).


1 Answers

Try this, it may help you.

function editTour(ID, myDate) {
    $.post("/Admin/EditTour", { TourID: ID },
        function (data) {
            $('#EditTour').html(data);
            $('#EditTour').dialog({
                modal: true,
                width: 400,
                resizable: false,
                title: formatDate(myDate)
            });

            var myButtons = {
                "Save": function () {
                    //some junk logic removed
                },
                "Cancel": function () {
                    $(this).dialog("close");
                }
            };

            if(ID == 0) {
                myButtons["Delete"] = function() {
                    // Delete logic here.
                }
            }

            $('#EditTour').dialog('option', 'buttons', myButtons);
        }
    );   //end post
}
like image 136
TaeV Avatar answered Sep 20 '22 03:09

TaeV