如何放置警报,如果行被拖放并掉落并未单击更新按钮?

发布于 2025-02-13 14:46:57 字数 1033 浏览 0 评论 0原文

我有一个表,其行是使用可排序属性拖动的,如下所述。

<script type="text/javascript">
    $(function () {
        $("#tblLookup1 tbody").sortable({
            items: 'tr',
            cursor: 'pointer',
            axis: 'y',
            dropOnEmpty: false,
            start: function (e, ui) {
                ui.item.addClass("selected");
                
            },

            stop: function (e, ui) {
                ui.item.removeClass("selected");

            },
            receive: function (e, ui) {
                $(this).find("tbody").append(ui.item);
            }
        });
    });

    
</script>

现在,一旦拖动行,我创建了一个更新按钮,该按钮用于将新订单更新为数据库。

<input type="submit" class="btn CreateButtonColor" id="btnSave" value="Update"/>

现在,我希望有一个警报,如果用户拖动并丢弃行和忘记以单击更新,则如何发出警告,以便用户在不保存的情况下不能导航到其他页面?

警告像下面的那样,

if (confirm('Do you want to save the data before leaving?')) {
  // Update code
}
else
{
// do nothing
}

我应该在何处放置上述代码块?

I have a table whose rows are made draggable using sortable property as mentioned below.

<script type="text/javascript">
    $(function () {
        $("#tblLookup1 tbody").sortable({
            items: 'tr',
            cursor: 'pointer',
            axis: 'y',
            dropOnEmpty: false,
            start: function (e, ui) {
                ui.item.addClass("selected");
                
            },

            stop: function (e, ui) {
                ui.item.removeClass("selected");

            },
            receive: function (e, ui) {
                $(this).find("tbody").append(ui.item);
            }
        });
    });

    
</script>

Now, once rows are dragged and dropped I have created an update button which is used to update the new order to database.

<input type="submit" class="btn CreateButtonColor" id="btnSave" value="Update"/>

Now I wish to have an alert, if the user drags and drops the rows and forgots to click on update, how to put warning so that user cannot navigate to other pages without saving?

warning something like below

if (confirm('Do you want to save the data before leaving?')) {
  // Update code
}
else
{
// do nothing
}

where shall I place the above block of code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2025-02-20 14:46:57

要执行您需要的事情,请在掉落东西时在桌子上设置标志。当用户尝试离开页面时,您可以检查是否设置了该标志并显示警告:

$(function() {
  // set the flag on drop
  let $tbody = $("#tblLookup1 tbody").sortable({
    // ... settings
    receive: function(e, ui) {
      $tbody.data('is-dirty', true).find("tbody").append(ui.item);
    }
  });

  // remove the flag when submitting the form through the update button
  $('#yourForm').on('submit', () => $tbody.data('is-dirty', false));

  // check the flag when the user leaves the page and display 
  // a message if it's set
  window.addEventListener('beforeunload', () => {
    if ($tbody.data('is-dirty')) {
      return 'Do you want to save the data before leaving?';
    }
  });
});

To do what you require, set a flag on the table when something is dropped. When the user attempts to leave the page you can then check to see if that flag is set and display the warning:

$(function() {
  // set the flag on drop
  let $tbody = $("#tblLookup1 tbody").sortable({
    // ... settings
    receive: function(e, ui) {
      $tbody.data('is-dirty', true).find("tbody").append(ui.item);
    }
  });

  // remove the flag when submitting the form through the update button
  $('#yourForm').on('submit', () => $tbody.data('is-dirty', false));

  // check the flag when the user leaves the page and display 
  // a message if it's set
  window.addEventListener('beforeunload', () => {
    if ($tbody.data('is-dirty')) {
      return 'Do you want to save the data before leaving?';
    }
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文