简体   繁体   English

Angularjs从1个选择框中按过去12个月的月份筛选

[英]Angularjs filter by month by the last 12 months from 1 select box

I have data that I wish to filter this is working fine with 2 separate inputs but I am trying to combine to a single select with the month and the year form the last 12 months. 我有一些数据希望通过2个单独的输入进行过滤,但是我想将最后12个月的月份和年份合并为一个选择。

these then populate a service call eg.: 这些然后填充服务调用,例如:

url.com?month=1&year=2015 url.com?month=1&year=2015

Currently I have the following 目前我有以下

var date = new Date();
var months = [],
monthNames = [ "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
for(var i = 0; i < 12; i++) {
    months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
    date.setMonth(date.getMonth() - 1);
}
$scope.months = months;

That populate the select box: 这将填充选择框:

   <select id="transMonth" name="transMonth" ng-model="month" class="form-control" ng-options="currMonth as currMonth for currMonth in months" ng-change="fetchTransactions()">
        <option value="">-- Please select --</option>
   </select>

This currently binds to a the single property 'month' with the value as month name year (eg January 2014) I need to split this and change to a numerical value ie 1 2014 当前绑定到单个属性“ month”,其值作为月份名称年份(例如2014年1月),我需要将其拆分并更改为数值,即1 2014

How do I go about this? 我该怎么办?

You could do something like this: 您可以执行以下操作:

JS: JS:

      var date = new Date();
      var months = [],
      monthNames = [ "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
      for(var i = 0; i < 12; i++) {
          months.push({value: monthNames[date.getMonth()] + ' ' + date.getFullYear(), id: {month: date.getMonth(), year: date.getFullYear()} });
          date.setMonth(date.getMonth() - 1);
      }
$scope.months = months;

HTML: HTML:

  <select id="transMonth" name="transMonth" ng-model="month" class="form-control" ng-options="currMonth.id as currMonth.value for currMonth in months" ng-change="fetchTransactions()">
        <option value="">-- Please select --</option>
   </select>
   {{month}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM