Skip to content

Commit 7c5cdca

Browse files
committed
fix(uiGridHeader): Allow header to shrink in size
The combo of ui-grid-selection and filtering was causes the headers to be unable to shrink in size if filtering were toggled off, because the selection grid header had an explicit height and when we saw it we would use that as the height to match up with. This change removes all explicit heights from the "max height" check, so we only rely on rendered dynamic heights to calculate the max height. A unit test has also been added to the selection specs to cover this case. Fixes #3138
1 parent 7375c49 commit 7c5cdca

File tree

2 files changed

+78
-25
lines changed

2 files changed

+78
-25
lines changed

src/features/selection/test/uiGridSelectionDirective.spec.js

+54-18
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ describe('ui.grid.selection uiGridSelectionDirective', function() {
22
var parentScope,
33
elm,
44
scope,
5-
gridCtrl;
5+
gridCtrl,
6+
$compile,
7+
$rootScope,
8+
uiGridConstants;
69

710
beforeEach(module('ui.grid.selection'));
811

9-
beforeEach(function() {
10-
var rootScope;
12+
beforeEach(inject(function(_$rootScope_, _$compile_, _uiGridConstants_) {
13+
$compile = _$compile_;
14+
$rootScope = _$rootScope_;
15+
uiGridConstants = _uiGridConstants_;
1116

12-
inject([
13-
'$rootScope',
14-
function (rootScopeInj) {
15-
rootScope = rootScopeInj;
16-
}
17-
]);
18-
19-
parentScope = rootScope.$new();
17+
parentScope = $rootScope.$new();
2018

2119
parentScope.options = {
2220
columnDefs : [{field: 'id'}]
@@ -32,19 +30,14 @@ describe('ui.grid.selection uiGridSelectionDirective', function() {
3230
}
3331

3432
var tpl = '<div ui-grid="options" ui-grid-selection options="options"></div>';
35-
36-
inject([
37-
'$compile',
38-
function ($compile) {
39-
elm = $compile(tpl)(parentScope);
40-
}]);
33+
elm = $compile(tpl)(parentScope);
4134

4235
parentScope.$digest();
4336
scope = elm.scope();
4437

4538
gridCtrl = elm.controller('uiGrid');
4639

47-
});
40+
}));
4841

4942
it('should set the "enableSelection" field of the row using the function specified in "isRowSelectable"', function() {
5043
for (var i = 0; i < gridCtrl.grid.rows.length; i++) {
@@ -61,4 +54,47 @@ describe('ui.grid.selection uiGridSelectionDirective', function() {
6154
}
6255
}
6356
});
57+
58+
describe('with filtering turned on', function () {
59+
var elm, $timeout;
60+
61+
/*
62+
NOTES
63+
- We have to flush $timeout because the header calculations are done post-$timeout, as that's when the header has been fully rendered.
64+
- We have to actually attach the grid element to the document body, otherwise it will not have a rendered height.
65+
*/
66+
67+
beforeEach(inject(function (_$timeout_) {
68+
$timeout = _$timeout_;
69+
70+
parentScope.options.enableFiltering = true;
71+
72+
elm = angular.element('<div style="width: 300px; height: 500px" ui-grid="options" ui-grid-selection></div>');
73+
document.body.appendChild(elm[0]);
74+
$compile(elm)(parentScope);
75+
$timeout.flush();
76+
parentScope.$digest();
77+
}));
78+
79+
afterEach(function () {
80+
$(elm).remove();
81+
});
82+
83+
it("doesn't prevent headers from shrinking when filtering gets turned off", function () {
84+
// Header height with filtering on
85+
var filteringHeight = $(elm).find('.ui-grid-header').height();
86+
87+
dump(elm.controller('uiGrid').grid.api.core.notifyDataChange);
88+
89+
parentScope.options.enableFiltering = false;
90+
elm.controller('uiGrid').grid.api.core.notifyDataChange( uiGridConstants.dataChange.COLUMN );
91+
$timeout.flush();
92+
parentScope.$digest();
93+
94+
var noFilteringHeight = $(elm).find('.ui-grid-header').height();
95+
96+
expect(noFilteringHeight).not.toEqual(filteringHeight);
97+
expect(noFilteringHeight < filteringHeight).toBe(true);
98+
});
99+
});
64100
});

src/js/core/factories/Grid.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -1877,8 +1877,9 @@ angular.module('ui.grid')
18771877

18781878
container.innerHeaderHeight = innerHeaderHeight;
18791879

1880-
// Save the largest header height for use later
1881-
if (innerHeaderHeight > maxHeaderHeight) {
1880+
// If the header doesn't have an explicit height set, save the largest header height for use later
1881+
// Explicit header heights are based off of the max we are calculating here. We never want to base the max on something we're setting explicitly
1882+
if (!container.explicitHeaderHeight && innerHeaderHeight > maxHeaderHeight) {
18821883
maxHeaderHeight = innerHeaderHeight;
18831884
}
18841885
}
@@ -1893,8 +1894,9 @@ angular.module('ui.grid')
18931894
rebuildStyles = true;
18941895
}
18951896

1896-
// Save the largest header canvas height for use later
1897-
if (headerCanvasHeight > maxHeaderCanvasHeight) {
1897+
// If the header doesn't have an explicit canvas height, save the largest header canvas height for use later
1898+
// Explicit header heights are based off of the max we are calculating here. We never want to base the max on something we're setting explicitly
1899+
if (!container.explicitHeaderCanvasHeight && headerCanvasHeight > maxHeaderCanvasHeight) {
18981900
maxHeaderCanvasHeight = headerCanvasHeight;
18991901
}
19001902
}
@@ -1904,12 +1906,27 @@ angular.module('ui.grid')
19041906
for (i = 0; i < containerHeadersToRecalc.length; i++) {
19051907
container = containerHeadersToRecalc[i];
19061908

1907-
// If this header's height is less than another header's height, then explicitly set it so they're the same and one isn't all offset and weird looking
1908-
if (maxHeaderHeight > 0 && typeof(container.headerHeight) !== 'undefined' && container.headerHeight !== null && container.headerHeight < maxHeaderHeight) {
1909+
/* If:
1910+
1. We have a max header height
1911+
2. This container has a header height defined
1912+
3. And either this container has an explicit header height set, OR its header height is less than the max
1913+
1914+
then:
1915+
1916+
Give this container's header an explicit height so it will line up with the tallest header
1917+
*/
1918+
if (
1919+
maxHeaderHeight > 0 && typeof(container.headerHeight) !== 'undefined' && container.headerHeight !== null &&
1920+
(container.explicitHeaderHeight || container.headerHeight < maxHeaderHeight)
1921+
) {
19091922
container.explicitHeaderHeight = maxHeaderHeight;
19101923
}
19111924

1912-
if (typeof(container.headerCanvasHeight) !== 'undefined' && container.headerCanvasHeight !== null && maxHeaderCanvasHeight > 0 && container.headerCanvasHeight < maxHeaderCanvasHeight) {
1925+
// Do the same as above except for the header canvas
1926+
if (
1927+
maxHeaderCanvasHeight > 0 && typeof(container.headerCanvasHeight) !== 'undefined' && container.headerCanvasHeight !== null &&
1928+
(container.explicitHeaderCanvasHeight || container.headerCanvasHeight < maxHeaderCanvasHeight)
1929+
) {
19131930
container.explicitHeaderCanvasHeight = maxHeaderCanvasHeight;
19141931
}
19151932
}

0 commit comments

Comments
 (0)