Skip to content

Commit 93b4846

Browse files
committed
add filter to dynamic table example
1 parent 83c572f commit 93b4846

File tree

7 files changed

+32
-10
lines changed

7 files changed

+32
-10
lines changed

examples/demo20.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ <h1>Dynamic columns</h1>
2222

2323
<div ng-controller="DemoCtrl">
2424

25+
<p><strong>Filter:</strong> {{tableParams.filter()|json}}
26+
2527
Columns:
2628
<label class="checkbox" ng-repeat="column in columns">
2729
<input type="checkbox" ng-model="column.visible" /> {{column.title}}
@@ -39,6 +41,18 @@ <h1>Dynamic columns</h1>
3941
<div>{{column.title}}</div>
4042
</th>
4143
</tr>
44+
<tr class="ng-table-filters" ng-init="tableParams">
45+
<th ng-repeat="column in columns" ng-show="column.visible" class="filter">
46+
<div ng-repeat="(name, filter) in column.filter">
47+
<div ng-if="column.filterTemplateURL" ng-show="column.filterTemplateURL">
48+
<div ng-include="column.filterTemplateURL"></div>
49+
</div>
50+
<div ng-if="!column.filterTemplateURL" ng-show="!column.filterTemplateURL">
51+
<div ng-include="'ng-table/filters/' + filter + '.html'"></div>
52+
</div>
53+
</div>
54+
</th>
55+
</tr>
4256
</thead>
4357
<tbody>
4458
<tr ng-repeat="user in $data">
@@ -87,6 +101,12 @@ <h1>Dynamic columns</h1>
87101
$filter('orderBy')(data, params.orderBy()) :
88102
data;
89103

104+
orderedData = params.filter() ?
105+
$filter('filter')(orderedData, params.filter()) :
106+
orderedData;
107+
108+
console.info(orderedData.length);
109+
params.total(orderedData.length);
90110
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
91111
}
92112
});

ng-table.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ app.factory('ngTableParams', ['$q', '$log', function ($q, $log) {
411411
self.data = settings.$scope.$data = data;
412412
}
413413
settings.$scope.pages = self.generatePagesArray(self.page(), self.total(), self.count());
414+
settings.$scope.$emit('ngTableAfterReloadData');
414415
});
415416
};
416417

@@ -461,7 +462,7 @@ app.factory('ngTableParams', ['$q', '$log', function ($q, $log) {
461462
* @description
462463
* Each {@link ngTable.directive:ngTable ngTable} directive creates an instance of `ngTableController`
463464
*/
464-
var ngTableController = ['$scope', 'ngTableParams', '$q', function ($scope, ngTableParams, $q) {
465+
var ngTableController = ['$scope', 'ngTableParams', '$timeout', function ($scope, ngTableParams, $timeout) {
465466
$scope.$loading = false;
466467

467468
if (!$scope.params) {
@@ -472,8 +473,8 @@ var ngTableController = ['$scope', 'ngTableParams', '$q', function ($scope, ngTa
472473
var delayFilter = (function () {
473474
var timer = 0;
474475
return function (callback, ms) {
475-
clearTimeout(timer);
476-
timer = setTimeout(callback, ms);
476+
$timeout.cancel(timer);
477+
timer = $timeout(callback, ms);
477478
};
478479
})();
479480

@@ -690,7 +691,7 @@ app.directive('ngTablePagination', ['$compile',
690691
replace: false,
691692
link: function (scope, element, attrs) {
692693

693-
scope.$watch('params.$params', function (newParams, oldParams) {
694+
scope.params.settings().$scope.$on('ngTableAfterReloadData', function () {
694695
scope.pages = scope.params.generatePagesArray(scope.params.page(), scope.params.total(), scope.params.count());
695696
}, true);
696697

ng-table.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ng-table.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scripts/03-params.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ app.factory('ngTableParams', ['$q', '$log', function ($q, $log) {
362362
self.data = settings.$scope.$data = data;
363363
}
364364
settings.$scope.pages = self.generatePagesArray(self.page(), self.total(), self.count());
365+
settings.$scope.$emit('ngTableAfterReloadData');
365366
});
366367
};
367368

src/scripts/04-controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @description
1414
* Each {@link ngTable.directive:ngTable ngTable} directive creates an instance of `ngTableController`
1515
*/
16-
var ngTableController = ['$scope', 'ngTableParams', '$q', function ($scope, ngTableParams, $q) {
16+
var ngTableController = ['$scope', 'ngTableParams', '$timeout', function ($scope, ngTableParams, $timeout) {
1717
$scope.$loading = false;
1818

1919
if (!$scope.params) {
@@ -24,8 +24,8 @@ var ngTableController = ['$scope', 'ngTableParams', '$q', function ($scope, ngTa
2424
var delayFilter = (function () {
2525
var timer = 0;
2626
return function (callback, ms) {
27-
clearTimeout(timer);
28-
timer = setTimeout(callback, ms);
27+
$timeout.cancel(timer);
28+
timer = $timeout(callback, ms);
2929
};
3030
})();
3131

src/scripts/06-pagination.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ app.directive('ngTablePagination', ['$compile',
2424
replace: false,
2525
link: function (scope, element, attrs) {
2626

27-
scope.$watch('params.$params', function (newParams, oldParams) {
27+
scope.params.settings().$scope.$on('ngTableAfterReloadData', function () {
2828
scope.pages = scope.params.generatePagesArray(scope.params.page(), scope.params.total(), scope.params.count());
2929
}, true);
3030

0 commit comments

Comments
 (0)