0

I'm fairly confused as to which is the best way of doing this, since I didn't manage to find a package that solves this easily and other answers of similar problems don't address this properly.

I need to have a template that lists days and display all the documents created on each single day, for example:

10/27/2015 - Document A - Document B

10/26/2015 - Document C - Document D - Document E

Or, less vaguely:

10/23/2014 - John Smith received 10 points on Basquet - Paul Atreides received 20 points on Future Telling

10/21/2014 - Heisenberg received 25 points on National Trade - etc.

Being the displayed document something like a 'Reports' collection that joins 'Players' with 'Activities', for example.

What is the proper way of achieving this functionality? I guess creating a Days Collection is not the best option.

Thanks in advance

Gatosaurix
  • 13
  • 1
  • 5

1 Answers1

0

You can just create an array of days in your template helper, iterate over that with {{#each}} then have another helper that returns a cursor of documents for each day.

html:

<template name="docsByDay">
{{#each days}}
  Date: {{this}} <!-- 'this' will be an individual date ->
  {{#each documents this}}
    {{content}}
  {{/each}}
{{/each}}
</template>

Replace {{content}} with whatever field(s) you want to display from your collection.

js:

Template.docsByDay.helpers({
  days: function(){
    var arrayOfDates =[];
    // create your array based on the date range and interval you want
    return arrayOfDates;
  },
  documents: function(d){
    var start = Date(getFullYear(d),getMonth(d),getDate(d));
    var end = Date(getFullYear(d),getMonth(d),getDate(d)+1);
    return Documents.find({ date: { $gte: start, $lt: end }});
  }
});

See javascript - get array of dates between 2 dates

Community
  • 1
  • 1
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39