1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
<!DOCTYPE html>
<html>
<head>
<title>Filters in Action</title>
<script src="../../tp/angular.js"></script>
</head>
<body ng-app="filtersApp">
<div ng-controller="FilterCtrl as ctrl">
<table>
<tr>
<td>
<button ng-click="ctrl.currentFilter = 'string'">
Filter with string
</button>
</td>
<td>
Filter Text
<input type="text"
ng-model="ctrl.filterOptions['string']"/>
</td>
</tr>
<tr>
<td>
<button ng-click="ctrl.currentFilter = 'object'">
Filter with object
</button>
</td>
<td>
Show Done or Not Done
<input type="checkbox"
ng-model="ctrl.filterOptions['object'].done"/>
</td>
</tr>
<tr>
<td>
<button ng-click="ctrl.currentFilter = 'function'">
Filter with function
</button>
</td>
</tr>
</table>
<ul>
<li ng-repeat="note in ctrl.notes |
filter:ctrl.filterOptions[ctrl.currentFilter] |
orderBy:ctrl.sortOrder |
limitTo:5">
{{note.label}} - {{note.type}} - {{note.done}}
</li>
</ul>
</div>
<script type="text/javascript">
angular.module('filtersApp', [])
.controller('FilterCtrl', [function () {
var self = this;
self.notes = [
{label: 'FC Todo', type: 'chore', done: false},
{label: 'FT Todo', type: 'task', done: false},
{label: 'FF Todo', type: 'fun', done: true},
{label: 'SC Todo', type: 'chore', done: false},
{label: 'ST Todo', type: 'task', done: true},
{label: 'SF Todo', type: 'fun', done: true},
{label: 'TC Todo', type: 'chore', done: false},
{label: 'TT Todo', type: 'task', done: false},
{label: 'TF Todo', type: 'fun', done: false}
];
self.sortOrder = ['+type', '-label'];
self.filterOptions = {
'string': '',
'object': {done: false, label: 'F'},
'function': function (note) {
return note.type === 'task' && note.done === false;
}
};
self.currentFilter = 'string';
}]);
</script>
</body>
</html>
|