0

I am having a problem showing a jquery dialog on the return of an ajax call. Below is the dialog and supporting html:

function ShowDeleteReturnStatusDialog(deletedId) {
    $("#idDeleteReturnStatusDialog").dialog({
        title: 'Purchase Order ID ' + deletedId + ' was deleted.',
        resizable: true,
        height: 250,
        width: 350,
        modal: true,
        autoOpen: true,
        buttons: {
            'Ok': function () {

                $(this).dialog('close');
            }
        }
    });
}

<div id="idDeleteReturnStatusDialog" [email protected] style="display:none;">
    <div class="container" style="border:groove;">
        <div class="row">
            <div class="col-6">
                @Model.StatusDescription
            </div>
        </div>
    </div>
</div>

I am able to test the dialog using the following button and jquery to open the dialog from the button:

<div id="idcontainerTopBtns" style="border:outset; background-color:#e6f7fe;">
    <input type="button" id="idTestBtn" value="Test" class="btn btn-primary" />

        $("#idcontainerTopBtns").on('click', '#idTestBtn', function () {
            ShowDeleteReturnStatusDialog(500);
        });
</div>

Although the dialog opens with a button click, it does not open when I make an ajax call and try to show it when ajax is done. Below is the ajax call which is defined to open the dialog on return from the ajax call. The problem is the dialog does not show up on the screen:

function deletePurchaseOrder() {
    var purchaseOrderId = getPurchaseOrderId();
    event.preventDefault();
    var sToken = document.getElementsByName("__RequestVerificationToken")[0].value;
    $.ajax({
        url: '/PurchaseOrder/PurchaseOrderDelete',
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        data: {
            '__RequestVerificationToken': sToken,
            'id': parseInt($(this).attr("title")),
            'purchaseOrderId': purchaseOrderId
        }
    })
        .done(function (deletedId) {
            window.location.replace("/PurchaseOrder/Create");
            ShowDeleteReturnStatusDialog(deletedId);
        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            //Process Failure here
        });
};

How can this be fixed so the dialog is shown when the ajax call is done? Thanks in advance.

4
  • window.location.replace("/PurchaseOrder/Create"); did you just redirect to a different page before showing the dialog?
    – fdomn-m
    Commented Apr 29, 2020 at 14:41
  • Yes. The application shows the record to be deleted. When a user clicks delete, the screen content is rest to a blank screen which comes from a different page. So delete is from a view for delete and the blank for create screen is on a create view. So after delete, the view from the delete page is switched to the view for a new form after the delete.
    – Robertcode
    Commented Apr 29, 2020 at 14:48
  • 1
    Well, there's your problem. Do you not realise that if you navigate away from the page (even to the same url) then the old page no longer exists? (and so the js running on that page no longer runs).
    – fdomn-m
    Commented Apr 29, 2020 at 14:49
  • ok, I see what you are saying. I thought that because the html for the dialog box was on the create page view and the jquery was in an external file not the edit view page that it would have what it needed when the create view page was loaded. I wanted the dialog to show on the create view page which is why I loaded the create view before making the dialog call. I will change and test if the dialog can show while the edit view page is showing then clicking the ok button to then load the create view page. I will post results
    – Robertcode
    Commented Apr 29, 2020 at 14:56

2 Answers 2

0

Based on the comment from Hadi, I have made changes to the code which has fixed the problem. Below are the changes:

The below ajax was modified by removing the window.location.replace function call from the .done code block.

function deletePurchaseOrder() {
    var purchaseOrderId = getPurchaseOrderId();
    event.preventDefault();
    var sToken = document.getElementsByName("__RequestVerificationToken")[0].value;
    $.ajax({
        url: '/PurchaseOrder/PurchaseOrderDelete',
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        data: {
            '__RequestVerificationToken': sToken,
            'id': parseInt($(this).attr("title")),
            'purchaseOrderId': purchaseOrderId
        }
    })
        .done(function (deletedId) {
            ShowDeleteReturnStatusDialog(deletedId);
        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            //Process Failure here
        });
};

The ShowDeleteReturnStatusDialog function was modified by adding the window.location.replace call into the OK button area. It was originally in the ajax .done area:

function ShowDeleteReturnStatusDialog(deletedId) {
    $("#idDeleteReturnStatusDialog").dialog({
        title: 'Purchase Order ID ' + deletedId + ' was deleted.',
        resizable: true,
        height: 250,
        width: 350,
        modal: true,
        autoOpen: true,
        buttons: {
            'Ok': function () {
                window.location.replace("/PurchaseOrder/Create");
                $(this).dialog('close');
            }
        }
    });
}

The support html (shown below) for the idDeleteReturnStatusDialog was moved from the create view page to the edit view page which is where the entire JQuery invocation was started for the delete process:

<div id="idDeleteReturnStatusDialog" title="Purchase Order Deleted Notice" style="display:none;">
    <div class="container" style="border:groove;">
        <div class="row">
            <div class="col">
                The purchase order has been deleted.
            </div>
        </div>
    </div>
</div>
0

Consider the following code.

$(function() {
  function redirect(u) {
    if (u == undefined) {
      return false;
    }
    window.location.replace(u);
  }

  function createConfirm(t, c, cb) {
    return $("<div>").html(c).dialog({
      resizable: true,
      height: 250,
      width: 350,
      modal: true,
      autoOpen: true,
      title: t,
      buttons: {
        "Ok": cb
      },
      close: function() {
        $(this).remove();
      }
    });
  }

  $("#idcontainerTopBtns").on('click', '#idTestBtn', function() {
    createConfirm("Delete Purchase Order", "Purchase Order 500 has been deleted.", function() {
      console.log("Ok Clicked");
      $(this).dialog("close");
    });
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="idcontainerTopBtns" style="border:outset; background-color:#e6f7fe;">
  <input type="button" id="idTestBtn" value="Test" class="btn btn-primary" />
</div>

This is a little more dynamic. This also allows you to cascade events by using a Callback function inside the function.

An Example:

  $("#idcontainerTopBtns").on('click', '#idTestBtn', function() {
    createConfirm("Delete Purchase Order", "Purchase Order 500 has been deleted.", function() {
      redirect("/PurchaseOrder/Create");
    });
  });

Not the answer you're looking for? Browse other questions tagged or ask your own question.