繁体   English   中英

Kendo Grid如何以编程方式聚焦网格单元格并阻止选择文本

[英]Kendo Grid how to programmatically focus a grid cell and block select the text

我有一个kendo网格,编辑模式设置为incell。

以编程方式聚焦特定单元格以使其进入编辑模式的最优雅方式是什么?

假设我有一个网格,其中第1列和第6列是可编辑的。 一旦用户完成在第1列中输入内容,我就希望第6列文本框能够自动聚焦并启用进行编辑,这样用户就不必手动点击同一行的下一个可编辑网格单元。

这就是我现在正在做的事情:

//Focuses the editable cell at given row/column index.
//Closes the previously editing cell
//EX: setGridFocus(SALE_01_DIV_GRID,0,0) --> Focuses SALE_01_DIV_GRID (0,0)
function setGridFocus(gridID, rowIndex, colIndex)
{
    var grid = $('#' + gridID).data('kendoGrid');
    grid.closeCell();

    setTimeout(function(){
        var cell = $('#' + gridID).find('tbody tr:eq('+rowIndex+') td:eq('+colIndex+')');
        grid.editCell(cell);
        var editTextCell = cell.find("input.k-formatted-value");

        editTextCell.focus(function() {
            $(this).select().mouseup(function (e) {
                e.preventDefault();
                $(this).unbind("mouseup");
                e.select();
            });
        });
        cell.find("input[type=text]").select();
        editTextCell.selectall();
    },50); 
}

首先,我正在使用setTimeout来实现看似简单的函数,所以这似乎不是理想的方法。

其次,上面的功能只有在感觉就好的时候才有效(只有一半的时间来自测试。可能是从setTimeout函数中预期的)。 我觉得这与Kendo Grid内部调用的事件顺序有关。

第三,我想阻止选择正在聚焦的单元格内的文本。 editTextCell.selectall(); 不适用于此目的。

我很感激任何指导。

这是实现您想要的可靠方式。 它仍然使用setTimeout ,因此它可以可靠地聚焦不同的剑道输入( 用于聚焦剑道输入的文档源 ):

function setGridFocus(gridID, rowIndex, colIndex) {
  var grid = $('#' + gridID).data('kendoGrid');
  grid.closeCell();

  var cell = $('#' + gridID).find('tbody tr:eq(' + rowIndex + ') td:eq(' + colIndex + ')');
  grid.editCell(cell);
  var editTextCell = cell.find('input');

  var selectTimeId;

  editTextCell
    .on('focus', function() {
      var input = $(this);
      clearTimeout(selectTimeId); // stop started time out if any

      selectTimeId = setTimeout(function() {
        input.select();
        // To make this work on iOS, too, replace the above line with the following one. Discussed in https://stackoverflow.com/q/3272089
        // input[0].setSelectionRange(0, 9999);
      });
    })
    .blur(function(e) {
      clearTimeout(selectTimeId); // stop started timeout
    });


  editTextCell.focus();
}

暂无
暂无

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

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