Skip to content

Commit

Permalink
feat(ngTableColumn): allow $column fields to be model bound as a gett…
Browse files Browse the repository at this point in the history
…er/setter's

You can now write code like `$column.show(true)` to set the value for the `$column` field.

**Tip:** Use the `ngTableColumnBindings` to obtain the array of `$column`'s. You can then use an
`ng-repeat` to bind each `$column` to the screen for manipulation.

Here's an example that creates a simple column picker:

```html
<div style="margin-bottom: 20px">
    <label class="checkbox-inline" ng-repeat="col in ctrl.columns">
        <input type="checkbox" ng-model-options="{ getterSetter: true }" ng-model="col.show"/> {{col.title()}}
    </label>
</div>

<table ng-table="ctrl.tableParams" class="table" export-csv="csv" ng-table-columns-binding="ctrl.columns">
    <tr ng-repeat="user in $data">
        <td data-title="'Name'" ng-if="true">
            {{user.name}}
        </td>
        <td data-title="'Age'" ng-if="true">
            {{user.age}}
        </td>
    </tr>
</table>
```
  • Loading branch information
ccrowhurstram committed Oct 3, 2015
1 parent 9ba4f47 commit e705fd9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/scripts/ngTable.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@
show: el.attr("ng-if") ? parsedAttribute('ng-if') : undefined
});

if (groupRow){
if (groupRow || el.attr("ng-if")){
// change ng-if to bind to our column definition which we know will be writable
// because this will potentially increase the $watch count, only do so when we definitely
// need to change visibility of the columns.
// currently only ngTableGroupRow directive needs this
// because this will potentially increase the $watch count, only do so if we already have an
// ng-if or when we definitely need to change visibility of the columns.
// currently only ngTableGroupRow directive needs to change visibility
setAttrValue('ng-if', '$columns[' + (columns.length - 1) + '].show(this)');
}
});
Expand Down
46 changes: 31 additions & 15 deletions src/scripts/ngTableColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,34 @@
// - note that the original column object is being "proxied"; this is important
// as it ensure that any changes to the original object will be returned by the "getter"
(function(prop1){
var getterFn = function () {
return column[prop1];
var getterSetter = function getterSetter(/*[value] || [$scope, locals]*/) {
if (arguments.length === 1 && !isScopeLike(arguments[0])) {
getterSetter.assign(null, arguments[0]);
} else {
return column[prop1];
}
};
getterFn.assign = function($scope, value){
getterSetter.assign = function($scope, value){
column[prop1] = value;
};
extendedCol[prop1] = getterFn;
extendedCol[prop1] = getterSetter;
})(prop);
}
(function(prop1){
// satisfy the arguments expected by the function returned by parsedAttribute in the ngTable directive
var getterFn = extendedCol[prop1];
extendedCol[prop1] = function () {
var scope = arguments[0] || defaultScope;
var context = Object.create(scope);
angular.extend(context, {
$column: extendedCol,
$columns: columns
});
return getterFn.call(column, context);
if (arguments.length === 1 && !isScopeLike(arguments[0])){
getterFn.assign(null, arguments[0]);
} else {
var scope = arguments[0] || defaultScope;
var context = Object.create(scope);
angular.extend(context, {
$column: extendedCol,
$columns: columns
});
return getterFn.call(column, context);
}
};
if (getterFn.assign){
extendedCol[prop1].assign = getterFn.assign;
Expand All @@ -93,13 +101,21 @@

function createGetterSetter(initialValue){
var value = initialValue;
var getter = function (/*$scope, locals*/){
return value;
var getterSetter = function getterSetter(/*[value] || [$scope, locals]*/){
if (arguments.length === 1 && !isScopeLike(arguments[0])) {
getterSetter.assign(null, arguments[0]);
} else {
return value;
}
};
getter.assign = function($scope, newValue){
getterSetter.assign = function($scope, newValue){
value = newValue;
};
return getter;
return getterSetter;
}

function isScopeLike(object){
return object != null && angular.isFunction(object.$new);
}
}]);
})();

0 comments on commit e705fd9

Please sign in to comment.