25 February 2018

AngularJS: Sắp xếp dữ liệu OrderBy AngularJS


OrderBy
app.js
AngularJS
(function () {
   var app = angular.module('app', []);

   app.controller('filterOrderByCtrl', filterOrderByCtrl);

   filterOrderByCtrl.$inject = ['$scope'];

   function filterOrderByCtrl($scope) {
      var employees = [{
            name: 'Hải',
            birthDate: new Date('12/3/1989'),
            salary: 15000000,
            gender: 'Male',
            status: true
         },
         {
            name: 'Hoàng',
            birthDate: new Date('6/15/1990'),
            salary: 12000000,
            gender: 'Male',
            status: true
         },
         {
            name: 'Long',
            birthDate: new Date('1/14/1992'),
            salary: 17000000,
            gender: 'Male',
            status: false
         },
         {
            name: 'Trung',
            birthDate: new Date('12/3/1993'),
            salary: 12000000,
            gender: 'Male',
            status: true
         },
         {
            name: 'Hương',
            birthDate: new Date('12/3/1995'),
            salary: 11000000,
            gender: 'Female',
            status: false
         },
         {
            name: 'Thủy',
            birthDate: new Date('12/3/1988'),
            salary: 10000000,
            gender: 'Female',
            status: true
         }
      ];
      $scope.employees = employees;
      $scope.sortColumn = 'name';
   }

   app.filter('status', function () {
      return function (input) {
         if (input == true)
            return 'Kích hoạt';
         else
            return 'Khóa';
      }
   });
})()
apps.js
AngularJS
<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <meta charset="utf-8" />
      <!-- Latest compiled and minified CSS -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
      <!-- Optional theme -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
      <script src="../srcipts/angular.js"></script>
      <script src="/session9/app.js"></script>
   </head>
   <body ng-app="app">
      <div ng-controller="filterOrderByCtrl">
         Show: 
         <select ng-model="sortColumn">
            <option value="name">Name ASC</option>
            <option value="birthDate">Date of Birth ASC</option>
            <option value="name">Salary ASC</option>
            <option value="-name">Name DESC</option>
            <option value="-birthDate">Date of Birth DESC</option>
            <option value="-salary">Salary DESC</option>
         </select>
         <table class="table">
            <tr>
               <td>Name</td>
               <td>Date of Birth</td>
               <td>Salary</td>
               <td>Gender</td>
               <td>Status</td>
            </tr>
            <tr ng-repeat="employee in employees | orderBy:sortColumn">
               <td>{{employee.name | uppercase}}</td>
               <td>{{employee.birthDate | date:"dd/MM/yyyy"}}</td>
               <td>{{employee.salary | currency:""}}</td>
               <td>{{employee.gender | lowercase}}</td>
               <td>{{employee.status | status}}</td>
            </tr>
         </table>
      </div>
   </body>
</html>
Nguồn TEDU

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang