2016年9月29日 星期四

[AngularJS] $filter

 AngularJS    filter    ngOptions  


Introduction

$filter is one of the most useful service in angular. There some filter components already defined in angular. In this sample, I would like to show how to use $filter for displaying different options of ngOptions which depends on another directive.


Environment

   AngularJS 1.5.8
 Sweet Alert 1.1.3




Implement


Goal

We will initialize all the data for ngOptions in the following drop-down-boxes.
1. Country
2. City
3. Gender

However, the options of City and Gender will be changed due to the selected value of Country. We will get this done by using $filter.


 


JS : Controller

angular.module('app', [])
  .factory('optionFactory', function () {
      var factory = [];

      factory.getGenders = function () {

          var data = [{
              'id': 1,
              'nameEN': 'male',
              'nameZH': '',
              'nameJP': 'マン'
          }, {
              'id': 2,
              'nameEN': 'female',
              'nameZH': '',
              'nameJP': 'おんな'
          }];
          return data;

      };

      factory.getCountries = function () {

          var data = [{'id': 1,'name': 'Taiwan','local': 'ZH'}, {'id': 2,'name': 'American','local': 'EN'}, {'id': 3,'name': 'Japan','local': 'JP'}];
          return data;

      }

      factory.getCities = function () {

          var data = [{'id': 1,'name': '台北(Taipei)','countryId': 1}, {'id': 2,'name': '台南(Tainan)','countryId': 1}, {'id': 3,'name': 'New York','countryId': 2}, {'id': 4, 'name': 'Chicago','countryId': 2}, {'id': 5,'name': 'Houston','countryId': 2}, {'id': 6, 'name': 'とうきょうと(Tokyo)','countryId': 3}, {'id': 7,'name': 'きょうとふ(Kyoto)', 'countryId': 3}];
          return data;

      }

      return factory;

  })
  .controller('DemoCtrl', function ($scope, $filter, optionFactory) {
      $scope.genderOptions = optionFactory.getGenders();
      $scope.countryOptions = optionFactory.getCountries();
      $scope.cityOptions = optionFactory.getCities();

      $scope.showResult = function () {
          //Show something…
      }
  })


Okay, the above codes will result in showing all the options in drop-down-box: City, and the default options of Gender are always displayed with English.

Let’s start to update the codes with $filter!


Filter for City

Create filter

angular.module('app', [])
  .filter('cityFilter', function () {
      return function (cities, selectedCountryId) {

          var filtered = [];

          angular.forEach(cities, function (city) {
              if (city.countryId === selectedCountryId) {
                  filtered.push(city);
              }
          });

          return filtered;
      };
  })


Put filter to ngOptions

<select ng-options="city.name for city in (cityOptions | cityFilter:my.country.id)" ng-model="my.city" />


So the filter will ignore the cities which not belong to the selected country!




Filter for Gender

Another use of Filter is changing the display value from original value.
In this sample, we want to show the different localization wording for gender options.
Fortunately, we had kept the Traditional Chinese, English and Japanese wordings in the original gender array.

var data = [{'id': 1,'nameEN': 'male','nameZH': '','nameJP': 'マン'}, {'id': 2,'nameEN': 'female', 'nameZH': '','nameJP': 'おんな'}];

So all we have to do is passing the selected country’s corresponding localization to Filter, and let the filter return the correct wording for us!

Create filter

angular.module('app', [])
  .filter('genderFilter', function () {
      return function (gender, local) {
          var converted = "";
          switch (local) {
              case "ZH":
                  converted = gender.nameZH;
                  break;
              case "JP":
                  converted = gender.nameJP;
                  break;
              case "ZH":
              default:
                  converted = gender.nameEN;
                  break;
          }
          return converted;
      }
  })


Put filter to ngOptions

<select ng-options="gender as (gender|genderFilter:my.country.local)  for gender  in genderOptions" ng-model="my.gender" />


We used the benefit of “select as …” and put the filter inside for returning the corresponding wording of gender.



Final result























Reference


2016年9月26日 星期一

[AngularJS] Sequence async requests in loop

 AngularJS    javascript    asyc callback   sequence request


Introduction


Though async calls (ajax) and callbacks are most useful way to implement front-end with javascript. We sometimes have to make the async function running as sequential function.
For example, we have an array with customers’ id, and we are going to get the customers’ information one by one from backend thru ajax.
There are several ways to complete this requirement, we can get the data async asynchronously and sort the results based on the order of the original array. However, in the following sample, I will try to implement it by sending sequence requests.

Environment

l   AngularJS 1.5.8




Implement


Goal

We have an array with the customers’ ids, and the final result will be displaying the customers’ information thru async inquiry function but in the same order of the original id array. Futhermore, we want to summarize all the cash they have AFTER all the customers’ information are responsed.


 


Step 1 : async calls  (Sample code)

