Skip to content

Commit

Permalink
refactor(filters): move responsibility for adding empty data item int…
Browse files Browse the repository at this point in the history
…o select.html template

Adding an empty option to the array returned by `$column.filterData` only makes sense when
using that array as the datasource for the `select.hmtl` template.

Therefore the responsibility for doing this should be moved into this template.

This allows the `$column.filterData`  to provide a suitable datasource for custom templates that
might not want an empty option.

This commit also fixes a problem where `$column.filterData` is used in more than one ngTable.
In this scenario duplicate empty items would be added to the array.

BREAKING CHANGE:

`ngTableController` no longer adds an empty item to the array returned by `$column.filterData`.

This will only affect those apps that were using `$column.filterData` to supply data to a *custom*
filter template.

Those apps that are using the `select.html` will be unaffected as the `select.html` filter will add
this empty item.
  • Loading branch information
ccrowhurstram committed Aug 15, 2015
1 parent c79fdd8 commit 6e1bd3d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/ng-table/filters/select.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<select ng-options="data.id as data.title for data in $column.data"
<select ng-options="data.id as data.title for data in $selectData"
ng-table-select-filter-ds="$column"
ng-disabled="$filterRow.disabled"
ng-model="params.filter()[name]"
class="filter filter-select form-control" name="{{name}}">
Expand Down
13 changes: 0 additions & 13 deletions src/scripts/ngTableController.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,15 @@
if (!angular.isArray(data) && !angular.isFunction(data) && !angular.isObject(data)) {
// if none of the above was found - we just want an empty array
data = [];
} else if (angular.isArray(data)) {
ensureEmptyListOption(data);
}
$column.data = data;
});
}
// otherwise, we just return what the user gave us. It could be a function, array, object, whatever
else {
ensureEmptyListOption(result);
return $column.data = result;
}
});

function ensureEmptyListOption(data){
if (!angular.isArray(data)){
return;
}
data.unshift({
title: '',
id: ''
});
}
};

this.buildColumns = function (columns) {
Expand Down

0 comments on commit 6e1bd3d

Please sign in to comment.