25

I'm using Kendo UI's window component, which is similar to any modal dialog.

I have a close button in it, how do I close the window upon clicking that button (instead of clicking the default 'x' button in the title bar)

The content in my window is loaded from another view

@(Html.Kendo().Window()
           .Name("window")
           .Title("Role")
           .Content("loading...")
           .LoadContentFrom("Create", "RolesPermissions", Model.Role)
           .Modal(true)
           .Width(550)           
           .Height(300)           
           .Draggable()
           .Visible(false)
          )

In that same view, I have

<span id="close" class="btn btn-inverse">Cancel</span>

This is what I have in my main view (the view calling the window)

$(document).ready(function () {
    var window = $("#window").data("kendoWindow");

    $("#open").click(function (e) {
        window.center();
        window.open();
    });

    $("#close").click(function(e) {
        window.close();
    });
});
tereško
  • 58,060
  • 25
  • 98
  • 150
Null Reference
  • 11,260
  • 40
  • 107
  • 184

4 Answers4

32

Basically you already know how to close the window - you need to do it with the close method of its API.

$("#window").data("kendoWindow").close();

But in order to attach the handler to the button inside of the view you need to wait until the content is loaded - you need to use the refresh event.

e.g.

$('#theWindowId').data().kendoWindow.bind('refresh',function(e){
    var win = this;
    $('#close').click(function(){
         win.close();
    })
})
Manuel Reis
  • 574
  • 8
  • 29
Petur Subev
  • 19,983
  • 3
  • 52
  • 68
  • Great, nice explanation, and it's exactly what I'm looking for! – Null Reference Nov 14 '12 at 14:52
  • 2
    Where does this code go? In the on ready function of the Main view? I tried that, but $('#theWindowId').data().kendoWindow is undefined ($('#theWindowId').data() is defined, but doesn't contain kendoWindow) – crichavin Apr 23 '13 at 16:05
  • 1
    You need to do it after the Window is initialized. Or you can specify the refresh handler initially when configuring the Window, like used here http://demos.kendoui.com/web/window/events.html – Petur Subev Apr 23 '13 at 16:14
1

In JavaScript - HTML window is an object that represents an open window in a browser. Try defining your window as something else.

$(document).ready(function () {
    var wnd = $("#window").data("kendoWindow");

    $("#open").click(function (e) {
        wnd.center();
        wnd.open();
    });

    $("#close").click(function(e) {
        wnd.close();
    });
});
OnaBai
  • 40,767
  • 6
  • 96
  • 125
0

there is an event in kendo ui for this it should be something like this

 $("#idofyourbutton").click(function () {
     $("#window").data("kendoWindow").close();
    });
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
0

In case of working with content loaded by ajax, the Petur Subev's answer is perfect! I´d like to give an example working with templates, that is more simple (whereas not all requests shoulb be by ajax). Consider the template below:

<script id="Template_Example1" type="text/kendo-tmpl">
<div class="i-contentWindow">
    <div>
        <a id="btn1" href="\#"><span class="k-icon k-i-cancel"></span>Button 1</a>
    </div>
</div>

And now, let´s load the template e call window close method:

function ExampleFn1(dataItem) {
    //create the template
    var template = kendo.template($("#Template_Example1").html());

    //create a kendo window to load content
    var kWindow = openKendoWindow({
        title: "Window Tests",
        iframe: false,
        resizable: false
    }).content(template(dataItem));

    // call window close from button inside template
    $("#btn1").click(function (e) {
        e.preventDefault();
        alert("btn1 on click!");
    });

    kWindow.open();
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423