简体   繁体   English

如何通过更改对话框选项来重用jQuery UI对话框

[英]How to re-use jQuery UI Dialog by changing dialog options

I know the correct way to manage JQuery.dialog is to initialize with: 我知道管理JQuery.dialog的正确方法是使用以下方法初始化:

$("#dialog").dialog({ autoOpen: false });

Then use the following to open and close it: 然后使用以下命令打开和关闭它:

$("#dialog").dialog("open");
$("#dialog").dialog("close");

But there are some cases when this model is not fully applicable. 但是在某些情况下,该模型不能完全适用。

For instance, I use a dialog to create new data and to edit existing data. 例如,我使用对话框创建新数据并编辑现有数据。 In the first case I have a cancel and a create button, but in the second case I have also a delete button. 在第一种情况下,我具有取消创建按钮,但是在第二种情况下,我也具有删除按钮。

I've seen that there is a destroy function in jquery.dialog. 我已经看到jquery.dialog中有一个destroy函数。 The question is: in these cases, should I destroy the dialog instead of close it and create a new one? 问题是:在这种情况下,我应该销毁该对话框而不是关闭它并创建一个新对话框吗? There is any better option? 还有更好的选择吗?

you can set different buttons as option before dialog open 您可以在对话框打开之前将其他按钮设置为选项

eg 例如

var  buttons = {
        "act_add": {
        "Insert": function() { ... },
        "Cancel": function() { ... }
        },
        "act_edit": {
        "Save": function() { ... },
        "Delete": function() { ... }
        }
      };

    $('.dialogOpenLink').click(function(){
      var $dlg = $('#dialog'),
      actType;

      //get an action name from data attribute of the clicked element
      actType = $(this).data('action'); //or get the action in way that best suits you

      $dlg.dialog( "option", "buttons", buttons[actType]);
      $dlg.dialog('open');
    });

jQuery UI dialog allows you to manipulate most properties after initialization. jQuery UI对话框允许您在初始化后操纵大多数属性。 You can change the buttons some time after the dialog is initialized; 可以在对话框初始化后的某个时间更改按钮; eg when the insert or update button is clicked. 例如,当单击插入或更新按钮时。

 // imported from http://jsfiddle.net/salman/VYAJw/9/ $(function() { $("#dialog1").dialog({ autoOpen: false, modal: true }); $("#button-insert").click(function() { $("#dialog1").dialog("option", "title", 'Insert Record'); $("#dialog1").dialog("option", "buttons", [{ text: "Insert", click: function() { alert("Record inserted"); $(this).dialog("close"); } }, { text: "Cancel", click: function() { $(this).dialog("close"); } }]); $("#dialog1").dialog("open"); }); $("#button-update").click(function() { $("#dialog1").dialog("option", "title", 'Update Record'); $("#dialog1").dialog("option", "buttons", [{ text: "Update", click: function() { alert("Record updated"); $(this).dialog("close"); } }, { text: "Delete", click: function() { alert("Record deleted"); $(this).dialog("close"); } }, { text: "Cancel", click: function() { $(this).dialog("close"); } }]); $("#dialog1").dialog("open"); }); }); 
 @import url("https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/blitzer/jquery-ui.min.css"); body { font: medium sans-serif; } #dialog1 label, #dialog1 input { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <div id="dialog1"> <p>Fill out this form.</p> <form> <fieldset> <label for="name">Name</label> <input type="text" name="name" id="name" /> <label for="email">Email</label> <input type="text" name="email" id="email" /> <label for="password">Password</label> <input type="password" name="password" id="password" /> </fieldset> </form> </div> <input type="button" id="button-insert" value="Insert" /> <input type="button" id="button-update" value="Update" /> 

An alternate method would be to add the buttons directly inside the form and .hide() them depending on whether you're showing insert or update dialog. 另一种方法是将按钮直接添加到表单中,然后根据要显示插入或更新对话框的.hide()按钮。

To safelly remove dialog all you need is to set close option like this: 为了安全地删除对话框,您需要设置关闭选项,如下所示:

close: function() {
 return $(this).remove();
} 

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

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