16

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.

1 Answer 1

45

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.