简体   繁体   English

Kendo UI 自定义网格弹窗编辑器 window 只打开一次

[英]Kendo UI custom grid popup editor window only opens once

I would like to use a custom window as popup editor of a Kendo UI Grid.我想使用自定义 window 作为 Kendo UI Grid 的弹出式编辑器。 Its content will be complex with search fields and a grid to display search results.它的内容将很复杂,包含搜索字段和显示搜索结果的网格。 To do that, I don't want to use the Kendo template mechanism but a real popup window.为此,我不想使用 Kendo 模板机制,而是使用真正的弹出窗口 window。

While doing tests with custom editor I faced an issue.在使用自定义编辑器进行测试时,我遇到了一个问题。 Even with a very basic and simple scenario (just the create command), I'm only able to open the editor once.即使是非常基本和简单的场景(只是创建命令),我也只能打开编辑器一次。 The second time I get an error, the editor doesn't show up anymore and the grid becomes empty.第二次出现错误时,编辑器不再显示,网格变空。

Here is my HTML code:这是我的 HTML 代码:

<div id="main-content">

    <div id="custom-window">
    </div>

    <table id="my-table-grid">
    </table>

</div>

The JavaScript part: JavaScript部分:

function openCustomWindow(e) {
   e.preventDefault();
   myWindow.center().open();
}

function editorWindowClosed(e) {  
    myGrid.cancelRow(); 
}

var myWindow = $("#custom-window").kendoWindow({
            modal: true,
            resizable: false,
            title: "Test",
            visible: false,
            close: editorWindowClosed
        }).data("kendoWindow");

var dummyDataSource = new kendo.data.DataSource(
    {
        schema : {
            model : {
                id: "id",
                fields: {
                    postion: { type: "number"},
                    name: { type: "string"},
                }
            }
        }
    });

dummyDataSource.add({postion: 1, name: "gswin"});

var myGrid = $("#my-table-grid").kendoGrid({
                dataSource: dummyDataSource,
                toolbar: [ {
                    name: "create",
                    text: "Create"
                } ],
                editable: {
                    mode: "popup",
                    window: {
                        animation: false,
                        open: openCustomWindow,
                    }
                },
                columns: [
                    {
                        field: "postion",
                        title: "Postion"
                    },
                    {
                        field: "name",
                        title: "Name"
                    }
               ]
            }).data("kendoGrid");

The error message in the JavaScript console: JavaScript控制台报错信息:

Uncaught TypeError: Cannot read property 'uid' of undefinedut.ui.DataBoundWidget.extend.cancelRow @ kendo.all.min.js:29ut.ui.DataBoundWidget.extend.addRow @ kendo.all.min.js:29(anonymous function) @ kendo.all.min.js:29jQuery.event.dispatch @ jquery-2.1.3.js:4430jQuery.event.add.elemData.handle @ jquery-2.1.3.js:4116未捕获的 TypeError:无法读取 undefinedut.ui.DataBoundWidget.extend.cancelRow 的属性“uid”@kendo.all.min.js:29ut.ui.DataBoundWidget.extend.addRow @kendo.all.min.js:29(匿名函数) @kendo.all.min.js:29jQuery.event.dispatch @jquery-2.1.3.js:4430jQuery.event.add.elemData.handle @jquery-2.1.3.js:4116

And finally a jsfiddle link to show what I'm doing.最后是一个jsfiddle 链接来展示我在做什么。 (The popup is empty because I just want to fix the open / close mechanism before going any further) (弹出窗口是空的,因为我只想在继续之前修复打开/关闭机制)

I don't understand why I get this error, I expected to be able to open / close the popup as many time as I wanted.我不明白为什么会出现此错误,我希望能够根据需要多次打开/关闭弹出窗口。 The default editor popup is working fine.默认的编辑器弹出窗口工作正常。

One of my colleague found the solution (thanks to him). 我的一位同事找到了解决方案(感谢他)。

Actually this piece of code 其实这段代码

        editable: {
            mode: "popup",
            window: {
                animation: false,
                open: openCustomWindow,
            }
        }

is designed to configure the default popup... 旨在配置默认弹出窗口...

The right way to use a custom popup is the following : 使用自定义弹出窗口的正确方法如下:

       editable :true,
       edit: openCustomWindow,

With this approach it's also better to use 通过这种方法,最好也使用

function editorWindowClosed(e) {  
    myGrid.cancelChanges(); 
}

Instead of 代替

function editorWindowClosed(e) {  
    myGrid.cancelRow(); 
}

Working jsfiddle link 工作jsfiddle链接

Actually the approach in my previous answer solved my issue but is causing another issue. 实际上,我先前的答案中的方法解决了我的问题,但又导致了另一个问题。 The grid becomes editable but in the default mode (which is "inline"). 网格变为可编辑状态,但处于默认模式(“内联”)。

Doing this 这样做

editable: "popup",
edit: openCustomWindow

has fixed this other issue but is now displaying 2 popups. 已解决此其他问题,但现在显示2个弹出窗口。

I finally succeeded to hide the default popup and only show my custom popup but it ended with the orginal issue described in initial question... 我终于成功地隐藏了默认弹出窗口,只显示了我的自定义弹出窗口,但是它以最初的问题中描述的原始问题结束了...

Considering all those informations I arrived to the conclusion that working with a custom popup window is definitely not an option. 考虑到所有这些信息,我得出的结论是,使用自定义弹出窗口绝对不是一种选择。 I'll start working with teamplates instead. 我将开始使用teamplates。

Another solution would have been to use a template to override the toolbar with a custom "Add" button and use a custom command for edition. 另一个解决方案是使用模板通过自定义的“添加”按钮覆盖工具栏,并使用自定义命令进行编辑。 But at this point I consider that too "tricky". 但是在这一点上,我认为这太“棘手”了。

To prevent Kendo UI custom grid popup editor window, define the editable property:为了防止 Kendo UI 自定义网格弹出编辑器 window,定义 editable 属性:

editable: 
    {
        mode: "popup",
        window: {               
            animation: false,
            open: updateRowEventHandler
        }
    } // skip edit property

Then paste preventDefault() at the beginning of the handler:然后将 preventDefault() 粘贴到处理程序的开头:

function updateRowEventHandler(e) {
     e.preventDefault(); // 

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

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