|
| 1 | +--- |
| 2 | +title: Prevent Grid PopUp editor from closing on update/create |
| 3 | +description: An example on how to keep the Grid popup editor open after update is finished |
| 4 | +type: how-to |
| 5 | +page_title: Keep Grid popup open | Kendo UI Grid |
| 6 | +slug: grid-prevent-popup-close-on-edit |
| 7 | +tags: grid, edit, popup, prevent, cancel, stop, close, edit, insert, create, modal, reopen, keep, open |
| 8 | +res_type: kb |
| 9 | +component: grid |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Progress Kendo UI Grid</td> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td>Progress Kendo UI version</td> |
| 21 | + <td>2017.3.1026</td> |
| 22 | + </tr> |
| 23 | +</table> |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +I want to keep popup editor open after I update or insert a record in the Grid. |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +To implement the feature you can handle the Grid [edit event](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid#events-edit) and attach handler for the PopUp window [close event](https://docs.telerik.com/kendo-ui/api/javascript/ui/window#events-close). |
| 32 | + |
| 33 | +In the close handler e.preventDefault() will be called to prevent the popup from closing. In order to allow the users to close the editor the flag can also be set when the cancel and close buttons are clicked. |
| 34 | + |
| 35 | +The example below illustrates the approach. |
| 36 | + |
| 37 | +```html |
| 38 | +<div id="grid"></div> |
| 39 | + |
| 40 | +<script> |
| 41 | + $(document).ready(function () { |
| 42 | + var crudServiceBaseUrl = "https://demos.telerik.com/kendo-ui/service", |
| 43 | + dataSource = new kendo.data.DataSource({ |
| 44 | + transport: { |
| 45 | + read: { |
| 46 | + url: crudServiceBaseUrl + "/Products", |
| 47 | + dataType: "jsonp" |
| 48 | + }, |
| 49 | + update: { |
| 50 | + url: crudServiceBaseUrl + "/Products/Update", |
| 51 | + dataType: "jsonp" |
| 52 | + }, |
| 53 | + destroy: { |
| 54 | + url: crudServiceBaseUrl + "/Products/Destroy", |
| 55 | + dataType: "jsonp" |
| 56 | + }, |
| 57 | + create: { |
| 58 | + url: crudServiceBaseUrl + "/Products/Create", |
| 59 | + dataType: "jsonp" |
| 60 | + }, |
| 61 | + parameterMap: function (options, operation) { |
| 62 | + if (operation !== "read" && options.models) { |
| 63 | + return { models: kendo.stringify(options.models) }; |
| 64 | + } |
| 65 | + } |
| 66 | + }, |
| 67 | + batch: true, |
| 68 | + pageSize: 20, |
| 69 | + schema: { |
| 70 | + model: { |
| 71 | + id: "ProductID", |
| 72 | + fields: { |
| 73 | + ProductID: { editable: false, nullable: true }, |
| 74 | + ProductName: { validation: { required: true } }, |
| 75 | + UnitPrice: { type: "number", validation: { required: true, min: 1 } }, |
| 76 | + Discontinued: { type: "boolean" }, |
| 77 | + UnitsInStock: { type: "number", validation: { min: 0, required: true } } |
| 78 | + } |
| 79 | + } |
| 80 | + }, |
| 81 | + }); |
| 82 | +
|
| 83 | + $("#grid").kendoGrid({ |
| 84 | + dataSource: dataSource, |
| 85 | + pageable: true, |
| 86 | + height: 550, |
| 87 | + toolbar: ["create"], |
| 88 | + columns: [ |
| 89 | + { field: "ProductName", title: "Product Name" }, |
| 90 | + { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "120px" }, |
| 91 | + { field: "UnitsInStock", title: "Units In Stock", width: "120px" }, |
| 92 | + { field: "Discontinued", width: "120px", editor: customBoolEditor }, |
| 93 | + { command: ["edit", "destroy"], title: " ", width: "250px" }], |
| 94 | + editable: "popup", |
| 95 | + edit: function (e) { |
| 96 | + var editWindow = this.editable.element.data("kendoWindow"); |
| 97 | + editWindow.bind("close", onWindowEditClose); |
| 98 | + }, |
| 99 | + save: function (e) { |
| 100 | + preventCloseOnSave = true; |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | +
|
| 105 | + var onWindowEditClose = function (e) { |
| 106 | + if (preventCloseOnSave) { |
| 107 | + e.preventDefault(); |
| 108 | + preventCloseOnSave = false; |
| 109 | + } |
| 110 | + }; |
| 111 | +
|
| 112 | + $(".k-grid-cancel").on("mousedown", function (e) { |
| 113 | + preventCloseOnSave = false; |
| 114 | + }); |
| 115 | +
|
| 116 | + function customBoolEditor(container, options) { |
| 117 | + var guid = kendo.guid(); |
| 118 | + $('<input class="k-checkbox" id="' + guid + '" type="checkbox" name="Discontinued" data-type="boolean" data-bind="checked:Discontinued">').appendTo(container); |
| 119 | + $('<label class="k-checkbox-label" for="' + guid + '"></label>').appendTo(container); |
| 120 | + } |
| 121 | +</script> |
| 122 | +``` |
| 123 | + |
0 commit comments