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