AngularJS 与 celltemplate 一起使用时,单元格导航不起作用

AngularJS cell navigation not working while using with celltemplate

我的控制器中有以下代码:

app.controller("homeController", homeController);
homeController.$inject = ["ui.grid.cellNav", "$scope"];

$scope.gridOptions = {
        enableRowSelection: true,
        enableSelectAll: true,
        modifierKeysToMultiSelect: false,        
        enableRowHeaderSelection: false,
    };


vm.objectViewGridOptions = {
    enableColumnMenus: false,
    enableSorting: true,
    enableGridMenu: true,
    angularCompileRows: true,
    multiSelect: false,
    modifierKeysToMultiSelect: false,                        
    enableRowSelection: true,
    enableRowHeaderSelection: false,
    exporterMenuPdf: false,
    onRegisterApi: function (gridApi) {
    //set gridApi on scope
    vm.objectViewGridOptions.gridApi = gridApi;                            
    gridApi.cellNav.on.navigate($scope, function (newRowCol, oldRowCol){                  
                                     vm.objectViewGridOptions.gridApi.selection.selectRow(newRowCol.row.entity);
                                    vm.objectViewGridOptions.gridApi.core.notifyDataChange($scope.gridApi.grid, uiGridConstants.dataChange.COLUMN);
    });
   }       
};


var colDef = [];               

for (var i = 0; i < columnNames.length; i++) {
                    if (i < 4) {
                        colDef.push(
                        {
                            field: columnNames[i],
                            displayName: columnNames[i],
                            width: '100',
                            // Highlighting first row in ui-grid
                            cellTemplate: '<div ui-grid-selection class="ui-grid-cell-contents"   ng-style="{\'background-color\':grid.appScope.getBackgroundColor(row,true)}">{{ COL_FIELD }}</div>',
                            sortingAlgorithm: sort
                        });
                    }
                    else
                        colDef.push(
                        {
                            field: columnNames[i],
                            displayName: columnNames[i],
                            width: '100',
                            cellTemplate: '<div ui-grid-selection class="ui-grid-cell-contents" ng-style="{\'background-color\':grid.appScope.getBackgroundColor(row,false)}" >{{ COL_FIELD }}</div>',
                            sortingAlgorithm: sort
                        });
                }

vm.objectViewGridOptions.columnDefs = colDef;

在我的 HTML 中:

<div style="clear:both;" ui-grid="vm.objectViewGridOptions" ng-if="vm.objectViewGridOptions.data" ui-grid-resize-columns ui-grid-cellnav ui-grid-move-columns ui-grid-exporter class="objectviewgrid"></div>

我使用 cellTemplate 为网格单元格提供背景颜色,并使用 ui-grid-cellNav 在网格单元格之间导航。

如果我将 ui-grid-cellNavcellTemplate 一起使用,单元格导航将无法正常工作。如果我注释掉 cellTemplate 代码,则单元格导航工作正常。

我哪里被击中了?忘记循环逻辑和所有。我无法将整个代码放在这里。这已经看起来很笨拙了。

我使用了 cellClass 和 cellStyle 而不是 cellTemplate。这是我控制器中的代码:

for (var i = 0; i < columnNames.length; i++) {
                        if (i < 4) {
                            colDef.push(
                            {
                                field: columnNames[i],
                                displayName: columnNames[i],
                                width: '100',
                                // fix for highlighting first row in ui-grid
                                //cellTemplate: '<div ui-grid-selection class="ui-grid-cell-contents"   ng-style="{\'background-color\':grid.appScope.getBackgroundColor(row,true)}">{{ COL_FIELD }}</div>',
                                cellClass: 'ui-grid-cell-contents',
                                cellStyle: { 'background-color': 'backgroundColor(row, true)' },
                                sortingAlgorithm: sort
                            });
                        }
                        else
                            colDef.push(
                            {
                                field: columnNames[i],
                                displayName: columnNames[i],
                                width: '100',
                                //cellTemplate: '<div ui-grid-selection class="ui-grid-cell-contents" ng-style="{\'background-color\':grid.appScope.getBackgroundColor(row,false)}" >{{ COL_FIELD }}</div>',
                                cellClass: 'ui-grid-cell-contents',
                                cellStyle: { 'background-color': 'backgroundColor(row, false)' },
                                sortingAlgorithm: sort
                            });
                    }

以上代码允许我同时使用 cellNav 和单元格自定义样式。