简体   繁体   English

jQuery UI 对话框按钮定位

[英]jQuery UI Dialog button positioning

How can I position buttons from jQuery UI separately from each other.如何将 jQuery UI 中的按钮彼此分开放置。 Buttons are aligned in same direction.按钮在同一方向对齐。 I would like one button to be aligned to left while the other to the right.我希望一个按钮向左对齐,而另一个向右对齐。 Is it possible?是否有可能?

OK, looking at this through Firebug...好的,通过 Firebug 看这个...

The Dialog control create a div with a class called ui-dialog-buttonpane , so you can search for that. Dialog 控件使用名为ui-dialog-buttonpane的类创建一个 div,因此您可以搜索它。

If you are dealing with multiple buttons, you will probably want :first and :last attributes.如果您正在处理多个按钮,您可能需要:first:last属性。

So, $(".ui-dialog-buttonpane button:first) should get you the first button. and $(".ui-dialog-buttonpane button:last) should get you the last button.因此, $(".ui-dialog-buttonpane button:first)应该为您提供第一个按钮。而$(".ui-dialog-buttonpane button:last)应该为您提供最后一个按钮。

From there you can modify the css/style to put each button on the right and left (modify the float values).从那里您可以修改 css/style 将每个按钮放在右侧和左侧(修改浮点值)。

Anyway, that is how I would approach it right now.无论如何,这就是我现在的处理方式。

This is the way that I was able to accomplish the results.这是我能够完成结果的方式。

 $('#dialog-UserDetail').dialog({
            autoOpen: false,
            height: 318,
            width: 531,
            modal: true,
            resize: false,
            buttons: {
                DelUser:{ 
                    class: 'leftButton',
                    text: 'Delete User',
                    click : function (){
                        alert('delete here');
                    }
                },
                "Update User": function () {
                       alert('update here');
           },
                Close: function () {
                    $(this).dialog("close");
                }
            }
        });

Then in Styles add the !important flag, which is needed because the class comes first and is overwritten by jquery.然后在样式中添加 !important 标志,这是必需的,因为类首先出现并被 jquery 覆盖。

<style>
    .leftButton
    {
        margin-right: 170px !important;
    }
</style>

I wound up with the following solution (based on the answer by The Cook).我最终得到了以下解决方案(基于 The Cook 的回答)。 This has a number of advantages (for me) over the other answers:与其他答案相比,这有许多优点(对我而言):

  • Pure HTML/CSS - no javascript纯 HTML/CSS - 没有 javascript
  • No need to set a fixed width on the dialog (button will always align to the left edge, even if the user resizes the dialog)无需在对话框上设置固定宽度(按钮将始终与左边缘对齐,即使用户调整对话框大小)
  • No need for the !important flag in the CSS不需要 CSS 中的 !important 标志

The HTML I used (Slightly modified from the answer by The Cook):我使用的 HTML(根据 The Cook 的回答稍作修改):

$('#dialog-UserDetail').dialog({
        autoOpen: false,
        width: 'auto',
        modal: true,
        buttons: {
            DelUser:{ 
                class: 'leftButton',
                text: 'Delete User',
                click : function (){
                    alert('delete here');
                }
            },
            "Update User": function () {
                   alert('update here');
       },
            Close: function () {
                $(this).dialog("close");
            }
        }
    });

Then I used the following CSS:然后我使用了以下 CSS:

.leftButton
{
    position: absolute;
    left:8px;
}

With the result that the "leftButton" button is always fixed at 8px off the left edge of the dialog, while the other buttons are right justified.结果是“leftButton”按钮始终固定在距对话框左边缘 8px 的位置,而其他按钮则右对齐。 If you have more than one button you need left justified, then you would need to increase the left parameter by the width of the previous buttons plus whatever spacing you want for each button, which is less than elegant in my opinion.如果您有多个按钮需要左对齐,那么您需要通过前一个按钮的宽度加上每个按钮所需的任何间距来增加左参数,在我看来,这不太优雅。 However, for a single left-justified button this works like a charm.然而,对于单个左对齐按钮,这就像一个魅力。

You'd first have to change width of .ui-dialog-buttonset to 100% in jquery-ui.css您首先必须在 jquery-ui.css 中将 .ui-dialog-buttonset 的宽度更改为 100%

.ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset { width: 100%;}

Then in your modal declaration, for buttons, you could do something explicit like below:然后在您的模态声明中,对于按钮,您可以执行如下明确的操作:

