简体   繁体   English

jQuery UI对话框,添加按钮旁边的元素

[英]jQuery UI Dialog, adding elements next to a button

One of the nice things about the jQuery UI Dialog is that it has an option for Buttons, which automatically positions them correctly. 关于jQuery UI Dialog的一个好处是它有一个Buttons选项,可以自动正确定位它们。 I just wonder: Can I somehow place elements next to the buttons? 我只是想知道:我可以以某种方式在按钮旁放置元素吗? I have a little Ajax-Loader gif that I would like to display in the lower left corner of the dialog, while the buttons stay at the lower right? 我有一个Ajax-Loader gif,我想在对话框的左下角显示,而按钮保持在右下方?

I know I can just remove the buttons and create them manually in HTML, but as jQuery takes care of positioning and styling already for me, I'd like to keep that functionality if it makes sense. 我知道我可以删除按钮并在HTML中手动创建它们,但由于jQuery已经为我提供了定位和样式,我想保留该功能,如果它有意义的话。

    $("#newProjectDialog").dialog({
        bgiframe: true,
        resizable: false,
        width: 400,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        },
        buttons: {
            'Create': function() {
                $("#ajax-loader").show();
                // Make the Ajax Call and whatever else is needed
                $(this).dialog('destroy');
            },
            Cancel: function() {
                $(this).dialog('destroy');
            }
        }
    });

All you basically need to do is 你基本上需要做的就是

//depending on what #ajax-loader is you maybe need to style it (float:left, ...)
$("#ajax-loader").clone(true).appendTo("div.ui-dialog-buttonpane").show();

Below a fancier version with a few considerations incorporated. 下面是一个有一些考虑因素的发烧友版本。


I imagine #ajax-loader to look similar to this 我想#ajax-loader看起来与此类似

<div id='ajax-loader'><img src='loader.gif' /><span>loading...</span></div>

or just this 或者就是这个

<img id='ajax-loader' src='loader.gif' />

javascript can look like this javascript可能看起来像这样

...
'Create': function() {
    var btnpane = $("div.ui-dialog-buttonpane");
    //prevent bad things if create is clicked multiple times
    var there = btnpane.find("#ajax-loader").size() > 0;
    if(!there) {
        $("#ajax-loader").clone(true).appendTo(btnpane).show();
        // Make the Ajax Call and whatever else is needed
        // if ajax call fails maybe add $("#ajax-loader", btnpane).remove();
        $(this).dialog('destroy');
    }
},
...

A note 一张纸条

  • You should call .dialog('destroy') in the complete event of the ajax request else the dialog may get destroyed before the ajax request finished and the user may not even see the "loader". 你应该在ajax请求的complete事件中调用.dialog('destroy') ,否则在ajax请求完成之前对话框可能会被销毁,用户甚至可能看不到“loader”。

How about just inserting your spinner before the first ui-dialog-button? 如何在第一个ui-dialog-button之前插入微调器?

buttons: {
   'Create' : function() {
       $('<img src="spinner.gif" style="float: left;" />').insertBefore('.ui-dialog-buttonpane > button:first');
       ...ajax stuff...
       $(this).dialog('destroy');
    }
}

The best way to do this, is to create another button, make it totally transparent with no border, and add the animated gif as its background image. 最好的方法是创建另一个按钮,使其完全透明,没有边框,并添加动画gif作为其背景图像。 By using another button, you can easily locate its position relative to all your other buttons. 通过使用另一个按钮,您可以轻松找到相对于所有其他按钮的位置。

First, to be able to style buttons more, you need to create them with one level higher of definition. 首先,为了能够更多地设置按钮样式,您需要创建一个更高级别的定义。 So instead of: 所以代替:

buttons: {
    'Create': function() {
        $("#ajax-loader").show();
        // Make the Ajax Call and whatever else is needed
        $(this).dialog('destroy');
    },
    Cancel: function() {
        $(this).dialog('destroy');
    }
}

Do it like this (notice square brackets and one more level of indent): 这样做(注意方括号和一个缩进级别):

buttons: [
    {
        id: 'create-button',
        class: 'create-button-class',
        text: 'Create',
        click: function() {
            $("#ajax-loader").show();
            // Make the Ajax Call and whatever else is needed
            $(this).dialog('destroy');
        }
    },
        text: 'Cancel',
        click: function() {
            $(this).dialog('destroy');
        }
    }
]

You can assign an id and class to each button or not. 您可以为每个按钮分配ID和类。 If you assign either id and/or class, then you can apply CSS styling to it. 如果您指定了id和/或类,则可以对其应用CSS样式。

<style>
.create-button-class{
    height:50px;
    width:50px;
    left:-300px; /* Pushes it left, change value for desired location. */
}
.ui-dialog .ui-dialog-buttonpane #create-button { 
    color: transparent; /* no inner color and also hides text */
    border: none; /* removes border */
    background-image:url(images/spinner-gif-25px.gif); /*replaces default image */
    background-size: 50px;
    background-repeat: no-repeat;
}
</style>

If you like, create a normal additional button and use CSS property left to push it as far left in the button panel as you like, before making it transparent and no border. 如果你愿意,可以创建一个普通的附加按钮,并使用左边的CSS属性将其推到按钮面板的最左边,然后再使其透明且没有边框。

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

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