2016년 1월 27일 수요일

[AngularJS] filter using javascript


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
<!doctype html>
<html lng='utf-8'>
  <head>
    <meta charset="utf-8">
 
  </head>
  <body ng-app="memberApp">
    <div ng-controller="ListCtrl" class="panel panel-primary">
    <div class="panel-heading">User List </div>
        <h1>search한 내용들을 이용하여, paging</h1>
    <div class="panel-body" >
 
        <p>search :</p>
    <select ng-model='type' ng-change="changeSearch(type)">
      <option vlaue="id" >id</option>
      <option vlaue="name">name</option>
      <option vlaue="years">years</option>
    </select>
    <input type='text' ng-model='search[type]'/>
    <table class=table>
      <tr ng-repeat="member in members|paging:page:search">
        <td> {{member.id}} </th>
        <td> {{member.name}} </th>
        <td> {{member.tel}} </th>
      </tr>
    </table>
        <p>{{page}} page</p>
        Page Number <input type='text' ng-model='page' >
    </div>
    </div>  
    </div>
 
 
    <script src="http://code.angularjs.org/1.2.12/angular.min.js"></script>
    <script>
    var memberApp = angular.module("memberApp", []);
    memberApp.filter('paging'function($filter){
        //Get Filter Object
        //Filter using Javascript
      var filterObj = $filter("filter");
      return function(array, page, search) {
          //1. Array를 search 로 filtering 하고
        var resultArray = filterObj(array, search);
        var pageNum = page?(page -1* 100;
          //2. paging 한다.
        return resultArray.slice(pageNum, pageNum + 10);
      }
    });
    
    memberApp.controller("ListCtrl"function($scope){
        $scope.page =0;
      $scope.search = {"id":"""name":"""tel":"", $:""};
      $scope.members = [];
      for(var i = 0; i < 121; i++){
        $scope.members.push({"id":"id"+(i + 1),"name":"user"+(i+1), "tel":"123-00"+i});
      }
    });      
    </script>
  </body>
</html>
 
 
 
cs

댓글 없음:

댓글 쓰기