angular-filter icon indicating copy to clipboard operation
angular-filter copied to clipboard

Using groupBy with date part

Open gbergamo opened this issue 9 years ago • 10 comments

Hi.. I cannot find a way to group by my array with a date part (dd/MM/yyyy). I have to group by day/month/year of a date property, how can I do it?

thanks!

gbergamo avatar Jun 17 '16 17:06 gbergamo

What's your data structure?

rafaelss95 avatar Jun 19 '16 01:06 rafaelss95

Messages = [ { id: 1, sent: Date(), message: "" }, { id: 1, sent: Date(), message: "" } ]

It's something like that... 'Sent' is timespam I want to group messages by MM/dd/yyyy

Thanks

gbergamo avatar Jun 20 '16 12:06 gbergamo

Well, I had a problem with groupBy with dates because my dates were formatted as timestamp, so I changed the format to MM/dd/yyyy before render the view, then it worked. You can format them both server-side as client-side.

rafaelss95 avatar Jun 21 '16 03:06 rafaelss95

To resolve that I use map function to before groupBy, so, on map function I create a new formatted property (MM/dd/yyyy). It works, but I don't know if that is the better way to do that. (honestly, I didn't like the solution)

ng-repeat="(key, value) in mensagens | map: dateFormatter | groupBy: 'dateFormatted'"

gbergamo avatar Jun 21 '16 12:06 gbergamo

i'm using the same solution but I assume it has performance issues with big lists :(

codekraft-studio avatar Apr 21 '17 14:04 codekraft-studio

Above solution proposed by @rafaelss95 can work if I want to group by date. But I have a requirement to group by Date as well as time (complete timestamp), and in such cases groupBy is misbehaving. It would be great if someone would investigate the issue.

karandesai28 avatar Jun 01 '17 03:06 karandesai28

@karandesai28 try to change the date format to something (for example) like mysql datetime: YYYY-MM-DD HH:MM:SS so the ~orderBy~ groupBy compare function will compare the full timestamp instead to compare only the date, I guess.

codekraft-studio avatar Jun 01 '17 06:06 codekraft-studio

@codekraft-studio Thanks for reply. Just to confirm, I mentioned about groupBy, not orderBy. Will it work for groupBy? I'll try from my end though.

karandesai28 avatar Jun 01 '17 06:06 karandesai28

@karandesai28 yea sorry I answered in early morning and I was still sleepy, I intended groupBy obviously :)

codekraft-studio avatar Jun 01 '17 11:06 codekraft-studio

Well I tried in groupBy, but it didn't work

karandesai28 avatar Jun 01 '17 12:06 karandesai28