add custom method to jquery ui dialog

倾然丶 夕夏残阳落幕 提交于 2019-12-22 09:38:49

问题


How can I do something like:

$("#some_div").dialog("doSomething");

And what that method should do is to add an extra icon in the titlebar

EDIT 1: I've tried this solution: the method gets called but I can't access the dialog object (maybe I'm doing something wrong)


回答1:


First, if you're adding an icon to the title bar I would suggest applying a class to that dialog box and styling it with CSS. Example:

$( "#some_div" ).dialog({ dialogClass: "someClass" });

If you'd still like to add a custom method, here's what the documentation says:

Supply a callback function to handle the create event as an init option.

$( ".selector" ).dialog({
   create: function(event, ui) { ... }
});

Bind to the create event by type: dialogcreate.

$( ".selector" ).bind( "dialogcreate", function(event, ui) { 
    ... 
});



回答2:


Ok, this is what I did:

//in a separate js file
$.ui.dialog.prototype.showExtraButton = function()
{
    this.uiDialogTitlebar.append("<a role='button' class='ui-dialog-titlebar-icon ui-dialog-titlebar-icon-extra'><span class='ui-icon'></span></a>");
}

//call it this way
$("#some_div").dialog("showExtraButton");

//css
.ui-dialog-titlebar-icon {
  position: absolute;
  right: 25px;
}

.ui-dialog-titlebar-icon-extra span
{
    display: block;
    background-image: url(../path_to_img/button_extra.png)!important;
}

This solution by Langdon, along with this one by Kevin B gave me the answer on how to resolve my problem

UPDATE 2014-01-03

TIL about $.widget(), so here is another implementation of the same thing

$.widget("ui.dialog", $.ui.dialog,
{
    showExtraButton: function()
    {
        this.uiDialogTitlebar.append("<a role='button' class='ui-dialog-titlebar-icon ui-dialog-titlebar-icon-extra'><span class='ui-icon'></span></a>");
    }
});


来源:https://stackoverflow.com/questions/12679232/add-custom-method-to-jquery-ui-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!