简体   繁体   English

Angular UI Grid将焦点设置为导航时的单元格

[英]Angular UI Grid set focus to cell on navigate

I have a grid which is editable. 我有一个可编辑的网格。 Two of the columns are ItemCode and Quantity. 其中两列是ItemCode和Quantity。 When a user enters a Quantity and presses the down key, I would like the next row to set the focus on the ItemCode cell for the next row. 当用户输入数量并按下向下键时,我希望下一行将焦点设置在下一行的ItemCode单元格上。 I added this event listener and it goes to the cell, but it quickly flashes into edit mode, then out into selected mode. 我添加了这个事件监听器并将其转到单元格,但它会快速进入编辑模式,然后进入选定模式。 How can I make sure the cell stays in edit mode? 如何确保单元格保持编辑模式?

gridApi.cellNav.on.navigate($scope, function (newRowcol, oldRowCol) {
    $scope.$broadcast('uiGridEventEndCellEdit');

    if (oldRowCol != null && 
        oldRowCol.col.colDef.name == 'quantity' && 
        oldRowCol.row != newRowcol.row) {
        newRowcol.row.grid.api.cellNav
            .scrollToFocus(newRowcol.row.entity, 
                            newRowcol.row.grid.options.columnDefs[1]);
        }
    });

UPDATE: Here is my columnDefs: 更新:这是我的columnDefs:

 $scope.gridOptions.columnDefs = [
 {
    name: 'itemCode',
    displayName: 'Part #',
    editableCellTemplate: $scope.itemCodeEditableTemplate,
    width: 150,
    cellTooltip: function (row, col) {
        return row.entity.itemCode
    }
},
{
    name: 'quantity', displayName: 'Qty', type: 'number', width: 60, 
    cellTooltip: function (row, col) {
      return row.entity.quantity
    }
}
]

As I commented above, I believe this is a bug in angular-ui-grid. 正如我上面评论的那样,我认为这是angular-ui-grid中的一个错误。 My workaround is as follows... 我的解决方法如下......

The problem is using scrollToFocus() to go to another line, but your normal keyboard operation (tab, down arrow etc) would have taken you to a column that has enableCellEdit: true . 问题是使用scrollToFocus()转到另一行,但正常的键盘操作(选项卡,向下箭头等)会将您带到一个具有enableCellEdit: true的列。 So one workaround is to have the column AFTER where you are calling scrollToFocus() from be set with enableCellEdit: false . 因此,一种解决方法是使用enableCellEdit: false设置要调用scrollToFocus()的列AFTER。

It's ugly, but I created a "spacer" column after every column that could possibly be tabbed from. 这很丑陋,但我在每一个可能被选中的列之后创建了一个“spacer”列。

{ name: 'itemCode', displayName: 'part #' },
{ name: 'quantity', displayName: 'qty' },
{ name: '_spacer1', displayName: '', width: '0%', minWidth: 1, maxWidth: 1, enableCellEdit: false },
{ name: 'someOtherColumn', displayName: 'something'},

So, in my case I wanted to catch a tab from "quantity" to "someOtherColumn" and if the value of row.entity.quantity was 0 (for example), then I would call scrollToFocus() to the next line. 所以,在我的情况下,我想从“数量”到“someOtherColumn”捕获一个选项卡,如果row.entity.quantity的值为0(例如),那么我将调用scrollToFocus()到下一行。 Because the normal next cell for tabbing had enableCellEdit: false it would work. 因为enableCellEdit: false的正常下一个单元格具有enableCellEdit: false所以它可以工作。

Now for your case, I am not sure it helps, because you are working with down arrow presses, which means you are not changing column with the normal keyboard operation. 现在针对您的情况,我不确定它是否有帮助,因为您正在使用向下箭头按下,这意味着您不使用正常的键盘操作更改列。 But perhaps it gives you an idea. 但也许它会给你一个想法。 (In addition, if your users would tab instead of down arrow, then you can use the workaround, or possibly not even need it, depending on which other columns you have.) (此外,如果您的用户使用tab而不是向下箭头,那么您可以使用变通方法,甚至可能根本不需要它,具体取决于您拥有的其他列。)

I think a better idea might be to re-open my bug (linked in comments above) and say you are also having the problem and see if you can get a straight answer on what the real fix should be! 我认为一个更好的想法可能是重新打开我的错误(在上面的评论中链接),并说你也遇到了问题,看看你是否可以直接回答真正的修复应该是什么! (I'd love to see it.) (我很乐意看到它。)

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

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