Filter by angularjs ng-repeat date

0

I have the following array :

$scope.array = [
    {'fecha': "2017-01-01",
    "texto": "luis"},
    {'fecha': "2017-01-01",
    "texto": "jorge"},
    {'fecha': "2017-01-01",
    "texto": "daniel"},
    {'fecha': "2017-01-02",
    "texto": "pedro"},
    {'fecha': "2017-01-03",
    "texto": "crhis"},
    ];

which in my view html I show it in the following way:

<ul ng-repeat="i in array">
    <li>{{i.fecha}} {{i.texto}}</li>
 </ul>

but the result of this is:

2017-01-01 luis
2017-01-01 jorge
2017-01-01 daniel
2017-01-02 pedro
2017-01-03 crhis

My question is how can I filter by date, meaning that the result is as follows:

2017-01-01 -----> luis jorge daniel
2017-01-02 -----> pedro
2017-01-03 -----> crhis

itself what I want to know is how to do the filtering by date and that the date is displayed only once together with your data.

    
asked by Dimoreno 06.01.2017 в 08:09
source

2 answers

1

You could use the filter groupBy angular-filter

You add the reference to angular-filter.min.js and the module as it explains the title Get Started

Then you'll see the example groupBy solve exactly what you're looking for

$scope.players = [
  {name: 'Gene', team: 'alpha'},
  {name: 'George', team: 'beta'},
  {name: 'Steve', team: 'gamma'},
  {name: 'Paula', team: 'beta'},
  {name: 'Scruath', team: 'gamma'}
];

<ul>
  <li ng-repeat="(key, value) in players | groupBy: 'team'">
    Group name: {{ key }}
    <ul>
      <li ng-repeat="player in value">
        player: {{ player.name }}
      </li>
    </ul>
  </li>
</ul>
    
answered by 06.01.2017 / 11:56
source
1

I leave the code working for you, the secret is in the lib. angular filter, let it run and test it.

<html lang="en-US">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.14/angular-filter.min.js"></script>
<body ng-app="myApp">
<div  ng-controller="myCtrl">
<ul>
  <li ng-repeat="(key, value) in array | groupBy: 'fecha'">
    Fecha: {{ key }}
    <ul>
      <li ng-repeat="person in value">
         {{ person.texto }}
      </li>
    </ul>
  </li>
</ul>
</div>

<script>
var app = angular.module("myApp", ['angular.filter']);
app.controller("myCtrl", function($scope) {
$scope.array = [
    {'fecha': "2017-01-01",
    "texto": "luis"},
    {'fecha': "2017-01-01",
    "texto": "jorge"},
    {'fecha': "2017-01-01",
    "texto": "daniel"},
    {'fecha': "2017-01-02",
    "texto": "pedro"},
    {'fecha': "2017-01-03",
    "texto": "crhis"},
    ];
});
</script>

Greetings

    
answered by 06.01.2017 в 12:53