jqgrid使用" tab"编辑和添加行键

时间:2011-01-20 11:04:14

标签: jqgrid

我是jqgrid的新手,我正试图用“tab”键浏览网格。 我希望能够编辑一行,当我正在编辑该行中的最后一个单元格时,如果我单击tab键,它将保存当前行更改(在客户端,而不是通过单击enter)并设置焦点到下一行并编辑它的单元格,当我到达最后一行和单元格时,选项卡单击将添加一个新行并使其处于编辑模式。

我尝试使用在线编辑,然后使用单元格编辑,但总是卡住... 如何才能做到这一点?

提前致谢。

1 个答案:

答案 0 :(得分:2)

我不知道你现在拥有什么,但我测试了它并且它有效。由于您没有提到最初如何开始编辑网格,我在ready事件中手动完成,您只需使用selIRow var跟踪当前正在编辑的行。

var selIRow = 1; //keep track of currently edited row
                 //initialized to 1 for testing purposes

    $(document).ready(function () {
        $("#jqGrid").jqGrid({
            datatype: 'local',
            colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'],
            colModel: [
                { name: 'id', index: 'id', width: 60, editable: true },
                { name: 'invdate', index: 'invdate', width: 90, editable: true },
                { name: 'name', index: 'name', width: 100, editable: true },
                { name: 'amount', index: 'amount', width: 80, editable: true },
                { name: 'tax', index: 'tax', width: 80, editable: true },
                { name: 'total', index: 'total', width: 80, editable: true },
                { name: 'note', index: 'note', width: 150, editable: true,
                    //Place this code in the col options of the last column in your grid
                    // it listens for the tab button being pressed
                    editoptions: {
                        dataInit: function (elem) { $(elem).focus(function () { this.select(); }) },
                        dataEvents: [
                            {
                                type: 'keydown',
                                fn: function (e) {
                                    var key = e.charCode || e.keyCode;
                                    if (key == 9)//tab
                                    {
                                        var grid = $('#jqGrid');
                                        //Save editing for current row
                                        grid.jqGrid('saveRow', selIRow, false, 'clientArray');
                                        //If at bottom of grid, create new row
                                        if (selIRow++ == grid.getDataIDs().length) {
                                            grid.addRowData(selIRow, {});
                                        }
                                        //Enter edit row for next row in grid
                                        grid.jqGrid('editRow', selIRow, false, 'clientArray');
                                    }
                                }
                            }
                        ]
                    }
                }
            ],
        });
    });

对于标签事件,kajo从here回答了一些信用。