Search code examples
meteor

Meteor - color highlight table row when date=today


i would like to highlight the date if match.datetime if equal to today, May I have some hints?

in .html

{{#each match in matches}}
<tr class="center aligned"  bgcolor= "red">
    <td>
            {{match.datetime}}
    </td>
</tr>
{{/each}}

in .js

Template.List_matches_page.helpers({
    matches() {
        return Matches.find({}, {sort: {datetime: -1}});
    },
});

Solution

  • You can write a helper for that:

    {{#each match in matches}}
    <tr class="center aligned"  bgcolor= "red">
        <td class="{{#if isToday match.datetime}}color-bg{{/if}}">
                {{match.datetime}}
        </td>
    </tr>
    {{/each}}
    
    const now = new Date()
    const isToday = (date) => 
      date.getFullYear() === now.getFullYear() &&
      date.getMonth() === now.getMonth() &&
      date.getDate() === now.getDate()
    
    
    Template.List_matches_page.helpers({
        matches() {
            return Matches.find({}, {sort: {datetime: -1}});
        },
        isToday(date) {
          return isToday(date)
        }
    });
    

    If you have many places in your code, that require this check, you should rather use Template.registerHelper to avoid duplicate code in your templates: https://www.blazejs.org/api/templates.html#Template-registerHelper