Skip to content

Commit

Permalink
feat(ngTable): getter methods declared on $column no longer require a…
Browse files Browse the repository at this point in the history
… $scope to be supplied

Previously you needed to supply a $scope when calling any of the getter methods on $column.
Now when a $scope is not supplied, the child $scope created for the directive will be assumed.
  • Loading branch information
ccrowhurstram committed Feb 11, 2015
1 parent afc1423 commit f9090b4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
19 changes: 16 additions & 3 deletions src/scripts/04-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ng
};

this.buildColumns = function (columns) {
return columns.map(ngTableColumn.buildColumn)
return columns.map(function(col){
return ngTableColumn.buildColumn(col, $scope)
})
};

this.setupBindingsToInternalScope = function(tableParamsExpr){
Expand Down Expand Up @@ -157,7 +159,7 @@ function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ng
};

$scope.sortBy = function(column, event) {
var parsedSortable = column.sortable && column.sortable($scope);
var parsedSortable = column.sortable && column.sortable();
if (!parsedSortable) {
return;
}
Expand Down Expand Up @@ -194,7 +196,7 @@ app.factory('ngTableColumn', [function () {
titleAlt: function(){ return ''; }
};

function buildColumn(column){
function buildColumn(column, defaultScope){
// note: we're not modifying the original column object. This helps to avoid unintended side affects
var extendedCol = Object.create(column);
for (var prop in defaults) {
Expand All @@ -212,6 +214,17 @@ app.factory('ngTableColumn', [function () {
};
})(prop);
}
(function(prop1){
// satisfy the arguments expected by the function returned by parsedAttribute in the ngTable directive
var getterFn = extendedCol[prop1];
extendedCol[prop1] = function(){
if (arguments.length === 0){
return getterFn.call(column, defaultScope);
} else {
return getterFn.apply(column, arguments);
}
};
})(prop);
}
return extendedCol;
}
Expand Down
24 changes: 17 additions & 7 deletions test/tableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,31 @@ describe('ng-table', function() {
'<div>' +
'<table ng-table="tableParams" show-filter="true">' +
'<tr ng-repeat="user in $data">' +
'<td data-header-title="\'Sort by Name\'" data-title="\'Name of person\'" filter="{ \'name\': \'text\' }" sortable="\'name\'" data-header-class="getCustomClass(column)">' +
'<td data-header-title="\'Sort by Name\'" data-title="nameTitle()" filter="{ \'name\': \'text\' }" sortable="\'name\'" data-header-class="getCustomClass(column)">' +
'{{user.name}}' +
'</td>' +
'<td x-data-header-title="\'Sort by Age\'" x-data-title="\'Age\'" sortable="\'age\'" x-data-header-class="getCustomClass(column)">' +
'<td x-data-header-title="\'Sort by Age\'" x-data-title="ageTitle()" sortable="\'age\'" x-data-header-class="getCustomClass(column)">' +
'{{user.age}}' +
'</td>' +
'<td header-title="\'Sort by Money\'" title="\'Money\'" filter="{ \'action\': \'select\' }" filter-data="money($column)" header-class="getCustomClass(column)">' +
'<td header-title="\'Sort by Money\'" title="moneyTitle()" filter="{ \'action\': \'select\' }" filter-data="money($column)" header-class="getCustomClass(column)">' +
'{{user.money}}' +
'</td>' +
'</tr>' +
'</table>' +
'</div>');

scope.nameTitle = function(){
return 'Name of person';
};
scope.ageTitle = function(){
return 'Age';
};
scope.moneyTitle = function(){
return 'Money';
};

scope.getCustomClass = function(column){
if (column.title(scope).indexOf('Money') !== -1){
if (column.title().indexOf('Money') !== -1){
return 'moneyHeaderClass';
} else{
return 'customClass';
Expand Down Expand Up @@ -305,7 +315,7 @@ describe('ng-table', function() {
it('should make filter def available on $column', function () {
expect(columnDef).toBeDefined();
expect(columnDef.filter).toBeDefined();
expect(columnDef.filter(scope)['username']).toBe('text');
expect(columnDef.filter()['username']).toBe('text');
});
});

Expand Down Expand Up @@ -381,8 +391,8 @@ describe('ng-table', function() {
it('should make filter def available on $column', function () {
expect(columnDef).toBeDefined();
expect(columnDef.filter).toBeDefined();
expect(columnDef.filter(scope)['name']).toBe('text');
expect(columnDef.filter(scope)['age']).toBe('text');
expect(columnDef.filter()['name']).toBe('text');
expect(columnDef.filter()['age']).toBe('text');
});
});
describe('dynamic filter', function(){
Expand Down

0 comments on commit f9090b4

Please sign in to comment.