2016년 1월 26일 화요일

[AngularjS] Multi Filter



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
<!doctype html>
<html ng-app lng='utf-8'>
<head>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
</head>
<body>
<div ng-controller="Ctrl">
    <!-- filter Date -->
    <h2>filter Date example </h2>
    <p> {{regDate | date:'yyyy-MM-dd'}}</p>
    <p> {{regDate | date:'yyyy-MM-dd HH:mm:ss'}}</p>
    <p> medium {{regDate | date:'medium'}}</p>
    <p> short {{regDate | date:'short'}}</p>
    <p> {{regDate | date:'y'}}</p>
    <!-- filter number -->
    <h2>filter number example </h2>
    <p> {{point|number}} </p>
    <p> {{point|number: 0}} </p>
    <p> {{point|number: 2}} </p>
    <!-- orderBy:["years","-name"]' -->
    <h2>orderBy_Filter example </h2>
    Filter : <input type='text' ng-model='keyword'>
    <table class=table>
        <tr>
            <th ng-click='criteria ="id"'> id </th>
            <th ng-click='criteria ="name"'> name </th>
            <th ng-click='criteria ="years"'> year </th>
        </tr>
        <!-- <tr ng-repeat='member in members|orderBy:["years","-name"]' >-->
        <tr ng-repeat='member in members | orderBy:criteria | filter:keyword:check' >
            <td> {{member.id}} </td>
            <td > {{member.name}} </td>
            <td> {{member.years}} </td>
        </tr>
    </table>
    <h2>Multi filter using Object </h2>
    <!-- filter의 경우 $를 사용하면 객체형태로 사용되어 유용하다-->
    id : <input type='text' ng-model='keyword.id'>
    name : <input type='text' ng-model='keyword.name'>
    year : <input type='text' ng-model='keyword.years'>
    <table class=table>
        <tr>
            <th ng-click='criteria ="id"'> id </th>
            <th ng-click='criteria ="name"'> name </th>
            <th ng-click='criteria ="years"'> year </th>
        </tr>
        <!-- <tr ng-repeat='member in members|orderBy:["years","-name"]' >-->
        <tr ng-repeat='member in members | orderBy:criteria | filter:keyword:check' >
            <td> {{member.id}} </td>
            <td > {{member.name}} </td>
            <td> {{member.years}} </td>
        </tr>
    </table>
</div>
<script src="http://code.angularjs.org/1.2.12/angular.min.js"></script>
<script>
    function Ctrl($scope){
        $scope.regDate = 1288323623006;
        $scope.point =  1234.56789;
        $scope.members = [
            {"id":"user00""name":"AAB""years""52"},
            {"id":"user01""name":"BB1""years""46"},
            {"id":"user02""name":"BB2""years""1"},
            {"id":"user03""name":"CCC""years""2"},
            {"id":"user04""name":"CCD""years""2"},
            {"id":"user05""name":"FFF""years""32"},
            {"id":"user06""name":"GGG""years""2"}
        ];
        $scope.criteria= 0;
        $scope.check = function(actual, expected){
            return actual.toLowerCase().indexOf(expected.toLowerCase()) >= 0 ? truefalse;
        }
    }
</script>
</body>
</html>
cs

댓글 없음:

댓글 쓰기