Skip to content

Commit 1b080b9

Browse files
Portugal, Marcelomportuga
Portugal, Marcelo
authored andcommitted
fix(expandable.js): Update expandedAll attribute when toggling rows.
fix #6077
1 parent 214f6cc commit 1b080b9

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

src/features/expandable/js/expandable.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@
128128
var publicApi = {
129129
events: {
130130
expandable: {
131+
/**
132+
* @ngdoc event
133+
* @name rowExpandedBeforeStateChanged
134+
* @eventOf ui.grid.expandable.api:PublicApi
135+
* @description raised when row is expanding or collapsing
136+
* <pre>
137+
* gridApi.expandable.on.rowExpandedBeforeStateChanged(scope,function(row){})
138+
* </pre>
139+
* @param {scope} scope the application scope
140+
* @param {GridRow} row the row that was expanded
141+
*/
142+
rowExpandedBeforeStateChanged: function(scope, row){
143+
},
131144
/**
132145
* @ngdoc event
133146
* @name rowExpandedStateChanged
@@ -136,10 +149,9 @@
136149
* <pre>
137150
* gridApi.expandable.on.rowExpandedStateChanged(scope,function(row){})
138151
* </pre>
152+
* @param {scope} scope the application scope
139153
* @param {GridRow} row the row that was expanded
140154
*/
141-
rowExpandedBeforeStateChanged: function(scope,row){
142-
},
143155
rowExpandedStateChanged: function (scope, row) {
144156
}
145157
}
@@ -270,15 +282,15 @@
270282

271283
if (row.isExpanded) {
272284
row.height = row.grid.options.rowHeight + row.expandedRowHeight;
273-
}
274-
else {
285+
grid.expandable.expandedAll = service.getExpandedRows(grid).length === grid.rows.length;
286+
} else {
275287
row.height = row.grid.options.rowHeight;
276288
grid.expandable.expandedAll = false;
277289
}
278290
grid.api.expandable.raise.rowExpandedStateChanged(row);
279291
},
280292

281-
expandAllRows: function(grid, $scope) {
293+
expandAllRows: function(grid) {
282294
grid.renderContainers.body.visibleRowCache.forEach( function(row) {
283295
if (!row.isExpanded && !(row.entity.subGridOptions && row.entity.subGridOptions.disableRowExpandable)) {
284296
service.toggleRowExpansion(grid, row);

src/features/expandable/test/expandable.spec.js

+66-8
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ describe('ui.grid.expandable', function() {
175175
grid.api.registerMethodsFromObject.and.callFake(function(publicMethods) {
176176
methods = publicMethods.expandable;
177177
});
178+
grid.api.registerEventsFromObject.and.callFake(function(events) {
179+
grid.api.expandable = {raise: events.expandable};
180+
});
178181
grid.rows = [
179182
{entity: {id: 1, name: 'John Doe'}, isExpanded: true},
180183
{entity: {id: 2, name: 'Joe Doe'}, isExpanded: false},
@@ -233,20 +236,64 @@ describe('ui.grid.expandable', function() {
233236
});
234237
describe('expandRow', function() {
235238
beforeEach(function() {
236-
grid.getRow.and.returnValue(null);
237-
methods.expandRow(grid.rows[1].entity);
239+
grid.getRow.and.callFake(function(rowEntity) {
240+
return rowEntity;
241+
});
242+
243+
grid.rows = [
244+
{isExpanded: false, grid: grid},
245+
{isExpanded: true, grid: grid},
246+
{isExpanded: true, grid: grid},
247+
{isExpanded: true, grid: grid},
248+
{isExpanded: true, grid: grid},
249+
{isExpanded: true, grid: grid}
250+
];
238251
});
239-
it('should call getRow on the grid with the row entity passed in', function() {
240-
expect(grid.getRow).toHaveBeenCalledWith(grid.rows[1].entity);
252+
it('should do nothing when the current row is expanded', function() {
253+
methods.expandRow(grid.rows[1]);
254+
255+
expect(grid.rows[1].isExpanded).toEqual(true);
256+
});
257+
it('should expand the current row if it is collapsed', function() {
258+
methods.expandRow(grid.rows[0]);
259+
260+
expect(grid.rows[0].isExpanded).toEqual(true);
261+
});
262+
it('should update the value of expandAll if all rows are expanded', function() {
263+
methods.expandRow(grid.rows[0]);
264+
265+
expect(grid.expandable.expandedAll).toEqual(true);
241266
});
242267
});
243268
describe('collapseRow', function() {
244269
beforeEach(function() {
245-
grid.getRow.and.returnValue(null);
246-
methods.collapseRow(grid.rows[0].entity);
270+
grid.getRow.and.callFake(function(rowEntity) {
271+
return rowEntity;
272+
});
273+
274+
grid.rows = [
275+
{isExpanded: false, grid: grid},
276+
{isExpanded: true, grid: grid},
277+
{isExpanded: true, grid: grid},
278+
{isExpanded: true, grid: grid},
279+
{isExpanded: true, grid: grid},
280+
{isExpanded: true, grid: grid}
281+
];
247282
});
248-
it('should call getRow on the grid with the row entity passed in', function() {
249-
expect(grid.getRow).toHaveBeenCalledWith(grid.rows[0].entity);
283+
it('should do nothing when the current row is collapsed', function() {
284+
methods.collapseRow(grid.rows[0]);
285+
286+
expect(grid.rows[0].isExpanded).toEqual(false);
287+
});
288+
it('should collapse the current row if it is expanded', function() {
289+
methods.collapseRow(grid.rows[1]);
290+
291+
expect(grid.rows[1].isExpanded).toEqual(false);
292+
});
293+
it('should update the value of expandAll if one row is collapsed', function() {
294+
methods.collapseRow(grid.rows[0]);
295+
296+
expect(grid.expandable.expandedAll).toEqual(false);
250297
});
251298
});
252299
describe('getExpandedRows', function() {
@@ -293,7 +340,18 @@ describe('ui.grid.expandable', function() {
293340
});
294341

295342
describe('getExpandedRows', function() {
343+
it('should return only the rows that are expanded', function() {
344+
grid.rows = [
345+
{isExpanded: false},
346+
{isExpanded: true},
347+
{isExpanded: false},
348+
{isExpanded: true},
349+
{isExpanded: true},
350+
{isExpanded: true}
351+
];
296352

353+
expect(uiGridExpandableService.getExpandedRows(grid).length).toEqual(4);
354+
});
297355
});
298356
});
299357
});

0 commit comments

Comments
 (0)