简体   繁体   English

Django/JqueryUI 对话框 - 如何将 Django 与 JqueryUI 对话框集成?

[英]Django/JqueryUI dialog box - How can I integreate Django with JqueryUI dialog box?

I want to add a feature on my Django project using jQueryUI dialog box where when you click on a link (like a "delete" link) a jQueryUI dialog box will pop up asking you if you really want to delete that item.我想使用jQueryUI 对话框在我的 Django 项目中添加一个功能,当您单击链接(如“删除”链接)时,会弹出一个 jQueryUI 对话框,询问您是否真的要删除该项目。 Then if you click on the delete button (found the jQuery dialog box) a Django function will do the delete job.然后,如果您单击删除按钮(找到 jQuery 对话框),则 Django function 将执行删除工作。

So how do I make the delete button (found the jQuery dialog box) send a post message (with respective variables) to a Django function found in my views.py that will do the delete job?那么如何使删除按钮(找到 jQuery 对话框)向 Django function 发送帖子消息(带有相应的变量)。

Real examples would be truly appreciated!真实的例子将不胜感激!

Say you have something like this in your template:假设您的模板中有这样的内容:

<div id="dialog" title="Confirm delete">Are you sure?</div>
{% for object in object_list %}
# display whatever you like here
<a id="{{ object.id }}" class="delete" href="#">Delete</a>
{% endfor %}

Then something like this (in your $(document).ready ) would work -- notice how we set the callback function that the dialog calls when the delete button is pressed (using the dialog's option method ) in the click handler:然后像这样的东西(在你的$(document).ready中)会起作用——注意我们如何设置回调 function 在click处理程序中按下删除按钮时对话框调用(使用对话框的选项方法):

$("#dialog").dialog({
    modal: true,
    autoOpen: false
});
$("a.delete").click(function(e) {
    e.preventDefault();
    var id = $(this).attr('id');
    $("#dialog").dialog('option', 'buttons', {
        "Delete": function() {
            $.post({
                url: {% url myapp.views.delete %},
                data: {'id': id},
                success: function() {
                   # whatever you like, some fancy effect that removes the object
                }
            });
            $(this).dialog("close");
        },
        "Cancel": function() {
            $(this).dialog("close");
        }
    });
    $("#dialog").dialog("open");
    return false;
});

You should also consider Aprise .您还应该考虑Aprise It is lovely, uses jQuery, is easy to use, and is very small (3k).很可爱,用的是jQuery,好用,而且很小(3k)。

apprise('Hello now?', {'verify':true});

对话框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM