简体   繁体   中英

Angular group by ng-repeat filter

I am using this library : https://github.com/a8m/angular-filter#groupby

So I can group list items, but my problem is a little different,

Suppose that this is my data set:

[
    {name: 'foo', date: '2015-01-01'},
    {name: 'bar', date: '2015-01-04'},
    {name: 'bazz', date: '2015-02-22'},
    {name: 'ahh', date: '2015-06-15'},
    {name: 'qwe', date: '2015-06-16'},
]

I want to filter it by month, and year and the library doesn't suffice what I need.

<li ng-repeat="(key,value) in items | groupBy: 'date'">
    <h1><--MONTH--></h1>
    <p>{{value.details}}</p>
</li>

I think you can convert the date string into date parts like day, month and year and then pass the nested property to groupBy filter.

Convert data to this.

[
    {name: 'foo', date: {day: 1, month: 'January', year: 2015 }
    {name: 'foo', date: {day: 1, month: 'February', year: 2015 }
    ..
    ..
]

You can convert the date string into date parts using the below logic

var data = [....]
var monthNames = ["January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
    ];
angular.forEach(data, function (d) {
  d.date = new Date(d.date);
  d.date = {
    d: dt.getDate(),
    m: monthNames[d.date.getMonth()],
    y: d.date.getFullYear() 
  };
})

Html

<li ng-repeat="(key,value) in items | groupBy: 'date.month'">
    <h1><--MONTH--></h1>
    <p>{{value.details}}</p>
</li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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