Skip to content

Commit be6fa19

Browse files
committed
fix(ngTableController): handle null columns array
Setting the columns array used by ngTableDynamic to null should not fail with a TypeError
1 parent 98592f6 commit be6fa19

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/scripts/ngTableController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140

141141
this.buildColumns = function (columns) {
142142
var result = [];
143-
columns.forEach(function(col){
143+
(columns || []).forEach(function(col){
144144
result.push(ngTableColumn.buildColumn(col, $scope, result));
145145
});
146146
return result

test/tableDynamicSpec.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ describe('ng-table-dynamic', function() {
196196
expect(angular.element(filterRow.find('th')[1]).find('select').length).toBe(1);
197197
}));
198198
});
199-
describe('add column', function(){
199+
describe('changing column list', function(){
200200
var elm;
201201
beforeEach(inject(function($compile, $q, NgTableParams) {
202202
elm = angular.element(
@@ -250,7 +250,7 @@ describe('ng-table-dynamic', function() {
250250
scope.$digest();
251251
}));
252252

253-
it('should create table header', function() {
253+
it('adding new column should update table header', function() {
254254
var newCol = {
255255
'class': 'moneyadd',
256256
field: 'money',
@@ -274,15 +274,49 @@ describe('ng-table-dynamic', function() {
274274
expect(angular.element(titles[1]).text().trim()).toBe('Age');
275275
expect(angular.element(titles[2]).text().trim()).toBe('Money');
276276

277-
expect(angular.element(rows[1]).hasClass('ng-table-filters')).toBeTruthy();
278-
var filters = angular.element(rows[1]).find('th');
277+
var filterRow = angular.element(rows[1]);
278+
expect(filterRow.hasClass('ng-table-filters')).toBeTruthy();
279+
expect(filterRow.hasClass("ng-hide")).toBe(false);
280+
281+
var filters = filterRow.find('th');
279282
expect(filters.length).toBe(3);
280283
expect(angular.element(filters[0]).hasClass('filter')).toBeTruthy();
281284
expect(angular.element(filters[1]).hasClass('filter')).toBeTruthy();
282285
expect(angular.element(filters[2]).hasClass('filter')).toBeTruthy();
283286

284287
});
285288

289+
it('removing new column should update table header', function() {
290+
scope.cols.splice(0, 1);
291+
scope.$digest();
292+
var thead = elm.find('thead');
293+
expect(thead.length).toBe(1);
294+
295+
var rows = thead.find('tr');
296+
var titles = angular.element(rows[0]).find('th');
297+
expect(titles.length).toBe(1);
298+
expect(angular.element(titles[0]).text().trim()).toBe('Age');
299+
300+
var filterRow = angular.element(rows[1]);
301+
expect(filterRow.hasClass("ng-hide")).toBe(true);
302+
});
303+
304+
it('setting columns to null should remove all table columns from header', function() {
305+
scope.cols = null;
306+
scope.$digest();
307+
var thead = elm.find('thead');
308+
expect(thead.length).toBe(1);
309+
310+
var rows = thead.find('tr');
311+
var titles = angular.element(rows[0]).find('th');
312+
expect(titles.length).toBe(0);
313+
314+
var filterRow = angular.element(rows[1]);
315+
expect(filterRow.hasClass("ng-hide")).toBe(true);
316+
317+
expect(filterRow.find('th').length).toBe(0);
318+
});
319+
286320
});
287321
describe('title-alt', function() {
288322

0 commit comments

Comments
 (0)