In step1, we will ignore the order of the result and just shows the customers’ information thru async requests.

First, we simulate an async function for customer-information inquery with different response time.

JS - service
angular.module('app', [])
  .service('getCustomers', function ($q, $timeout) {
      var data = [{
          'id': '1',
          'name': 'JB',
          'phone': '0933XXXXXX',
          'cash': 100
      }, {
          'id': '2',
          'name': 'Lily',
          'phone': '0910YYYYYY',
          'cash': 200
      }, {
          'id': '3',
          'name': 'Leia',
          'phone': '0982ZZZZZZ',
          'cash': 300
      }, {
          'id': '4',
          'name': 'hachi',
          'phone': '0955ZZZZZZ',
          'cash': 400
      }];

      var getdata = function (id) {

          var deferred = $q.defer();
          try {
              var randomTimeout = getRandomInt(1000, 3000); //The response time will be during 1sec-3sec
              $timeout(function () {
                  for (var i = 0; i < data.length; i++) {
                      var item = data[i];
                      if (item.id === id) {
                          deferred.resolve(item);
                          break;
                      }
                  }

              }, randomTimeout);
          } catch (err) {
              deferred.reject(err);
          }

          return deferred.promise;
      }

      function getRandomInt(min, max) {
          return Math.floor(Math.random() * (max - min + 1)) + min;
      }

      return getdata;
  })

JS – controller
angular.module('app', [])
  .controller('DemoCtrl', function ($scope, $q, getCustomers) {
      $scope.customers = [];
      $scope.cashSum = 0;
      var searchIds = ['3', '4', '1', '2'];
      angular.forEach(searchIds, function (id) {

          var promise = getCustomers(id);
          promise.then((rtn) => {
              $scope.customers.push(rtn);
              $scope.cashSum += rtn.cash;
          });
      })
  })


HTML
<div ng-app='app' ng-controller='DemoCtrl'>
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Phone</th>
                <th>Cash</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="cust in customers">
                <td>{{cust.id}}</td>
                <td>{{cust.name}}</td>
                <td>{{cust.phone}}</td>
                <td>{{cust.cash}}</td>
            </tr>
            <tr>
                <td colspan=3>總金額</td>
                <td>{{cashSum}}</td>
            <tr>
        </tbody>
        <tr>
    </table>
</div>

Okay, it’s done the final result will be like the following snapshot. The order of the results will depends on the resposne time of each async call and the cash summary is called after each response.

 


Step 2 : Summarize after all customers’ requests response (Sample code)

In this step, we will use $q.all to make sure that all the requests are back and then trigger the summarize callback.

JS - controller
angular.module('app', [])
  .controller('DemoCtrl', function ($scope, $q, $timeout, getCustomers) {
      $scope.customers = [];
      $scope.cashSum = 0;
      var searchIds = ['3', '4', '1', '2'];

      var promiseCollection = [];
      angular.forEach(searchIds, function (id) {

          var promise = getCustomers(id);
          promiseCollection.push(promise);

          promise.then((rtn) => {
              $scope.customers.push(rtn);
          });
      })

      $q.all(promiseCollection).then(() => {
              angular.forEach($scope.customers, function (cust) {
                  $scope.cashSum += cust.cash;
              })
      })

  })

Result :

 


Final step : Sequence reuquests (Sample code)

Here is the sample code of using recursive function for making sequential requests in a for-loop.

JS – recursive sample
var index = 0;
next();

function next() {
    if (index < searchIds.length) {
        var promise = getCustomers(searchIds[index]);
        promise.then((rtn) => {
            $scope.customers.push(rtn);
            index++;
            next();
        });
    }
}

This is a smart way by using recursive method and callback to make the request be sent one by one (sequentially).


Now we can use the above method to modify our js:controller.

JS – controller
angular.module('app', [])
  .controller('DemoCtrl', function ($scope, $q, $timeout, getCustomers, seqSearch) {
      $scope.customers = [];
      $scope.cashSum = 0;
      var searchIds = ['3', '4', '1', '2'];

      var deferred = $q.defer();
      var index = 0;
      next();

      function next() {
          if (index < searchIds.length) {
              var promise = getCustomers(searchIds[index]);
              promise.then((rtn) => {
                  $scope.customers.push(rtn);
                  index++;
                  next();
              });
          }
          else {
              deferred.resolve();
          }
      }

      deferred.promise.then(() => {
              angular.forEach($scope.customers, function (cust) {
                  $scope.cashSum += cust.cash;
              })
      })

  })

Notice that we don’t need to use $q.all to wait all the customers’ information requests because they are now sequence requests. Instead, we use a deferred ($q.defer) to promise that the recursive function is finished and then summarize the cash.

Final result is as following. The order of the result now is based on the order of the customers’ id array. Cheeerrrrs!


 




Reference