Skip to content

Commit 23d83de

Browse files
committed
Merge pull request #2374 from PaulL1/2161_cell_class
Fix #2161 #2120 (cellClass): watch that notices changed cell, update cel...
2 parents d1abe4c + 0957dc0 commit 23d83de

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

misc/tutorial/111_cellClass.ngdoc

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ In this example, we will override the color and background for the first column
6262
// color blue for 2,1, which has company name 'Velity'
6363
gridTestUtils.expectCellValueMatch( 'grid1', 2, 1, 'Velity' );
6464
expect( gridTestUtils.dataCell( 'grid1', 2, 1 ).getCssValue('color')).toEqual('rgba(0, 0, 255, 1)');
65+
66+
// sort by company, 2,1 is no longer Velity so shouldn't be blue, check it's the same colour as row 1
67+
gridTestUtils.clickHeaderCell( 'grid1', 1 );
68+
gridTestUtils.expectCellValueMatch( 'grid1', 2, 1, 'Acusage' );
69+
expect( gridTestUtils.dataCell( 'grid1', 1, 1 ).getCssValue('color')).toEqual('rgba(44, 62, 80, 1)');
70+
expect( gridTestUtils.dataCell( 'grid1', 2, 1 ).getCssValue('color')).toEqual('rgba(44, 62, 80, 1)');
6571
});
6672
});
6773
</file>

src/js/core/directives/ui-grid-cell.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,27 @@ angular.module('ui.grid').directive('uiGridCell', ['$compile', '$parse', 'gridUt
6767
// Register a data change watch that would get triggered whenever someone edits a cell or modifies column defs
6868
var watchUid = $scope.grid.registerDataChangeCallback( updateClass, [uiGridConstants.dataChange.COLUMN, uiGridConstants.dataChange.EDIT]);
6969

70+
// watch the col and row to see if they change - which would indicate that we've scrolled or sorted or otherwise
71+
// changed the row/col that this cell relates to, and we need to re-evaluate cell classes and maybe other things
72+
var cellChangeFunction = function( n, o ){
73+
if ( n !== o ) {
74+
if ( classAdded || $scope.col.cellClass ){
75+
updateClass();
76+
}
77+
}
78+
};
79+
80+
var colWatchDereg = $scope.$watch( 'col', cellChangeFunction );
81+
var rowWatchDereg = $scope.$watch( 'row', cellChangeFunction );
82+
83+
7084
var deregisterFunction = function() {
71-
$scope.grid.deregisterDataChangeCallback( watchUid );
85+
$scope.grid.deregisterDataChangeCallback( watchUid );
86+
colWatchDereg();
87+
rowWatchDereg();
7288
};
7389

90+
7491
$scope.$on( '$destroy', deregisterFunction );
7592
}
7693
};

test/unit/core/directives/uiGridCell.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ describe('uiGridCell', function () {
6565
$scope.grid.api.core.notifyDataChange( $scope.grid, uiGridConstants.dataChange.COLUMN );
6666
expect(gridCell.hasClass('funcCellClass')).toBe(false);
6767
}));
68+
69+
it('should notice col changes and update cellClass', inject(function () {
70+
$scope.col.cellClass = function (grid, row, col, rowRenderIndex, colRenderIndex) {
71+
if (rowRenderIndex === 2 && colRenderIndex === 2) {
72+
if ( col.noClass ){
73+
return '';
74+
} else {
75+
return 'funcCellClass';
76+
}
77+
}
78+
};
79+
recompile();
80+
var displayHtml = gridCell.html();
81+
expect(gridCell.hasClass('funcCellClass')).toBe(true);
82+
83+
$scope.col = new GridColumn({name: 'col2'}, 0, $scope.grid);
84+
$scope.$digest();
85+
expect(gridCell.hasClass('funcCellClass')).toBe(false);
86+
}));
6887
});
6988

89+
7090
});

0 commit comments

Comments
 (0)