Dgrid set focus on cell

主宰稳场 提交于 2019-12-10 15:27:38

问题


I am using dgrid for displaying data in grid format , it has four columns. all are editable

I have used following way to declare it.

<table id="grid" data-dojo-type="dgrid.CustomGrid" data-dojo-props="store: memoryStore">
<thead>
    <tr>
        <th data-dgrid-column="dgrid.editor({ field: 'id' }, dijit.form.TextBox, 'click')">ID</th>
        <th data-dgrid-column="dgrid.editor({ field: 'name' }, dijit.form.TextBox, 'click')">Name</th>
        <th data-dgrid-column="dgrid.editor({ field: 'description' }, dijit.form.TextBox, 'click')">Description</th>
    </tr>
</thead>
</table>

My Problem is , when I will edit first column , after editing it should set focus on second column and show cursor in that cell so I can start editing second one ; same is for third column.

I am very much new to dojo and dgrid. I found some APIs on sitepen but couldn't solve my problem

Please help me on this


回答1:


There is dgrid/Grid::edit(cell) method to start with.

I used dojo/aspect::after to add an event listener to an editor widget (e.g. dijit/form/TextBox), if it does not exist, to listen keypress event for a specific key, in my case TAB, and then call grid.edit(cell) with the cell that should be edited next.

See a working example at jsFiddle: http://jsfiddle.net/phusick/2jU7R/

It is far from finished, but at least it can provide a possible directon. Use double click to edit and press TAB when editing to jump to the following cell.

aspect.after(grid, "edit", function(promise, cellNode) {

    if(promise === null) return;

    promise.then(function(widget) {
        if (!widget._editorKeypressHandle) {
            widget._editorKeypressHandle = widget.on("keypress", function(e) {

                for (var rowId in grid.selection) { break;}            
                for (var columnId in grid.selection[rowId]) { break;}

                if(e.charOrCode == keys.TAB) {
                    e.preventDefault();
                    var cellEdited = grid.cell(rowId, columnId);
                    var cellToEdit = grid.right(cellEdited, 1);

                    if(cellEdited.row.id == cellToEdit.row.id
                       && cellEdited.column.id == cellToEdit.column.id
                      ) {
                          // go to next line
                          for(var firstColumnId in grid.columns) { break;}
                          var nextRow = grid.down(cellEdited.row, 1);
                          cellToEdit = grid.cell(nextRow, firstColumnId);
                    };

                    grid.deselect(cellEdited);
                    grid.select(cellToEdit);
                    grid.edit(cellToEdit);
                }
            })
        }
    });
});



回答2:


Here's another option. Also included a workaround the text selection bug in older version of dgrid.

postCreate: function() {
    var that = this;
    this.inherited(arguments);

    // FIXME Workaround bug in dgrid that causes the grid to lose
    // focus after each save.
    this.on('dgrid-datachange', function(evt) {
        that._selectedCell = that.cell(evt);
    });

    aspect.after(this, 'save', function(dfd) {
        dfd.then(function() {
        var nextCell = that.right(that.cell(that._selectedCell.row.id, that._selectedCell.column.id));
            that.edit(nextCell);
        // FIXME Workaround dgrid bug that blocks field text to be selected on focus.
        nextCell.element.widget && nextCell.element.widget.textbox && nextCell.element.widget.textbox.select();
        });
    });
}


来源:https://stackoverflow.com/questions/12216280/dgrid-set-focus-on-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!