buttons: [
        {
           text: "Close",
           open: function(){
                        $(this).css('float','left');}, 
                  }
        }
             ]

您还可以将类添加到您的对话框 ( http://docs.jquery.com/UI/Dialog#option-dialogClass ) 以本地化您的样式。

Ok,As @The Cook's answer,you can change this css in style tag.Think by another way.you can add style within button,no need css anymore.Good Luck.好的,正如@The Cook 的回答,您可以在样式标签中更改此 css。换一种方式思考。您可以在按钮内添加样式,不再需要 css。祝您好运。

                      "Ok":{                                
                            style:"margin-right:640px",                                
                            click: function () {
                                //Your Code 
                            }
                        },

I found the .ui-dialog-buttonset collapses down with no width anyway so to position two bottons in the bottom left and right corner added just css like this:我发现 .ui-dialog-buttonset 折叠起来没有宽度,所以要在左下角和右下角放置两个按钮,只添加了这样的 css:

.ui-dialog-buttonset {width:100%}
.ui-dialog-buttonset .ui-button:last-child {float:right !important;}

The first line make the button set container 100% wide, the second one pushes the last button found to the right.第一行使按钮设置容器 100% 宽,第二行将找到的最后一个按钮推到右侧。

.ui-dialog .ui-dialog-buttonpane .ui-dialog-buttonset {
  float: none !important;
  text-align:right;
}

.ui-button-left {
  float: left;
}

This does not impact the default buttons, they will appear to the right.这不会影响默认按钮,它们会出现在右侧。 Also, you can place as many buttons on the left as you want without special formatting for each button.此外,您可以根据需要在左侧放置任意数量的按钮,而无需为每个按钮设置特殊格式。 To set the class of the left buttons, there are many ways to do that.要设置左侧按钮的类,有很多方法可以做到这一点。

As part of the dialog definition:作为对话框定义的一部分:

$("#mydialog").dialog(
    {buttons: [
        {"class": "ui-button-left", "text": "Delete"},
        {"text": "Cancel"},
        {"text": "Save"}
    ]});

Or you can do something like this:或者你可以做这样的事情:

$("#mydialog").dialog(
    {buttons: [
        {"id": "myDeleteButton", "text": "Delete"},
        {"text": "Cancel"},
        {"text": "Save"}
    ]});

And later style the css for the 'myDeleteButton' object as floating left.然后将 'myDeleteButton' 对象的 css 样式设置为向左浮动。 Setting the button id can be helpful if you also want to show or hide the button without recreating the dialog.如果您还想在不重新创建对话框的情况下显示或隐藏按钮,则设置按钮 ID 会很有帮助。

Tested in Chrome, Safari, FF, IE11, jQuery UI 1.10在 Chrome、Safari、FF、IE11、jQuery UI 1.10 中测试

我遇到了同样的问题,但我找到了一个简单的解决方案,即每当我们定义 jquery ui 对话框表单时,我们都会更改按钮的顺序 Ok 或关闭表单对话框。

选择后,尝试:
.prependTo(".ui-dialog-buttonpane");

$('#dialog-UserDetail').dialog({
            autoOpen: false,
            height: 318,
            width: 531,
            modal: true,
            resize: false,
            buttons: {
                DelUser:{ 
                    text  : 'Delete User',
                    click : function (){
                        alert('delete here');
                    }
                },
                space: { 
                    text  : '',
                    id : 'space',
                    width : '(the distance you want!)',
                },
                "Update User": function () {
                       alert('update here');
           },
                Close: function () {
                    $(this).dialog("close");
                }
            }
        });
$("#space").visibility("hidden");

I use the following approach to left align only the first button.我使用以下方法仅左对齐第一个按钮。 This works for any dialog width:这适用于任何对话框宽度:

    open: function(event, ui) {
        var firstButton = $(this).parent().find('.ui-dialog-buttonpane button:first')
        var firstButtonPos = firstButton.position().left - 15;
        firstButton.css({ marginRight: firstButtonPos + 'px', color: 'red' });
    },

All I did was add the following to the CSS to center the buttons.我所做的只是将以下内容添加到 CSS 以将按钮居中。 The key here is setting the width to 100%, bc checking with firebug, the width was only size of the buttons.这里的关键是将宽度设置为 100%,bc 检查萤火虫,宽度只是按钮的大小。

.ui-dialog-buttonset {
    width: 100%; 
    text-align: center
}

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

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