【问题标题】:How to add a new record in kendo grid with enter key press event如何使用输入按键事件在剑道网格中添加新记录
【发布时间】:2013-07-04 04:44:00
【问题描述】:

我使用一些 kendo ui 工具开发了一个简单的网络应用程序。

在那里我使用剑道网格工具来查看和插入产品详细信息的数据。

但我的问题是在 编辑模式(当我插入新记录时)当我按下 enter 键时我需要转到下一个单元格(同一行的下一列),然后在最后一列之后转到下一行(新记录)。

这是我在 view "index.cshtml"

中用于网格的代码
    button class="k-button" id="batchGrid">
    Batch Edit</button>
<div id="example" class="k-content">
    <div id="batchgrid">
    </div>
</div>
<script>
    $("#batchGrid").click(function () {
        var crudServiceBaseUrl = "http://demos.kendoui.com/service",
                        dataSource = new kendo.data.DataSource({
                            transport: {
                                read: {
                                    url: crudServiceBaseUrl + "/Products",
                                    dataType: "jsonp"
                                },
                                update: {
                                    url: crudServiceBaseUrl + "/Products/Update",
                                    dataType: "jsonp"
                                },
                                destroy: {
                                    url: crudServiceBaseUrl + "/Products/Destroy",
                                    dataType: "jsonp"
                                },
                                create: {
                                    url: crudServiceBaseUrl + "/Products/Create",
                                    dataType: "jsonp"
                                },
                                parameterMap: function (options, operation) {
                                    if (operation !== "read" && options.models) {
                                        return { models: kendo.stringify(options.models) };
                                    }
                                }
                            },
                            batch: true,
                            pageSize: 20,
                            schema: {
                                model: {
                                    id: "ProductID",
                                    fields: {
                                        ProductID: { editable: false, nullable: true },
                                        ProductName: { validation: { required: true} },
                                        UnitPrice: { type: "number", validation: { required: true, min: 1} },
                                        Discontinued: { type: "boolean" },
                                        UnitsInStock: { type: "number", validation: { min: 0, required: true} }
                                    }
                                }
                            }
                        });

        $("#batchgrid").kendoGrid({
            dataSource: dataSource,
            navigatable: true,
            filterable: true,
            pageable: true,
            height: 430,
            width: 300,
            toolbar: ["create", "save", "cancel"],
            columns: [
                            "ProductName",
                            { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "130px" },
                            { field: "UnitsInStock", title: "Units In Stock", width: "130px" },
                            { field: "Discontinued", width: "130px" },
                            { command: ["destroy"], title: "&nbsp;", width: "100px"}],
            editable: true
        });
    });
</script>

如何在剑道网格中处理输入按键事件并遍历列和行?

请帮帮我。

【问题讨论】:

    标签: jquery kendo-ui keypress kendo-grid enter


    【解决方案1】:

    试试这个代码。

    $("#batchgrid").on("click", "td", function (e) {
    
                var rowIndex = $(this).parent().index();
                var cellIndex = $(this).index();
                $("input").on("keydown", function (event) {
                    if (event.keyCode == 13) {
                        $("#batchgrid").data("kendoGrid").editCell($(".k-grid-content").find("table").find("tbody").find("tr:eq(" + rowIndex + ")").find("td:eq(" + cellIndex + ")").next().focusin($("#batchgrid").data("kendoGrid").closeCell($(".k-grid-content").find("table").find("tbody").find("tr:eq(" + rowIndex + ")").find("td:eq(" + cellIndex + ")").parent())));
                        return false;
                    }
                });
    
            });
    

    【讨论】:

    • 我真的很抱歉先生,但问题仍然存在。我认为问题是组合框。当我在文本框中按 enter 时,无论那里有什么,它都会跳转到下一个单元格,但是当我在组合框或复选框或下拉菜单中按回车,它不会像在文本框中那样正常工作。如果可以请给我一个合适的解决方案。
    【解决方案2】:

    试试这个代码,它可以帮助你在输入按键时移动下一个单元格,

     $(document).ready(function () {
            $("#batchGrid").on("click", "td", function (e) {
                var rowIndex = $(this).parent().index();
                var cellIndex = $(this).index();
                window.onkeydown = function (event) {
                    if (event.keyCode == 13) {
                        $("#batchGrid").data("kendoGrid").editCell($(".k-grid-content").find("table").find("tbody").find("tr:eq(" + rowIndex + ")").find("td:eq(" + cellIndex + ")").next().focusin($("#batchGrid").data("kendoGrid").closeCell($(".k-grid-content").find("table").find("tbody").find("tr:eq(" + rowIndex + ")").find("td:eq(" + cellIndex + ")").parent())));
    
                    }
                }
    
            });
        });
    

    【讨论】:

    • 我尝试了您的代码,但没有给出任何积极的反馈。当我按下回车键时,它不会移动到下一个单元格。请在此处关注..
    【解决方案3】:

    试试这个

     $("#batchgrid").on("click", "td", function (e) {
    
                var rowIndex = $(this).parent().index();
                var cellIndex = $(this).index();
                $(".k-textbox").on("keydown", function (event) {
                    if (event.keyCode == 13) {
                        $("#batchgrid").data("kendoGrid").editCell($(".k-grid-content").find("table").find("tbody").find("tr:eq(" + rowIndex + ")").find("td:eq(" + cellIndex + ")").next().focusin($("#batchgrid").data("kendoGrid").closeCell($(".k-grid-content").find("table").find("tbody").find("tr:eq(" + rowIndex + ")").find("td:eq(" + cellIndex + ")").parent())));
                        return false;
                    }
                });
    
            });
    

    【讨论】:

    • 到目前为止,如果我只使用文本框,它可以工作,但问题是当我在组合框或复选框中按下回车键时,它无法正常工作。如何解决?
    • 我真的很抱歉先生,但问题仍然存在。我认为问题是组合框。当我在文本框中按 Enter 时,无论那里有什么,它都会跳转到下一个单元格,但是当我在组合框或复选框或下拉菜单中按回车,它不会像在文本框中那样正常工作。如果可以请给我一个合适的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2014-06-14
    • 2016-07-02
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多