如何以编程方式在Dijit Dialog中制作和显示表单?

时间:2013-02-21 10:45:36

标签: forms dojo dialog

我一直试图弄清楚如何使用Dojo 1.7在Dialog中创建和显示表单。

我希望我的对话框看起来像这样:

enter image description here

我见过的所有样本都使用Markup,但没有使用AMD

1 个答案:

答案 0 :(得分:20)

创建对话框时,可以使用窗口小部件(例如表单)作为内容。例如,您可以这样做:

require([
    "dijit/Dialog", 
    "dijit/form/Form", 
    "dijit/form/TextBox", 
    "dijit/form/Button", 
    "dojo/domReady!" 
], function(Dialog, Form, TextBox, Button) 
{
    var form = new Form();

    new TextBox({
        placeHolder: "Name"
    }).placeAt(form.containerNode);

    new Button({
      label: "OK"
    }).placeAt(form.containerNode);

    var dia = new Dialog({
        content: form,
        title: "Dialog with form",
        style: "width: 300px; height: 300px;"
    });
    form.startup();
    dia.show();
});//~require

require()由Dojo提供。它加载依赖项(Form,Dialog等),然后运行创建小部件的给定函数。但是,因为我们在依赖项中包含domReady!,所以Dojo确保DOM已完全加载并首先就绪。

因为我在该功能中也有dia.show(),所以只要页面打开就会显示对话框。假设您想要在单击页面上的某个按钮时显示对话框:

require([
    "dijit/Dialog", 
    "dijit/form/Form", 
    "dijit/form/TextBox", 
    "dijit/form/Button",
    "dojo/on",      // Added this!
    "dojo/domReady!" 
], function(Dialog, Form, TextBox, Button, onEvent) 
{
    // ... as above, we create the dialog and form when the page loads
    // but it remains hidden until we call dia.show() ...
    form.startup();
    // dia.show();  Commented out this!

    onEvent(document.getElementById("someButtonOnYourPage"), "click", 
        function()
        {
            dia.show();
        });
});//~require