基于按钮点击的当前日期 - 排序json数据

时间:2016-01-29 07:54:31

标签: angularjs json

我正在使用angularJs为IOS和Android开发混合移动应用程序。我得到的JSON数据如下:

[{"EventId":101,"Title":"No title","Date":"9/8/2015","Time":"12:00 AM","Location":""},{"EventId":120,"Title":"My First Event","Date":"9/15/2015","Time":"12:00 AM","Location":""}]

我的问题是我有一个按钮,点击该按钮我需要根据当前日期缩短数据。如果我点击一个按钮,它应该只显示与当前日期匹配的记录。

我尝试按照link进行操作,但它无效。

代码:

 $scope.Schedule = [{"EventId":101,"Title":"No title","Date":"9/8/2015","Time":"12:00 AM","Location":""},{"EventId":120,"Title":"My First Event","Date":"9/15/2015","Time":"12:00 AM","Location":""}];

  <ion-content>   
        <ul ng-repeat="member in Schedule">
            <li class="item">
                <div class="item" style="border-width: 0px; padding : 1px;">{{member.Title}}</div> 
                <div class="item" style="border-width: 0px; padding : 1px;">{{member.Location}}</div>
                <div class="item" style="border-width: 0px; padding : 1px;">{{member.Time}}{{member.Date}}</div> 
            </li>  
        </ul>  
    </ion-content>
    <ion-footer-bar align-title="left" class="bar-assertive">  
    <div class="list"> 
        <a ng-click="setOrder()"> Show today's </a>
    </div>
    </ion-footer-bar>

3 个答案:

答案 0 :(得分:1)

这里是working plnkr

<强>标记

<button ng-show="curDate == ''" type="button" ng-click="filterByCurDate()">Filter By Current Date</button>
<button ng-hide="curDate == ''" type="button" ng-click="curDate = ''">Clear Filter</button>
<table>
  <thead>
    <tr>
      <th>EventId</th>
      <th>Title</th>
      <th>Date</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="event in events | filter: {Date: curDate}">
      <td>{{event.EventId}}</td>
      <td>{{event.Title}}</td>
      <td>{{event.Date}}</td>
    </tr>
  </tbody>
</table>

控制器代码

$scope.curDate = '';
$scope.filterByCurDate = function() {
  var now = new Date();
  $scope.curDate = $filter('date')(now, 'M/dd/yyyy');
};

$scope.events = [...];

答案 1 :(得分:0)

如果您想根据日期字段订购记录,请使用 orderBy 谓词代替过滤

Asc 订单

<ANY ng-repeat="record in records | orderBy:'Date'">..</ANY>

Desc 订单

<ANY ng-repeat="record in records | orderBy:'-Date'">..</ANY>

Angular DOC: orderBy

答案 2 :(得分:0)

您可以尝试这样的事情:

  var today = new Date(),
  jsonData = JSON.parse('[{"EventId":101,"Title":"No title","Date":"9/8/2015","Time":"12:00 AM","Location":""},{"EventId":120,"Title":"My First Event","Date":"1/29/2016","Time":"12:00 AM","Location":""}]');

  today.setHours(0,0,0,0); //we don't need time to compare

  jsonData.forEach(function(entry) {
    entry.Date = new Date(entry.Date);

    if (entry.Date.valueOf()===today.valueOf()){
      console.log("Pass: "+entry);
    }
  });