Skip to main content

Auto generating the year in select using ngOption

While creating an angular form, we come upon a situation where we required to generate the sequence of years in the select HTML tag. There is some way to perform this task, but each has its limitation. Let's look the way to generate the years :
  • Hard code an array having years in your controller and assign it to some $scope variable.
  • Use for loop to generate the years and then assign to $scope variable.
  • Send $http request to generate the array of year and assign the response data to $scope variable.
  • Define a filter that will be used to generate the sequence of years.
As the first and second method makes our controller dirty and the third method causes HTTP request that will overhead server, so we are going to see the fourth method to generate the years as it has several advantages over other three methods. So let's jump into the code:

HTML Code:

<form name="myform">
  <select ng-options="year for year in [] | range: '2000':'2017'" ng-model="user.year" name="year" id="year" class="form-control"></select>
</form>
Javascript Code:

app.filter('range', function() {
  return function(input, start, end) {  
   if(start == '') {
    start = (new Date()).getFullYear();
   }
   if(end == '') {
    end = (new Date()).getFullYear();
   }
    start = parseInt(start);
    end = parseInt(end);
    var direction = (start <= end) ? 1 : -1;
    end = (direction == 1) ? (end+1) : (end-1);
    while (start != end) {
        input.push(start);
        start += direction;
    }
    return input;
  };
});
How to use:
  1. Generating years in ascending order:
    
    ng-options = "year for year in [] | range: '2000':'2018'"
    
  2. Generating years in descending order:
    
    ng-options = "year for year in [] | range: '2018':'2000'"
    
  3. Generating years from given year to current year:
    
    ng-options = "year for year in [] | range: '2000':''"
    
  4. Generating years from current year to given year:
    
    ng-options = "year for year in [] | range: '':'2000'"
    
Working Examples:


Comments

Post a Comment

Popular posts from this blog

Automatic Slashing the Date Input using Angularjs

We define a directive that will automatically slash the input field as user typein the date. The directive have following capabiltiy: The directive will append slash '/' when input field has 2 or 5 characters. The input will accept only 10 characters (dd/mm/yyyy). Validity of date provide will not be checked. When the backspace is pressed it will remove the last two character if last character is slash. Otherwise it will remove last character from the input field. The input will not accept characters other than numbers (0 to 9). Let's jump into the code. We will use bootstrap for awesome look: HTML Code: <div class="container" ng-app="myApp" ng-controller="myAppCtrl"> <div class="row"> <div class="col-sm-8 col-sm-offset-2"> <form class="form-horizontal"> <h4 class="bg-primary">Auto Slashing Date using Angularjs Directive</h4> <div class=...

Code Syntax Highlighting on Blogger using Prismjs

Prismjs is developed by Lea Verou. It is a lightweight, extensible syntax highlighter, built with modern web standards in mind. It’s used in thousands of websites, including some of those you visit daily.Today, we will see how to use the prismjs on blogger to highlight our codes. It very easy and step by step process to follow: Goto to this link and get the personalized links for prismjs or simply copy the links given below: <link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.6.0/themes/prism-coy.min.css" rel="stylesheet"></link> <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.0/prism.min.js"></script> Goto Theme > Edit HTML and paste the links before the closing </head> section as shown below: Default prism.min.js file contain the functionality to highlight the code for only HTML, CSS and JS code. If you wish to use other language to highlight, you have to paste the link for that langu...

Conversion between Date String and Timestamp using Angularjs Directive

Let me ask you one question. Can a veiwValue and modelValue have same value into a different format? The answer is YES . Here we will see how it can be possible. We will see it through a simple example that converts date string (viewValue) into timestamp (modelValue). We will use a text box to enter a date string (dd/mm/yyyy) and convert it into timestamp that will be stored into model value. The trick is that we will use $parsers to convert the date string into the timestamp and $formatters to change the timestamp stored in modelValue to date string that will be shown on text input. Let's see the codes first: HTML Code: <h4>Date to Timestamp</h4> <div class="form-group"> <label for="date1" class="col-sm-3 control-label">Date1 VeiwValue:</label> <div class="col-sm-3"> <input type="text" id="date1" class="form-control" name="date1" ng-model="ob...