Introduction
Using Esc key to close the kendo window is a default functionality of it, but in some scenario if you have editable kendo grid in kendo window then there will be conflict while the user uses the Esc key, because the same Esc key will close the kendo grid cell if it is in editable mode, but by default it will close the entire window, now we need to prevent this default functionality of kendo window.
Prevent Esc key functionality to close the kendo window
The below function block is used to prevent the Esc key functionality to close the kendo window.
- $(function () {
- kendo.ui.Window.fn._keydown = function (originalFn) {
- var KEY_ESC = 27;
- return function (e) {
- if (e.which !== KEY_ESC) {
- originalFn.call(this, e);
- }
- };
- }(kendo.ui.Window.fn._keydown);
- });
KendoWindow.html
- <!DOCTYPE html>
- <html>
- <head>
- <base href="https://demos.telerik.com/kendo-ui/window/index">
- <style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
- <title></title>
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.620/styles/kendo.common-material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.620/styles/kendo.material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.2.620/styles/kendo.material.mobile.min.css" />
-
- <script src="https://kendo.cdn.telerik.com/2018.2.620/js/jquery.min.js"></script>
- <script src="https://kendo.cdn.telerik.com/2018.2.620/js/kendo.all.min.js"></script>
-
-
- </head>
- <body>
-
- <div id="example">
- <div id="window">
- <div id="grid"></div>
-
- </div>
- <button id="undo" style="display:none" class="k-button hide-on-narrow">Click here to open the window.</button>
-
-
-
- <script>
- $(function () {
- kendo.ui.Window.fn._keydown = function (originalFn) {
- var KEY_ESC = 27;
- return function (e) {
- if (e.which !== KEY_ESC) {
- originalFn.call(this, e);
- }
- };
- }(kendo.ui.Window.fn._keydown);
- });
- $(document).ready(function() {
-
- $("#grid").kendoGrid({
- dataSource: {
- type: "odata",
- transport: {
- read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers"
- },
- pageSize: 20
- },
- height: 550,
- groupable: true,
- sortable: true,
- editable: true,
- pageable: {
- refresh: true,
- pageSizes: true,
- buttonCount: 5
- },
- columns: [{
- template: "<div class='customer-photo'" +
- "style='background-image: url(../content/web/Customers/#:data.CustomerID#.jpg);'></div>" +
- "<div class='customer-name'>#: ContactName #</div>",
- field: "ContactName",
- title: "Contact Name",
- width: 240
- }, {
- field: "ContactTitle",
- title: "Contact Title"
- }, {
- field: "CompanyName",
- title: "Company Name"
- }, {
- field: "Country",
- width: 150
- }]
- });
-
- var myWindow = $("#window"),
- undo = $("#undo");
-
- undo.click(function() {
- myWindow.data("kendoWindow").open();
- undo.fadeOut();
- });
-
- function onClose() {
- undo.fadeIn();
- }
-
- myWindow.kendoWindow({
- width: "600px",
- title: "About Alvar Aalto",
- visible: false,
- actions: [
- "Pin",
- "Minimize",
- "Maximize",
- "Close"
- ],
- close: onClose
- }).data("kendoWindow").center().open();
- });
- </script>
-
- <style>
- .customer-photo {
- display: inline-block;
- width: 32px;
- height: 32px;
- border-radius: 50%;
- background-size: 32px 35px;
- background-position: center center;
- vertical-align: middle;
- line-height: 32px;
- box-shadow: inset 0 0 1px #999, inset 0 0 10px rgba(0,0,0,.2);
- margin-left: 5px;
- }
-
- .customer-name {
- display: inline-block;
- vertical-align: middle;
- line-height: 32px;
- padding-left: 3px;
- }
- #example {
- min-height:500px;
- }
- #undo {
- text-align: center;
- position: absolute;
- white-space: nowrap;
- padding: 1em;
- cursor: pointer;
- }
- .armchair {
- float: left;
- margin: 30px 30px 120px 30px;
- text-align: center;
- }
- .armchair img {
- display: block;
- margin-bottom: 10px;
- }
- .k-window-content a {
- color: #BBB;
- }
- .k-window-content p {
- margin-bottom: 1em;
- }
-
- @media screen and (max-width: 1023px) {
- div.k-window {
- display: none !important;
- }
- }
- </style>
- </div>
-
-
- </body>
- </html>
Result in Browser
Reference
https://demos.telerik.com/kendo-ui/window/index
Summary
We have seen how to prevent the default Esc key functionality to close the kendo window, which is useful when we are using the editable Kendo Grid.
I hope you have enjoyed this blog. Your valuable feedback, questions, or comments about this article are always welcomed