Search code examples
jquerykendo-uitelerikkendo-gridtelerik-grid

Kendo Grid Key enter key press event handling


I have a Kendo grid.I wanted to handle the enter key down event.If a user press enter key inside a Kendo cell.it must focus and put the next cell to edit mode.(cursor should be moved to next grid cell). I have try this code

      $("#list").on("focus", "td", function (e) {         
     $("input").on("keydown", function (event) {
         if (event.keyCode == 13) {
             setTimeout(function () {
                 var curCell = $("#list").find(".k-state-selected")
                 var eCell = $("#list").find(".k-edit-cell")

                 curCell.removeClass("k-state-selected");
                 curCell.removeClass("k-state-focused");
                 curCell.removeAttr("data-role");
                 curCell.next().addClass("k-state-selected");
                 curCell.next().addClass("k-state-focused");
                 try {                         $('#list').data('kendoGrid').closeCell(eCell);
                 } catch (ex) {
                 }
                 $('#list').data('kendoGrid').select();                     
                 $('#list').data('kendoGrid').editCell(curCell.next());

             }, 50);

         }
     });
 });
 }

Demo

or is there any way to override the tab key functionality with enter key(Because tab key does that function properly)


Solution

  • Please try with the below code snippet.

    <!DOCTYPE html>
    <html>
    <head>
        <title>Jayesh Goyani</title>
        <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.902/styles/kendo.common-bootstrap.min.css" />
        <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.902/styles/kendo.bootstrap.min.css" />
        <script src="https://kendo.cdn.telerik.com/2015.2.902/js/jquery.min.js"></script>
        <script src="https://kendo.cdn.telerik.com/2015.2.902/js/kendo.all.min.js"></script>
    </head>
    <body>
        <div id="list"></div>
        <script>
            $(document).ready(function () {
                function onDataBound(e) {
                    $("#list").on("focus", "td", function (e) {
                        $("input").on("keydown", function (event) {
                            if (event.keyCode == 13) {
                                setTimeout(function () {
                                    var curCell = $("#list").find(".k-state-selected")
                                    var eCell = $("#list").find(".k-edit-cell")
    
                                    curCell.removeClass("k-state-selected");
                                    curCell.removeClass("k-state-focused");
                                    curCell.removeAttr("data-role");
                                    curCell.next().addClass("k-state-selected");
                                    curCell.next().addClass("k-state-focused");
                                    try {
                                        $('#list').data('kendoGrid').closeCell(eCell);
                                    } catch (ex) {
                                    }
                                    $('#list').data('kendoGrid').select(); $('#list').data('kendoGrid').editCell(curCell.next());
    
                                    if ($(curCell.next()).find('select')) {
                                        $($(curCell.next()).find('select')).data("kendoDropDownList").open()
                                    }
    
                                }, 50);
    
                            }
                        });
                    });
                }
    
                var savings = [{
                    month: "January",
                    saving: "$200",
                    kind: 1
                }, {
                    month: "March",
                    saving: "$100",
                    kind: 1
                }, {
                    month: "March sdjhf as dfjh;as",
                    saving: "$100",
                    kind: 1
                }, {
                    month: "March",
                    saving: "$100",
                    kind: 1
                }, {
                    month: "March",
                    saving: "$10000",
                    kind: 1
                }, {
                    month: "December",
                    saving: "$100",
                    kind: 1
                }, {
                    month: "March",
                    saving: "$100",
                    kind: 1
                }, {
                    month: "March",
                    saving: "$100",
                    kind: 2
                }, {
                    month: "March",
                    saving: "$100",
                    kind: 3
                }, {
                    month: "March",
                    saving: "$100",
                    kind: 1
                }];
    
                var savingsDataSource = new kendo.data.DataSource({
                    data: savings
                });
    
    
                var grid = $("#list").kendoGrid({
                    dataSource: savingsDataSource,
                    navigatable: true,
                    pageable: true,
                    height: 400,
                    selectable: "cell",
                    toolbar: ["create", "save", "cancel"],
                    columns: [{
                        field: "month",
                        title: "Month"
                    }, {
                        field: "saving"
                    }, {
                        field: "saving"
                    }, {
                        field: "kind",
                        values: [{
                            value: 1,
                            text: "Kind1"
                        }, {
                            value: 2,
                            text: "Kind2"
                        }, {
                            value: 3,
                            text: "Kind3"
                        }]
                    }],
                    editable: true,
                    dataBound: onDataBound,
    
                }).data("kendoGrid");
    
    
                var specialKeys = [kendo.keys.UP, kendo.keys.DOWN];
    
                function edit(e) {
                    //$("#list").find(".k-state-selected").focus();
                    //e.container.find("td:eq(1) input").focus();
    
                }
            });
        </script>
    </body>
    </html>
    

    Let me know if any concern.